From 341c3ef6b9c494afe05a3c7cfe3d4de09be30b64 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 18 Mar 2026 18:45:18 +0000 Subject: [PATCH] Fix publicreport creation The consistency is good, but I added some errors, like not using an enum --- db/dbinfo/publicreport.report.bob.go | 21 +----- db/enums/enums.bob.go | 73 +++++++++++++++++++ .../00115_publicreport_reporttype.sql | 6 ++ db/models/publicreport.report.bob.go | 8 +- rmo/nuisance.go | 5 +- rmo/water.go | 7 +- 6 files changed, 95 insertions(+), 25 deletions(-) create mode 100644 db/migrations/00115_publicreport_reporttype.sql diff --git a/db/dbinfo/publicreport.report.bob.go b/db/dbinfo/publicreport.report.bob.go index 6cca69e5..3e7d8741 100644 --- a/db/dbinfo/publicreport.report.bob.go +++ b/db/dbinfo/publicreport.report.bob.go @@ -206,7 +206,7 @@ var PublicreportReports = Table[ }, ReportType: column{ Name: "report_type", - DBType: "text", + DBType: "publicreport.reporttype", Default: "", Comment: "", Nullable: false, @@ -318,16 +318,7 @@ var PublicreportReports = Table[ Comment: "", }, }, - Checks: publicreportReportChecks{ - ReportReportTypeCheck: check{ - constraint: constraint{ - Name: "report_report_type_check", - Columns: []string{"report_type"}, - Comment: "", - }, - Expression: "(report_type = ANY (ARRAY['nuisance'::text, 'water'::text]))", - }, - }, + Comment: "", } @@ -398,12 +389,8 @@ func (u publicreportReportUniques) AsSlice() []constraint { } } -type publicreportReportChecks struct { - ReportReportTypeCheck check -} +type publicreportReportChecks struct{} func (c publicreportReportChecks) AsSlice() []check { - return []check{ - c.ReportReportTypeCheck, - } + return []check{} } diff --git a/db/enums/enums.bob.go b/db/enums/enums.bob.go index 4537c154..7bf7b211 100644 --- a/db/enums/enums.bob.go +++ b/db/enums/enums.bob.go @@ -2136,6 +2136,79 @@ func (e *PublicreportReportstatustype) Scan(value any) error { return nil } +// Enum values for PublicreportReporttype +const ( + PublicreportReporttypeNuisance PublicreportReporttype = "nuisance" + PublicreportReporttypeWater PublicreportReporttype = "water" +) + +func AllPublicreportReporttype() []PublicreportReporttype { + return []PublicreportReporttype{ + PublicreportReporttypeNuisance, + PublicreportReporttypeWater, + } +} + +type PublicreportReporttype string + +func (e PublicreportReporttype) String() string { + return string(e) +} + +func (e PublicreportReporttype) Valid() bool { + switch e { + case PublicreportReporttypeNuisance, + PublicreportReporttypeWater: + return true + default: + return false + } +} + +// useful when testing in other packages +func (e PublicreportReporttype) All() []PublicreportReporttype { + return AllPublicreportReporttype() +} + +func (e PublicreportReporttype) MarshalText() ([]byte, error) { + return []byte(e), nil +} + +func (e *PublicreportReporttype) UnmarshalText(text []byte) error { + return e.Scan(text) +} + +func (e PublicreportReporttype) MarshalBinary() ([]byte, error) { + return []byte(e), nil +} + +func (e *PublicreportReporttype) UnmarshalBinary(data []byte) error { + return e.Scan(data) +} + +func (e PublicreportReporttype) Value() (driver.Value, error) { + return string(e), nil +} + +func (e *PublicreportReporttype) Scan(value any) error { + switch x := value.(type) { + case string: + *e = PublicreportReporttype(x) + case []byte: + *e = PublicreportReporttype(x) + case nil: + return fmt.Errorf("cannot nil into PublicreportReporttype") + default: + return fmt.Errorf("cannot scan type %T: %v", value, value) + } + + if !e.Valid() { + return fmt.Errorf("invalid PublicreportReporttype value: %s", *e) + } + + return nil +} + // Enum values for Reviewtaskresolutiontype const ( ReviewtaskresolutiontypeCommitted Reviewtaskresolutiontype = "committed" diff --git a/db/migrations/00115_publicreport_reporttype.sql b/db/migrations/00115_publicreport_reporttype.sql new file mode 100644 index 00000000..c81fb3b0 --- /dev/null +++ b/db/migrations/00115_publicreport_reporttype.sql @@ -0,0 +1,6 @@ +-- +goose Up +ALTER TABLE publicreport.report DROP CONSTRAINT report_report_type_check; +CREATE TYPE publicreport.ReportType AS ENUM('nuisance', 'water'); +ALTER TABLE publicreport.report ALTER COLUMN report_type TYPE publicreport.ReportType USING report_type::publicreport.ReportType; +-- +goose Down + diff --git a/db/models/publicreport.report.bob.go b/db/models/publicreport.report.bob.go index eaccca9a..836c7156 100644 --- a/db/models/publicreport.report.bob.go +++ b/db/models/publicreport.report.bob.go @@ -48,7 +48,7 @@ type PublicreportReport struct { ReporterEmail string `db:"reporter_email" ` ReporterPhone string `db:"reporter_phone" ` ReporterContactConsent null.Val[bool] `db:"reporter_contact_consent" ` - ReportType string `db:"report_type" ` + ReportType enums.PublicreportReporttype `db:"report_type" ` Reviewed null.Val[time.Time] `db:"reviewed" ` ReviewerID null.Val[int32] `db:"reviewer_id" ` Status enums.PublicreportReportstatustype `db:"status" ` @@ -178,7 +178,7 @@ type PublicreportReportSetter struct { ReporterEmail omit.Val[string] `db:"reporter_email" ` ReporterPhone omit.Val[string] `db:"reporter_phone" ` ReporterContactConsent omitnull.Val[bool] `db:"reporter_contact_consent" ` - ReportType omit.Val[string] `db:"report_type" ` + ReportType omit.Val[enums.PublicreportReporttype] `db:"report_type" ` Reviewed omitnull.Val[time.Time] `db:"reviewed" ` ReviewerID omitnull.Val[int32] `db:"reviewer_id" ` Status omit.Val[enums.PublicreportReportstatustype] `db:"status" ` @@ -1859,7 +1859,7 @@ type publicreportReportWhere[Q psql.Filterable] struct { ReporterEmail psql.WhereMod[Q, string] ReporterPhone psql.WhereMod[Q, string] ReporterContactConsent psql.WhereNullMod[Q, bool] - ReportType psql.WhereMod[Q, string] + ReportType psql.WhereMod[Q, enums.PublicreportReporttype] Reviewed psql.WhereNullMod[Q, time.Time] ReviewerID psql.WhereNullMod[Q, int32] Status psql.WhereMod[Q, enums.PublicreportReportstatustype] @@ -1892,7 +1892,7 @@ func buildPublicreportReportWhere[Q psql.Filterable](cols publicreportReportColu ReporterEmail: psql.Where[Q, string](cols.ReporterEmail), ReporterPhone: psql.Where[Q, string](cols.ReporterPhone), ReporterContactConsent: psql.WhereNull[Q, bool](cols.ReporterContactConsent), - ReportType: psql.Where[Q, string](cols.ReportType), + ReportType: psql.Where[Q, enums.PublicreportReporttype](cols.ReportType), Reviewed: psql.WhereNull[Q, time.Time](cols.Reviewed), ReviewerID: psql.WhereNull[Q, int32](cols.ReviewerID), Status: psql.Where[Q, enums.PublicreportReportstatustype](cols.Status), diff --git a/rmo/nuisance.go b/rmo/nuisance.go index 603da8c7..4ae5e349 100644 --- a/rmo/nuisance.go +++ b/rmo/nuisance.go @@ -163,6 +163,7 @@ func postNuisance(w http.ResponseWriter, r *http.Request) { ReporterEmail: omit.From(""), ReporterName: omit.From(""), ReporterPhone: omit.From(""), + ReportType: omit.From(enums.PublicreportReporttypeNuisance), Status: omit.From(enums.PublicreportReportstatustypeReported), } setter_nuisance := models.PublicreportNuisanceSetter{ @@ -183,6 +184,6 @@ func postNuisance(w http.ResponseWriter, r *http.Request) { TodEvening: omit.From(tod_evening), TodNight: omit.From(tod_night), } - public_id, err := platform.ReportNuisanceCreate(ctx, setter_report, setter_nuisance, latlng, address, uploads) - http.Redirect(w, r, fmt.Sprintf("/submit-complete?report=%s", public_id), http.StatusFound) + report, err := platform.ReportNuisanceCreate(ctx, setter_report, setter_nuisance, latlng, address, uploads) + http.Redirect(w, r, fmt.Sprintf("/submit-complete?report=%s", report.PublicID), http.StatusFound) } diff --git a/rmo/water.go b/rmo/water.go index 7ce156ab..bbed67cf 100644 --- a/rmo/water.go +++ b/rmo/water.go @@ -107,6 +107,8 @@ func postWater(w http.ResponseWriter, r *http.Request) { AddressRegion: omit.From(address_region), Created: omit.From(time.Now()), //H3cell: omitnull.From(geospatial.Cell.String()), + LatlngAccuracyType: omit.From(latlng.AccuracyType), + LatlngAccuracyValue: omit.From(float32(latlng.AccuracyValue)), //Location: add later MapZoom: omit.From(latlng.MapZoom), //OrganizationID: omitnull.FromPtr(organization_id), @@ -114,6 +116,7 @@ func postWater(w http.ResponseWriter, r *http.Request) { ReporterEmail: omit.From(""), ReporterName: omit.From(""), ReporterPhone: omit.From(""), + ReportType: omit.From(enums.PublicreportReporttypeWater), Status: omit.From(enums.PublicreportReportstatustypeReported), } setter_water := models.PublicreportWaterSetter{ @@ -135,12 +138,12 @@ func postWater(w http.ResponseWriter, r *http.Request) { OwnerPhone: omit.From(owner_phone), //ReportID omit.Val[int32] } - public_id, err := platform.ReportWaterCreate(ctx, setter_report, setter_water, latlng, address, uploads) + report, err := platform.ReportWaterCreate(ctx, setter_report, setter_water, latlng, address, uploads) if err != nil { respondError(w, "Failed to save new report", err, http.StatusInternalServerError) return } - http.Redirect(w, r, fmt.Sprintf("/submit-complete?report=%s", public_id), http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("/submit-complete?report=%s", report.PublicID), http.StatusFound) } func postWaterDistrict(w http.ResponseWriter, r *http.Request) { }