From b23fc6edc59ca110ef254df19d57d46c97c32ffc Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Fri, 10 Apr 2026 15:38:05 +0000 Subject: [PATCH] Fix dodgy creation of compliance report in database --- api/routes.go | 1 + platform/district.go | 31 +++++++++++++++++-------------- platform/publicreport.go | 2 +- resource/compliance.go | 2 +- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/api/routes.go b/api/routes.go index bb1fdae8..2be78a2c 100644 --- a/api/routes.go +++ b/api/routes.go @@ -43,6 +43,7 @@ func AddRoutes(r *mux.Router) { r.Handle("/review-task", authenticatedHandlerJSON(review_task.List)).Methods("GET") compliance := resource.Compliance(router) r.HandleFunc("/rmo/compliance", handlerFormPost(compliance.Create)).Methods("POST") + r.HandleFunc("/rmo/compliance/{public_id}", handlerFormPost(compliance.Update)).Methods("PUT") nuisance := resource.Nuisance(router) r.HandleFunc("/rmo/nuisance", handlerFormPost(nuisance.Create)).Methods("POST") water := resource.Water(router) diff --git a/platform/district.go b/platform/district.go index a2950342..e2336ff2 100644 --- a/platform/district.go +++ b/platform/district.go @@ -9,6 +9,7 @@ import ( "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/nidus-sync/db" "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/Gleipnir-Technology/nidus-sync/platform/types" "github.com/rs/zerolog/log" ) @@ -39,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, longitude, latitude float64, images []ImageUpload) (*int32, error) { +func matchDistrict(ctx context.Context, location *types.Location, images []ImageUpload) (*int32, error) { var err error var org *models.Organization for _, image := range images { @@ -58,27 +59,29 @@ func MatchDistrict(ctx context.Context, longitude, latitude float64, images []Im return &org.ID, nil } } - if longitude == 0 || latitude == 0 { - org, err = DistrictCatchall(ctx) - if err != nil { - return nil, fmt.Errorf("get catchall: %w", err) + if location != nil { + if location.Longitude == 0 || location.Latitude == 0 { + org, err = DistrictCatchall(ctx) + if err != nil { + return nil, 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 + } + org, err = DistrictForLocation(ctx, location.Longitude, location.Latitude) + if err != nil { + log.Warn().Err(err).Msg("Failed to get district for location") + return nil, fmt.Errorf("Failed to get district for location: %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 - } - org, err = DistrictForLocation(ctx, longitude, latitude) - if err != nil { - log.Warn().Err(err).Msg("Failed to get district for location") - return nil, fmt.Errorf("Failed to get district for location: %w", err) } if org == nil { org, err = DistrictCatchall(ctx) if err != nil { return nil, fmt.Errorf("get catchall: %w", err) } - log.Debug().Err(err).Float64("lng", longitude).Float64("lat", latitude).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, using catchall") return &org.ID, nil } - log.Debug().Err(err).Int32("org_id", org.ID).Float64("lng", longitude).Float64("lat", latitude).Msg("Found district match by report location") + log.Debug().Err(err).Int32("org_id", org.ID).Msg("Found district match by report location") return &org.ID, nil } diff --git a/platform/publicreport.go b/platform/publicreport.go index f2c719b0..9c01bb0b 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -152,7 +152,7 @@ func reportCreate(ctx context.Context, setter_report models.PublicreportReportSe return nil, fmt.Errorf("Failed to save image uploads: %w", err) } var organization_id *int32 - organization_id, err = MatchDistrict(ctx, location.Longitude, location.Latitude, images) + organization_id, err = matchDistrict(ctx, location, images) if err != nil { log.Warn().Err(err).Msg("Failed to match district") } diff --git a/resource/compliance.go b/resource/compliance.go index 11a8b4ef..c3891170 100644 --- a/resource/compliance.go +++ b/resource/compliance.go @@ -31,10 +31,10 @@ type compliance struct { URI string `json:"uri"` } type complianceForm struct { + ClientID string `schema:"client_id"` DistrictID string `schema:"district"` Location *types.Location `schema:"location"` Locator *Locator `schema:"locator"` - ClientID string `schema:"client_id"` } func (res *complianceR) Create(ctx context.Context, r *http.Request, n complianceForm) (*compliance, *nhttp.ErrorWithStatus) {