From 148454d39276f6428b3335878128eae4aa6758b0 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Sat, 14 Mar 2026 18:14:30 +0000 Subject: [PATCH] Push update to public report event when reporter is saved --- platform/event/event.go | 53 +++++++++++++++++++++++++++------------- platform/publicreport.go | 18 ++++++++++++++ rmo/notification.go | 15 ++++++++++++ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/platform/event/event.go b/platform/event/event.go index 66f27915..e703deb3 100644 --- a/platform/event/event.go +++ b/platform/event/event.go @@ -71,34 +71,53 @@ func EventTypeFromString(s string) EventType { type ResourceType int const ( - TypeRMONuisance = iota + TypeUnknown = iota + TypeRMONuisance TypeRMOWater ) -func Created(type_ ResourceType, organization_id int32, uri_id string) { - var resource string - var uri string - switch type_ { - case TypeRMONuisance: - resource = "rmo:nuisance" - uri = config.MakeURLReport("/report/%s", uri_id) - case TypeRMOWater: - resource = "rmo:water" - uri = config.MakeURLReport("/report/%s", uri_id) - default: - - } +func Created(t ResourceType, organization_id int32, uri_id string) { go Send(Envelope{ Event: Event{ - Resource: resource, + Resource: resourceString(t), Time: time.Now(), Type: EventTypeCreated, - URI: uri, + URI: makeURI(t, uri_id), + }, + OrganizationID: organization_id, + }) +} +func Updated(t ResourceType, organization_id int32, uri_id string) { + go Send(Envelope{ + Event: Event{ + Resource: resourceString(t), + Time: time.Now(), + Type: EventTypeUpdated, + URI: makeURI(t, uri_id), }, OrganizationID: organization_id, }) } func Send(env Envelope) { chanEvents <- env - +} +func resourceString(t ResourceType) string { + switch t { + case TypeRMONuisance: + return "rmo:nuisance" + case TypeRMOWater: + return "rmo:water" + default: + return "unknown" + } +} +func makeURI(t ResourceType, id string) string { + switch t { + case TypeRMONuisance: + return config.MakeURLReport("/report/%s", id) + case TypeRMOWater: + return config.MakeURLReport("/report/%s", id) + default: + return config.MakeURLReport("/unknown") + } } diff --git a/platform/publicreport.go b/platform/publicreport.go index 4dd53b22..74350dde 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -11,6 +11,7 @@ import ( "github.com/Gleipnir-Technology/nidus-sync/db" "github.com/Gleipnir-Technology/nidus-sync/db/enums" "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/Gleipnir-Technology/nidus-sync/platform/event" "github.com/rs/zerolog/log" ) @@ -36,5 +37,22 @@ func PublicreportInvalid(ctx context.Context, user User, report_id string) error } log.Info().Str("report-id", report_id).Str("tablename", tablename).Msg("Marked as invalid") + resource := resourceTypeFromTablename(tablename) + event.Updated(resource, user.Organization.ID(), report_id) return nil } + +func PublicReportReporterUpdated(ctx context.Context, org_id int32, report_id string, tablename string) { + resource := resourceTypeFromTablename(tablename) + event.Updated(resource, org_id, report_id) +} +func resourceTypeFromTablename(tablename string) event.ResourceType { + switch tablename { + case "nuisance": + return event.TypeRMONuisance + case "water": + return event.TypeRMOWater + default: + return event.TypeUnknown + } +} diff --git a/rmo/notification.go b/rmo/notification.go index d2cec9f8..6ce5ca1d 100644 --- a/rmo/notification.go +++ b/rmo/notification.go @@ -5,6 +5,8 @@ import ( "net/http" "github.com/Gleipnir-Technology/nidus-sync/db" + "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/Gleipnir-Technology/nidus-sync/platform" "github.com/Gleipnir-Technology/nidus-sync/platform/report" "github.com/Gleipnir-Technology/nidus-sync/platform/text" "github.com/rs/zerolog/log" @@ -41,6 +43,17 @@ func postRegisterNotifications(w http.ResponseWriter, r *http.Request) { return } defer txn.Rollback(ctx) + location, err := models.PublicreportReportLocations.Query( + models.SelectWhere.PublicreportReportLocations.PublicID.EQ(report_id), + ).One(ctx, db.PGInstance.BobDB) + if err != nil { + log.Error().Err(err).Msg("Failed to get report location") + http.Redirect(w, r, fmt.Sprintf("/error?code=report-location-failed&report=%s", report_id), http.StatusFound) + return + } + + tablename := location.TableName.MustGet() + org_id := location.OrganizationID.MustGet() e := report.SaveReporter(ctx, txn, report_id, name, email, phone, has_consent) if e != nil { log.Error().Err(e).Str("name", name).Msg("Failed to save reporter") @@ -80,5 +93,7 @@ func postRegisterNotifications(w http.ResponseWriter, r *http.Request) { } } txn.Commit(ctx) + platform.PublicReportReporterUpdated(ctx, org_id, report_id, tablename) + http.Redirect(w, r, fmt.Sprintf("/register-notifications-complete?report=%s", report_id), http.StatusFound) }