Push update to public report event when reporter is saved

This commit is contained in:
Eli Ribble 2026-03-14 18:14:30 +00:00
parent 1075e35bca
commit 148454d392
No known key found for this signature in database
3 changed files with 69 additions and 17 deletions

View file

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

View file

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

View file

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