Fix publicreport creation

The consistency is good, but I added some errors, like not using an enum
This commit is contained in:
Eli Ribble 2026-03-18 18:45:18 +00:00
parent 1d2570c912
commit 341c3ef6b9
No known key found for this signature in database
6 changed files with 95 additions and 25 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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