From 2fbceb11e36de20174b663daea84e11c53f959ca Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 29 Apr 2026 15:01:35 +0000 Subject: [PATCH] Don't bail on district match early, check address This is the other half of doing proper district match via raw address - we have to use the address if available for looking up a district. --- platform/address.go | 18 ++++++++++++++++++ platform/district.go | 29 ++++++++++++++++------------- platform/publicreport.go | 2 +- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/platform/address.go b/platform/address.go index 85a0f685..e151c3e1 100644 --- a/platform/address.go +++ b/platform/address.go @@ -45,3 +45,21 @@ func AddressFromComplianceReportRequestID(ctx context.Context, public_id string) } return row, nil } + +func AddressLocation(ctx context.Context, address *models.Address) (*types.Location, error) { + if address == nil { + return nil, fmt.Errorf("nil address") + } + row, err := bob.One(ctx, db.PGInstance.BobDB, psql.Select( + sm.Columns( + models.Addresses.Columns.LocationLatitude.As("latitude"), + models.Addresses.Columns.LocationLongitude.As("longitude"), + ), + sm.From(models.Addresses.NameAs()), + sm.Where(models.Addresses.Columns.ID.EQ(psql.Arg(address.ID))), + ), scan.StructMapper[*types.Location]()) + if err != nil { + return nil, fmt.Errorf("query address: %w", err) + } + return row, nil +} diff --git a/platform/district.go b/platform/district.go index 9035e7b1..a3dbf06c 100644 --- a/platform/district.go +++ b/platform/district.go @@ -40,7 +40,7 @@ func DistrictForLocation(ctx context.Context, lng float64, lat float64) (*models return nil, errors.New("too many organizations") } } -func matchDistrict(ctx context.Context, location *types.Location, images []ImageUpload) (int32, error) { +func matchDistrict(ctx context.Context, location *types.Location, images []ImageUpload, address *models.Address) (int32, error) { var err error var org *models.Organization for _, image := range images { @@ -59,29 +59,32 @@ func matchDistrict(ctx context.Context, location *types.Location, images []Image return org.ID, nil } } - if location != nil { - if location.Longitude == 0 || location.Latitude == 0 { - org, err = DistrictCatchall(ctx) - if err != nil { - return 0, fmt.Errorf("get catchall: %w", err) - } - log.Debug().Int32("id", org.ID).Msg("No location from images, no latlng for the report itself, using catchall") - return org.ID, nil + if location != nil && location.Longitude != 0 && location.Latitude != 0 { + org, err = DistrictForLocation(ctx, location.Longitude, location.Latitude) + if err != nil { + return 0, fmt.Errorf("Failed to get district for location: %w", err) + } + } + if address != nil { + log.Debug().Msg("doing district match via address...") + location, err = AddressLocation(ctx, address) + if err != nil { + return 0, fmt.Errorf("location for address: %w", err) } org, err = DistrictForLocation(ctx, location.Longitude, location.Latitude) if err != nil { - log.Warn().Err(err).Msg("Failed to get district for location") - return 0, fmt.Errorf("Failed to get district for location: %w", err) + return 0, fmt.Errorf("Failed to get district for location from address: %w", err) } + log.Debug().Float64("loc.lat", location.Latitude).Float64("loc.lng", location.Longitude).Bool("org", org != nil).Msg("address match") } if org == nil { org, err = DistrictCatchall(ctx) if err != nil { return 0, fmt.Errorf("get catchall: %w", err) } - log.Debug().Err(err).Int32("id", org.ID).Msg("No district match by report location, using catchall") + log.Debug().Err(err).Int32("id", org.ID).Msg("No district match by report location, images, or address, using catchall") return org.ID, nil } - log.Debug().Err(err).Int32("org_id", org.ID).Msg("Found district match by report location") + log.Debug().Err(err).Int32("org_id", org.ID).Msg("Found district match for report") return org.ID, nil } diff --git a/platform/publicreport.go b/platform/publicreport.go index de354b60..2b484324 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -330,7 +330,7 @@ func publicReportCreate(ctx context.Context, setter_report models.PublicreportRe return nil, fmt.Errorf("Failed to save image uploads: %w", err) } if organization_id == 0 { - organization_id, err = matchDistrict(ctx, location, images) + organization_id, err = matchDistrict(ctx, location, images, addr) if err != nil { log.Warn().Err(err).Msg("Failed to match district") }