diff --git a/platform/address.go b/platform/address.go index a3916392..c89ced69 100644 --- a/platform/address.go +++ b/platform/address.go @@ -1,7 +1,35 @@ package platform import ( + "context" + + "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/platform/types" + "github.com/stephenafamo/scan" ) type Address = types.Address + +func AddressList(ctx context.Context, ids []int32) ([]*types.Address, error) { + rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select( + sm.Columns( + "COALESCE(address.country, 'usa') AS \"country\"", + "COALESCE(address.gid, '') AS \"gid\"", + "COALESCE(address.locality, '') AS \"locality\"", + "COALESCE(address.number_, '') AS \"number\"", + "COALESCE(address.postal_code, '') AS \"postal_code\"", + "COALESCE(address.region, '') AS \"region\"", + "COALESCE(address.street, '') AS \"street\"", + "COALESCE(address.unit, '') AS \"unit\"", + // This will work great, up until we add polygons to signal + "COALESCE(ST_Y(address.location_latitude), 0) AS \"location.latitude\"", + "COALESCE(ST_X(address.location_longitude), 0) AS \"location.longitude\"", + ), + sm.From("address"), + sm.Where(psql.Quote("address", "id").EQ(psql.Arg(ids))), + ), scan.StructMapper[*types.Address]()) + return rows, err +} diff --git a/platform/types/site.go b/platform/types/site.go index 8cbd7f0e..05f27e74 100644 --- a/platform/types/site.go +++ b/platform/types/site.go @@ -5,17 +5,17 @@ import ( ) type Site struct { - Address Address `json:"address"` - Created time.Time `json:"created"` - CreatorID int32 `json:"creator_id"` - FileID int32 `json:"file_id"` - ID int32 `json:"id"` - Notes string `json:"notes"` - OrganizationID int32 `json:"organization_id"` - Owner *Contact `json:"owner"` - ParcelID *int32 `json:"parcel_id"` - Resident *Contact `json:"resident"` - ResidentOwned bool `json:"resident_owned"` - Tags map[string]string `json:"tags"` - Version int32 `json:"version"` + Address Address `db:"address" json:"address"` + Created time.Time `db:"created" json:"created"` + CreatorID int32 `db:"creator_id" json:"creator_id"` + FileID int32 `db:"file_id" json:"file_id"` + ID int32 `db:"id" json:"id"` + Notes string `db:"notes" json:"notes"` + OrganizationID int32 `db:"organization_id" json:"organization_id"` + Owner *Contact `db:"owner" json:"owner"` + ParcelID *int32 `db:"parcel_id" json:"parcel_id"` + Resident *Contact `db:"resident" json:"resident"` + ResidentOwned bool `db:"resident_owned" json:"resident_owned"` + Tags map[string]string `db:"tags" json:"tags"` + Version int32 `db:"version" json:"version"` } diff --git a/platform/upload.go b/platform/upload.go index c7560ce1..08c5f7b5 100644 --- a/platform/upload.go +++ b/platform/upload.go @@ -199,6 +199,20 @@ func getUploadDetailPool(ctx context.Context, file *models.FileuploadFile) (*Upl if err != nil { return nil, fmt.Errorf("Failed to query pools for %d: %w", file.ID, err) } + address_ids := make([]int32, 0) + for _, r := range pool_rows { + if r.AddressID.IsValue() { + address_ids = append(address_ids, r.AddressID.MustGet()) + } + } + addresses, err := AddressList(ctx, address_ids) + if err != nil { + return nil, fmt.Errorf("get address list: %w", err) + } + addresses_by_id := make(map[int32]*types.Address, len(address_ids)) + for _, a := range addresses { + addresses_by_id[*a.ID] = a + } pools := make([]UploadPoolRow, 0) count_existing := 0 @@ -223,15 +237,21 @@ func getUploadDetailPool(ctx context.Context, file *models.FileuploadFile) (*Upl if !ok { errors = []UploadPoolError{} } - pools = append(pools, UploadPoolRow{ - Address: types.Address{ + var address *types.Address + if r.AddressID.IsValue() { + address = addresses_by_id[r.AddressID.MustGet()] + } else { + address = &types.Address{ Country: "usa", Locality: r.AddressLocality, Number: r.AddressNumber, PostalCode: r.AddressPostalCode, Region: r.AddressRegion, Street: r.AddressStreet, - }, + } + } + pools = append(pools, UploadPoolRow{ + Address: *address, Condition: r.Condition.String(), Errors: errors, Status: status,