diff --git a/platform/client.go b/platform/client.go index d7cd5faa..bad32075 100644 --- a/platform/client.go +++ b/platform/client.go @@ -9,22 +9,27 @@ import ( "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/aarondl/opt/omit" "github.com/google/uuid" + "github.com/rs/zerolog/log" ) func EnsureClient(ctx context.Context, client uuid.UUID, user_agent string) error { _, err := models.PublicreportClients.Query( models.SelectWhere.PublicreportClients.UUID.EQ(client), ).One(ctx, db.PGInstance.BobDB) - if err != nil { - if err.Error() == "sql: no rows in result set" { - return nil - } + if err == nil { + log.Debug().Str("client", client.String()).Msg("already exists") + return nil + } else if err != nil && err.Error() != "sql: no rows in result set" { return fmt.Errorf("failed existing client %s: %w", client.String(), err) } - models.PublicreportClients.Insert(&models.PublicreportClientSetter{ + _, err = models.PublicreportClients.Insert(&models.PublicreportClientSetter{ Created: omit.From(time.Now()), UserAgent: omit.From(user_agent), UUID: omit.From(client), }).One(ctx, db.PGInstance.BobDB) + if err != nil { + return fmt.Errorf("insert client: %w", err) + } + log.Debug().Str("client", client.String()).Str("ua", user_agent).Msg("Created client") return nil } diff --git a/resource/publicreport_compliance.go b/resource/publicreport_compliance.go index e7edd891..af6e6204 100644 --- a/resource/publicreport_compliance.go +++ b/resource/publicreport_compliance.go @@ -49,7 +49,10 @@ func (res *complianceR) ByID(ctx context.Context, r *http.Request, query QueryPa } func (res *complianceR) Create(ctx context.Context, r *http.Request, n publicreportComplianceForm) (*compliance, *nhttp.ErrorWithStatus) { user_agent := r.Header.Get("User-Agent") - platform.EnsureClient(ctx, n.ClientID, user_agent) + err := platform.EnsureClient(ctx, n.ClientID, user_agent) + if err != nil { + return nil, nhttp.NewError("Failed to ensure client: %w", err) + } setter_report := models.PublicreportReportSetter{ //AddressID: omitnull.From(latlng.Cell.String()), AddressGid: omit.From(""), diff --git a/resource/publicreport_notification.go b/resource/publicreport_notification.go index ac41b856..e905228b 100644 --- a/resource/publicreport_notification.go +++ b/resource/publicreport_notification.go @@ -15,6 +15,7 @@ type publicreportNotificationR struct { } type publicreportNotification struct { + CanSMS bool `json:"can_sms"` Consent bool `json:"consent"` Email string `json:"email"` Name string `json:"name"` diff --git a/resource/publicreport_nuisance.go b/resource/publicreport_nuisance.go index 4434392c..892c077c 100644 --- a/resource/publicreport_nuisance.go +++ b/resource/publicreport_nuisance.go @@ -32,16 +32,12 @@ type nuisance struct { ID string `json:"id"` URI string `json:"uri"` } -type Locator struct { - Address types.Address `schema:"address"` - Location types.Location `schema:"location"` -} type nuisanceForm struct { + Address types.Address `schema:"address"` AdditionalInfo string `schema:"additional-info"` ClientID uuid.UUID `schema:"client_id" json:"client_id"` Duration string `schema:"duration"` Location types.Location `schema:"location"` - Locator Locator `schema:"locator"` MapZoom string `schema:"map-zoom"` SourceStagnant bool `schema:"source-stagnant"` SourceContainer bool `schema:"source-container"` @@ -56,7 +52,10 @@ type nuisanceForm struct { func (res *nuisanceR) Create(ctx context.Context, r *http.Request, n nuisanceForm) (*nuisance, *nhttp.ErrorWithStatus) { user_agent := r.Header.Get("User-Agent") - platform.EnsureClient(ctx, n.ClientID, user_agent) + err := platform.EnsureClient(ctx, n.ClientID, user_agent) + if err != nil { + return nil, nhttp.NewError("Failed to ensure client: %w", err) + } duration := enums.PublicreportNuisancedurationtypeNone is_location_frontyard := slices.Contains(n.SourceLocations, "frontyard") is_location_backyard := slices.Contains(n.SourceLocations, "backyard") @@ -64,7 +63,7 @@ func (res *nuisanceR) Create(ctx context.Context, r *http.Request, n nuisanceFor is_location_pool := slices.Contains(n.SourceLocations, "pool-area") is_location_other := slices.Contains(n.SourceLocations, "other") - err := duration.Scan(n.Duration) + err = duration.Scan(n.Duration) if err != nil { log.Warn().Err(err).Str("duration_str", n.Duration).Msg("Failed to interpret 'duration'") } @@ -74,19 +73,14 @@ func (res *nuisanceR) Create(ctx context.Context, r *http.Request, n nuisanceFor if err != nil { return nil, nhttp.NewError("Failed to extract image uploads: %w", err) } - address := platform.Address{ - GID: n.Locator.Address.GID, - Raw: n.Locator.Address.Raw, - } accuracy := float32(0.0) if n.Location.Accuracy != nil { accuracy = *n.Location.Accuracy } - log.Info().Str("address.raw", address.Raw).Str("address.gid", address.GID).Msg("making nuisance") setter_report := models.PublicreportReportSetter{ //AddressID: omitnull.From(latlng.Cell.String()), - AddressGid: omit.From(address.GID), - AddressRaw: omit.From(address.Raw), + AddressGid: omit.From(n.Address.GID), + AddressRaw: omit.From(n.Address.Raw), ClientUUID: omitnull.From(n.ClientID), Created: omit.From(time.Now()), //H3cell: omitnull.From(latlng.Cell.String()), @@ -97,11 +91,12 @@ func (res *nuisanceR) Create(ctx context.Context, r *http.Request, n nuisanceFor MapZoom: omit.From(float32(0.0)), //OrganizationID: omitnull.FromPtr(organization_id), //PublicID: omit.From(public_id), - ReporterEmail: omit.From(""), - ReporterName: omit.From(""), - ReporterPhone: omit.From(""), - ReportType: omit.From(enums.PublicreportReporttypeNuisance), - Status: omit.From(enums.PublicreportReportstatustypeReported), + ReporterEmail: omit.From(""), + ReporterName: omit.From(""), + ReporterPhone: omit.From(""), + ReporterPhoneCanSMS: omit.From(true), + ReportType: omit.From(enums.PublicreportReporttypeNuisance), + Status: omit.From(enums.PublicreportReportstatustypeReported), } setter_nuisance := models.PublicreportNuisanceSetter{ AdditionalInfo: omit.From(n.AdditionalInfo), @@ -121,7 +116,7 @@ func (res *nuisanceR) Create(ctx context.Context, r *http.Request, n nuisanceFor TodEvening: omit.From(n.TODEvening), TodNight: omit.From(n.TODNight), } - report, err := platform.PublicReportNuisanceCreate(ctx, setter_report, setter_nuisance, n.Location, address, uploads) + report, err := platform.PublicReportNuisanceCreate(ctx, setter_report, setter_nuisance, n.Location, n.Address, uploads) if err != nil { return nil, nhttp.NewError("create nuisance report: %w", err) } diff --git a/resource/publicreport_water.go b/resource/publicreport_water.go index 3c890dae..e31a9c7b 100644 --- a/resource/publicreport_water.go +++ b/resource/publicreport_water.go @@ -38,7 +38,7 @@ type waterForm struct { AccessGate bool `schema:"access-gate"` AccessLocked bool `schema:"access-locked"` AccessOther bool `schema:"access-other"` - Address string `schema:"address"` + Address types.Address `schema:"address"` AddressGID string `schema:"address-gid"` ClientID uuid.UUID `schema:"client_id" json:"client_id"` Comments string `schema:"comments"` @@ -49,7 +49,6 @@ type waterForm struct { IsReporterConfidential bool `schema:"reporter-confidential"` IsReporter_owner bool `schema:"property-ownership"` Location types.Location `schema:"location"` - Locator Locator `schema:"locator"` OwnerEmail string `schema:"owner-email"` OwnerName string `schema:"owner-name"` OwnerPhone string `schema:"owner-phone"` @@ -57,7 +56,10 @@ type waterForm struct { func (res *waterR) Create(ctx context.Context, r *http.Request, w waterForm) (*water, *nhttp.ErrorWithStatus) { user_agent := r.Header.Get("User-Agent") - platform.EnsureClient(ctx, w.ClientID, user_agent) + err := platform.EnsureClient(ctx, w.ClientID, user_agent) + if err != nil { + return nil, nhttp.NewError("Failed to ensure client: %w", err) + } uploads, err := html.ExtractImageUploads(r) log.Info().Int("len", len(uploads)).Msg("extracted water uploads") @@ -65,17 +67,13 @@ func (res *waterR) Create(ctx context.Context, r *http.Request, w waterForm) (*w return nil, nhttp.NewError("Failed to extract image uploads: %w", err) } - address := platform.Address{ - GID: w.AddressGID, - Raw: w.Address, - } accuracy := float32(0.0) if w.Location.Accuracy != nil { accuracy = *w.Location.Accuracy } setter_report := models.PublicreportReportSetter{ - AddressGid: omit.From(address.GID), - AddressRaw: omit.From(address.Raw), + AddressGid: omit.From(w.Address.GID), + AddressRaw: omit.From(w.Address.Raw), ClientUUID: omitnull.From(w.ClientID), Created: omit.From(time.Now()), //H3cell: omitnull.From(geospatial.Cell.String()), @@ -86,11 +84,12 @@ func (res *waterR) Create(ctx context.Context, r *http.Request, w waterForm) (*w MapZoom: omit.From(float32(0.0)), //OrganizationID: omitnull.FromPtr(organization_id), //PublicID: omit.From(public_id), - ReporterEmail: omit.From(""), - ReporterName: omit.From(""), - ReporterPhone: omit.From(""), - ReportType: omit.From(enums.PublicreportReporttypeWater), - Status: omit.From(enums.PublicreportReportstatustypeReported), + ReporterEmail: omit.From(""), + ReporterName: omit.From(""), + ReporterPhone: omit.From(""), + ReporterPhoneCanSMS: omit.From(true), + ReportType: omit.From(enums.PublicreportReporttypeWater), + Status: omit.From(enums.PublicreportReportstatustypeReported), } setter_water := models.PublicreportWaterSetter{ AccessComments: omit.From(w.AccessComments), @@ -111,7 +110,7 @@ func (res *waterR) Create(ctx context.Context, r *http.Request, w waterForm) (*w OwnerPhone: omit.From(w.OwnerPhone), //ReportID omit.Val[int32] } - report, err := platform.PublicReportWaterCreate(ctx, setter_report, setter_water, w.Location, address, uploads) + report, err := platform.PublicReportWaterCreate(ctx, setter_report, setter_water, w.Location, w.Address, uploads) if err != nil { return nil, nhttp.NewError("Failed to save new report: %w", err) } diff --git a/ts/rmo/view/ReportSubmitted.vue b/ts/rmo/view/ReportSubmitted.vue index 4167b2df..7bf2c03f 100644 --- a/ts/rmo/view/ReportSubmitted.vue +++ b/ts/rmo/view/ReportSubmitted.vue @@ -125,6 +125,24 @@ +