From c5f6db0b7353579e0e0766a13e159add991894fa Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Sun, 1 Feb 2026 02:37:35 +0000 Subject: [PATCH] Lookup district and show on report submission complete --- platform/report/notification.go | 45 +++++++++++++++++++++++++++++++++ rmo/district.go | 23 +++++++++++++++++ rmo/nuisance.go | 11 +++++--- rmo/root.go | 5 ---- rmo/template/nuisance.html | 4 +-- 5 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 rmo/district.go diff --git a/platform/report/notification.go b/platform/report/notification.go index cd1a4c35..c5dd4caa 100644 --- a/platform/report/notification.go +++ b/platform/report/notification.go @@ -7,13 +7,17 @@ import ( "math/big" "strings" + "github.com/Gleipnir-Technology/bob" "github.com/Gleipnir-Technology/bob/dialect/psql" + "github.com/Gleipnir-Technology/bob/dialect/psql/sm" "github.com/Gleipnir-Technology/bob/dialect/psql/um" "github.com/Gleipnir-Technology/nidus-sync/background" "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/db/sql" "github.com/Gleipnir-Technology/nidus-sync/platform/text" "github.com/rs/zerolog/log" + "github.com/stephenafamo/scan" ) type ErrorWithCode struct { @@ -34,6 +38,31 @@ type SomeReport struct { tableName string } +func (sr SomeReport) districtID(ctx context.Context) *int32 { + type _Row struct { + OrganizationID int32 + } + + from := sm.From("no-such-table") + switch sr.tableName { + case "nuisance": + from = sm.From("publicreport.nuisance") + case "pool": + from = sm.From("publicreport.pool") + default: + log.Error().Str("table-name", sr.tableName).Msg("Programmer error, non-exhaustive switch statement in SomeReport.districtID") + } + row, err := bob.One(ctx, db.PGInstance.BobDB, psql.Select( + from, + sm.Columns("organization_id"), + sm.Where(psql.Quote("public_id").EQ(psql.Arg(sr.reportID))), + ), scan.StructMapper[_Row]()) + if err != nil { + log.Warn().Err(err).Msg("Failed to query for organization_id") + return nil + } + return &row.OrganizationID +} func (sr SomeReport) updateReporterEmail(ctx context.Context, email string) *ErrorWithCode { table := um.Table("so-such-table") switch sr.tableName { @@ -89,6 +118,22 @@ func (sr SomeReport) updateReporterPhone(ctx context.Context, phone text.E164) * return nil } +func DistrictForReport(ctx context.Context, report_id string) (*models.Organization, error) { + some_report, err := findSomeReport(ctx, report_id) + if err != nil { + return nil, fmt.Errorf("Failed to find report %s: %w", report_id, err) + } + org_id := some_report.districtID(ctx) + if org_id == nil { + return nil, nil + } + result, e := models.FindOrganization(ctx, db.PGInstance.BobDB, *org_id) + if e != nil { + return nil, fmt.Errorf("Failed to load organization %d: %w", org_id, e) + } + return result, nil +} + // GenerateReportID creates a 12-character random string using only unambiguous // capital letters and numbers func GenerateReportID() (string, error) { diff --git a/rmo/district.go b/rmo/district.go new file mode 100644 index 00000000..56cd085a --- /dev/null +++ b/rmo/district.go @@ -0,0 +1,23 @@ +package rmo + +import ( + "github.com/Gleipnir-Technology/nidus-sync/config" + "github.com/Gleipnir-Technology/nidus-sync/db/models" +) + +type ContentDistrict struct { + Name string + URLLogo string + URLWebsite string +} + +func newContentDistrict(d *models.Organization) *ContentDistrict { + if d == nil { + return nil + } + return &ContentDistrict{ + Name: d.Name, + URLLogo: config.MakeURLNidus("/api/district/%s/logo", d.Slug.GetOr("unset")), + URLWebsite: d.Website.GetOr(""), + } +} diff --git a/rmo/nuisance.go b/rmo/nuisance.go index 6706ed69..30bdefd8 100644 --- a/rmo/nuisance.go +++ b/rmo/nuisance.go @@ -49,13 +49,18 @@ func getNuisance(w http.ResponseWriter, r *http.Request) { ) } func getSubmitComplete(w http.ResponseWriter, r *http.Request) { - report := r.URL.Query().Get("report") + report_id := r.URL.Query().Get("report") + district, err := report.DistrictForReport(r.Context(), report_id) + if err != nil { + respondError(w, fmt.Sprintf("Failed to get district for report '%s'", report_id, err), err, http.StatusInternalServerError) + return + } html.RenderOrError( w, SubmitCompleteT, ContentNuisanceSubmitComplete{ - District: nil, - ReportID: report, + District: newContentDistrict(district), + ReportID: report_id, URL: makeContentURL(), }, ) diff --git a/rmo/root.go b/rmo/root.go index 9e7ce3db..070765c8 100644 --- a/rmo/root.go +++ b/rmo/root.go @@ -9,11 +9,6 @@ import ( "github.com/rs/zerolog/log" ) -type ContentDistrict struct { - Name string - URLLogo string - URLWebsite string -} type ContentPrivacy struct { Address string Company string diff --git a/rmo/template/nuisance.html b/rmo/template/nuisance.html index d6d5adfe..321d2851 100644 --- a/rmo/template/nuisance.html +++ b/rmo/template/nuisance.html @@ -94,8 +94,8 @@ document.addEventListener('DOMContentLoaded', function() { console.log("Got location", position); latitudeInput.value = position.coords.latitude; longitudeInput.value = position.coords.longitude; - latLngAccuracyType.value = 'browser'; - latLngAccuracyValue.value = position.coords.accuracy; + latlngAccuracyType.value = 'browser'; + latlngAccuracyValue.value = position.coords.accuracy; mapLocator.JumpTo({ center: { lng: position.coords.longitude,