diff --git a/platform/lead.go b/platform/lead.go index 0943165a..8208b09a 100644 --- a/platform/lead.go +++ b/platform/lead.go @@ -6,6 +6,8 @@ import ( "time" "github.com/Gleipnir-Technology/bob" + "github.com/Gleipnir-Technology/bob/dialect/psql" + "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/nidus-sync/db" "github.com/Gleipnir-Technology/nidus-sync/db/enums" "github.com/Gleipnir-Technology/nidus-sync/db/models" @@ -13,6 +15,7 @@ import ( "github.com/aarondl/opt/omit" "github.com/aarondl/opt/omitnull" //"github.com/rs/zerolog/log" + "github.com/stephenafamo/scan" ) // Create a lead from the given signal and site @@ -45,3 +48,31 @@ func leadCreate(ctx context.Context, txn bob.Executor, user User, signal_id int3 } return &lead.ID, nil } +func leadsBySiteID(ctx context.Context, site_ids []int32) (map[int32][]types.Lead, error) { + rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select( + sm.Columns( + "id", + "site_id", + ), + sm.From("lead"), + sm.Where( + models.Leads.Columns.SiteID.EQ(psql.Any(site_ids)), + ), + ), scan.StructMapper[types.Lead]()) + if err != nil { + return nil, fmt.Errorf("query leads: %w", err) + } + results := make(map[int32][]types.Lead, len(site_ids)) + for _, site_id := range site_ids { + results[site_id] = make([]types.Lead, 0) + } + for _, row := range rows { + leads, ok := results[row.SiteID] + if !ok { + return nil, fmt.Errorf("impossible") + } + leads = append(leads, row) + results[row.SiteID] = leads + } + return results, nil +} diff --git a/platform/site.go b/platform/site.go index a2fae236..28aea195 100644 --- a/platform/site.go +++ b/platform/site.go @@ -117,6 +117,18 @@ func SiteList(ctx context.Context, user User, limit int) ([]*types.Site, error) } result.Features = features } + leads_by_site_id, err := leadsBySiteID(ctx, site_ids) + if err != nil { + return nil, fmt.Errorf("query leads for sites: %w", err) + } + for _, result := range results { + leads, ok := leads_by_site_id[result.ID] + if !ok { + return nil, fmt.Errorf("impossible") + } + result.Leads = leads + } + return results, nil } func SitesByID(ctx context.Context, ids []int32) (map[int32]*models.Site, error) { diff --git a/platform/types/site.go b/platform/types/site.go index c1d53b82..1759272c 100644 --- a/platform/types/site.go +++ b/platform/types/site.go @@ -13,6 +13,7 @@ type Site struct { Features []Feature `db:"-" json:"features"` FileID int32 `db:"file_id" json:"file_id"` ID int32 `db:"id" json:"id"` + Leads []Lead `db:"-" json:"leads"` Notes string `db:"notes" json:"notes"` OrganizationID int32 `db:"organization_id" json:"organization_id"` Owner Contact `db:"owner" json:"owner"` diff --git a/ts/components/ReviewSiteColumnDetail.vue b/ts/components/ReviewSiteColumnDetail.vue index 1069d217..c9bda9fd 100644 --- a/ts/components/ReviewSiteColumnDetail.vue +++ b/ts/components/ReviewSiteColumnDetail.vue @@ -35,6 +35,19 @@ Parcel Description {{ selectedSite?.parcel?.description }} + + + Features
+ + + + + Leads: {{ selectedSite?.leads.length ?? "none" }} + diff --git a/ts/type/api.ts b/ts/type/api.ts index 8f6e62b1..577c91c9 100644 --- a/ts/type/api.ts +++ b/ts/type/api.ts @@ -528,12 +528,23 @@ export interface Parcel { description: string; id: number; } +export interface Feature { + id: number; + location: Location; + type: string; +} +export interface Lead { + id: number; + site_id: number; +} export interface Site { address: Address; created: string; creator_id: number; + features: Feature[]; file_id: number; id: number; + leads: Lead[]; notes: string; organization_id: number; owner?: Contact;