From d120ed05f2ec701dbc70486cbb469b5a1c65af01 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Thu, 21 May 2026 04:26:07 +0000 Subject: [PATCH] Emit communication updated events when reports change --- db/query/public/communication.go | 16 ++++++++++++++++ platform/publicreport.go | 13 ++++++++----- platform/publicreport/notification.go | 4 +++- platform/publicreport_notification.go | 10 +++++++++- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/db/query/public/communication.go b/db/query/public/communication.go index 019e12ab..8b1cd3e1 100644 --- a/db/query/public/communication.go +++ b/db/query/public/communication.go @@ -2,6 +2,8 @@ package public import ( "context" + "errors" + "fmt" "source.gleipnir.technology/Gleipnir/nidus-sync/db" //"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/public/enum" @@ -31,6 +33,20 @@ func CommunicationsFromOrganization(ctx context.Context, txn db.Ex, org_id int64 ORDER_BY(table.Communication.Created.DESC()) return db.ExecuteManyTx[model.Communication](ctx, txn, statement) } +func CommunicationFromReportID(ctx context.Context, txn db.Ex, report_id int64) (*model.Communication, error) { + statement := table.Communication.SELECT( + table.Communication.AllColumns, + ).FROM(table.Communication). + WHERE(table.Communication.SourceReportID.EQ(postgres.Int(report_id))) + row, err := db.ExecuteOneTx[model.Communication](ctx, txn, statement) + if err != nil { + if errors.Is(err, db.ErrNoRows) { + return nil, nil + } + return nil, fmt.Errorf("query communication from report %d: %w", report_id, err) + } + return &row, nil +} func CommunicationSetStatus(ctx context.Context, txn db.Ex, org_id int64, comm_id int64, status model.Communicationstatus) error { statement := table.Communication.UPDATE(). SET( diff --git a/platform/publicreport.go b/platform/publicreport.go index 36a23277..f4792f52 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -85,7 +85,8 @@ func PublicReportByIDWater(ctx context.Context, report_id string, is_public bool return &result, err } func PublicReportInvalid(ctx context.Context, user User, public_id string) error { - report, err := querypublicreport.ReportFromPublicID(ctx, db.PGInstance.PGXPool, public_id) + txn := db.PGInstance.PGXPool + report, err := querypublicreport.ReportFromPublicID(ctx, txn, public_id) if err != nil { return fmt.Errorf("query report existence: %w", err) } @@ -102,10 +103,15 @@ func PublicReportInvalid(ctx context.Context, user User, public_id string) error report_updater.Set(tablepublicreport.Report.ReviewerID) report_updater.Model.Status = modelpublicreport.Reportstatustype_Invalidated report_updater.Set(tablepublicreport.Report.Status) - err = report_updater.Execute(ctx, db.PGInstance.PGXPool, report.ID) + err = report_updater.Execute(ctx, txn, report.ID) log.Info().Int32("id", report.ID).Msg("Report marked as invalid") event.Updated(event.TypeRMOPublicReport, user.Organization.ID, public_id) + comm, err := querypublic.CommunicationFromReportID(ctx, txn, int64(report.ID)) + if err != nil { + return fmt.Errorf("communication from report ID %d: %w", report.ID, err) + } + event.Updated(event.TypeCommunication, user.Organization.ID, strconv.Itoa(int(comm.ID))) return nil } @@ -260,9 +266,6 @@ func PublicReportUpdateCompliance(ctx context.Context, public_id string, report_ } return nil } -func PublicReportReporterUpdated(ctx context.Context, org_id int32, report_id string) { - event.Updated(event.TypeRMOPublicReport, org_id, report_id) -} func PublicReportsForOrganization(ctx context.Context, org_id int32, is_public bool) ([]types.PublicReport, error) { return publicreport.UnreviewedForOrganization(ctx, db.PGInstance.PGXPool, int64(org_id), is_public) } diff --git a/platform/publicreport/notification.go b/platform/publicreport/notification.go index 4ab37d86..8a9f1661 100644 --- a/platform/publicreport/notification.go +++ b/platform/publicreport/notification.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + "github.com/rs/zerolog/log" "source.gleipnir.technology/Gleipnir/nidus-sync/db" modelcomms "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/model" modelpublic "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/public/model" @@ -15,7 +16,6 @@ import ( "source.gleipnir.technology/Gleipnir/nidus-sync/platform/email" "source.gleipnir.technology/Gleipnir/nidus-sync/platform/text" "source.gleipnir.technology/Gleipnir/nidus-sync/platform/types" - //"github.com/rs/zerolog/log" ) func DistrictForReport(ctx context.Context, report_id string) (modelpublic.Organization, error) { @@ -89,11 +89,13 @@ func SaveReporter(ctx context.Context, txn db.Ex, report modelpublicreport.Repor if err != nil { return contact, fmt.Errorf("contact insert: %w", err) } + log.Debug().Str("name", name).Int32("id", contact.ID).Msg("new contact inserted") } else { contact, err = querycomms.ContactFromID(ctx, txn, int64(*report.ReporterContactID)) if err != nil { return contact, fmt.Errorf("contact query: %w", err) } + log.Debug().Str("name", name).Str("old name", contact.Name).Int32("id", contact.ID).Msg("contact updated") if name != "" && contact.Name != name { err = querycomms.ContactUpdateName(ctx, txn, int64(contact.ID), name) if err != nil { diff --git a/platform/publicreport_notification.go b/platform/publicreport_notification.go index 668228e9..e2a4e0c0 100644 --- a/platform/publicreport_notification.go +++ b/platform/publicreport_notification.go @@ -3,10 +3,13 @@ package platform import ( "context" "fmt" + "strconv" "source.gleipnir.technology/Gleipnir/nidus-sync/db" + querypublic "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/public" querypublicreport "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/publicreport" "source.gleipnir.technology/Gleipnir/nidus-sync/lint" + "source.gleipnir.technology/Gleipnir/nidus-sync/platform/event" "source.gleipnir.technology/Gleipnir/nidus-sync/platform/publicreport" "source.gleipnir.technology/Gleipnir/nidus-sync/platform/types" //"github.com/rs/zerolog/log" @@ -72,6 +75,11 @@ func PublicreportNotificationCreate(ctx context.Context, pn PublicreportNotifica if err := txn.Commit(ctx); err != nil { return fmt.Errorf("commit: %w", err) } - PublicReportReporterUpdated(ctx, report.OrganizationID, pn.ReportID) + event.Updated(event.TypeRMOPublicReport, report.OrganizationID, pn.ReportID) + comm, err := querypublic.CommunicationFromReportID(ctx, db.PGInstance.PGXPool, int64(report.ID)) + if err != nil { + return fmt.Errorf("communication from report ID %d: %w", report.ID, err) + } + event.Updated(event.TypeCommunication, report.OrganizationID, strconv.Itoa(int(comm.ID))) return nil }