Fix setting address on compliance reports

This error was subtle. First, we want to set the GID and raw content
directly using the updater instead of doing two round trips because we
can. Second, we want to do some geocoding if the address isn't already
in the system. Likely it is, because the frontend would have requested a
geocode, but it's possible that it isn't.
This commit is contained in:
Eli Ribble 2026-05-08 22:43:57 +00:00
parent 24a3610c4c
commit d1ba2f53fa
No known key found for this signature in database

View file

@ -187,6 +187,12 @@ func PublicReportUpdateCompliance(ctx context.Context, public_id string, report_
} }
// Avoid attempting to perform an empty update // Avoid attempting to perform an empty update
if address != nil {
report_updates.Model.AddressGid = address.GID
report_updates.Set(tablepublicreport.Report.AddressGid)
report_updates.Model.AddressRaw = address.Raw
report_updates.Set(tablepublicreport.Report.AddressRaw)
}
err = report_updates.Execute(ctx, txn, int64(report.ID)) err = report_updates.Execute(ctx, txn, int64(report.ID))
if err != nil { if err != nil {
return fmt.Errorf("update report: %w", err) return fmt.Errorf("update report: %w", err)
@ -196,7 +202,7 @@ func PublicReportUpdateCompliance(ctx context.Context, public_id string, report_
return fmt.Errorf("update compliance: %w", err) return fmt.Errorf("update compliance: %w", err)
} }
if address != nil { if address != nil {
err = publicReportUpdateAddress(ctx, txn, report, *address) err = publicReportUpdateAddressID(ctx, txn, report, *address)
if err != nil { if err != nil {
return fmt.Errorf("update address: %w", err) return fmt.Errorf("update address: %w", err)
} }
@ -406,23 +412,23 @@ func publicReportCreate(ctx context.Context, setter_report modelpublicreport.Rep
) )
return result, nil return result, nil
} }
func publicReportUpdateAddress(ctx context.Context, txn db.Tx, report *modelpublicreport.Report, address types.Address) error { func publicReportUpdateAddressID(ctx context.Context, txn db.Tx, report *modelpublicreport.Report, address types.Address) error {
var err error
if address.GID == "" && address.Raw != "" {
geo_res, err := geocode.GeocodeRaw(ctx, nil, address.Raw)
if err != nil {
return fmt.Errorf("Failed to geocode raw: %w", err)
}
statement := tablepublicreport.Report.UPDATE( statement := tablepublicreport.Report.UPDATE(
tablepublicreport.Report.AddressGid, tablepublicreport.Report.AddressID,
tablepublicreport.Report.AddressRaw,
).SET( ).SET(
postgres.String(address.GID), tablepublicreport.Report.AddressID.SET(postgres.Int(int64(*geo_res.Address.ID))),
postgres.String(address.Raw), ).WHERE(
).FROM(tablepublic.Address).
WHERE(
tablepublicreport.Report.ID.EQ(postgres.Int(int64(report.ID))), tablepublicreport.Report.ID.EQ(postgres.Int(int64(report.ID))),
) )
err := db.ExecuteNoneTx(ctx, txn, statement) err = db.ExecuteNoneTx(ctx, txn, statement)
} else {
if err != nil { statement := tablepublicreport.Report.UPDATE(
return fmt.Errorf("update report: %w", err)
}
statement = tablepublicreport.Report.UPDATE(
tablepublicreport.Report.AddressID, tablepublicreport.Report.AddressID,
).SET( ).SET(
tablepublic.Address.SELECT( tablepublic.Address.SELECT(
@ -434,6 +440,7 @@ func publicReportUpdateAddress(ctx context.Context, txn db.Tx, report *modelpubl
tablepublicreport.Report.ID.EQ(postgres.Int(int64(report.ID))), tablepublicreport.Report.ID.EQ(postgres.Int(int64(report.ID))),
) )
err = db.ExecuteNoneTx(ctx, txn, statement) err = db.ExecuteNoneTx(ctx, txn, statement)
}
if err != nil { if err != nil {
return fmt.Errorf("update report address_id: %w", err) return fmt.Errorf("update report address_id: %w", err)
} }