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.
This commit is contained in:
Eli Ribble 2026-04-29 15:01:35 +00:00
parent 524353bfa1
commit 2fbceb11e3
No known key found for this signature in database
3 changed files with 35 additions and 14 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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")
}