Lookup district and show on report submission complete

This commit is contained in:
Eli Ribble 2026-02-01 02:37:35 +00:00
parent d28e3e2ccc
commit c5f6db0b73
No known key found for this signature in database
5 changed files with 78 additions and 10 deletions

View file

@ -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) {

23
rmo/district.go Normal file
View file

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

View file

@ -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(),
},
)

View file

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

View file

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