Emit communication updated events when reports change
Some checks failed
/ golint (push) Failing after 10s

This commit is contained in:
Eli Ribble 2026-05-21 04:26:07 +00:00
parent 594bf33b0a
commit d120ed05f2
No known key found for this signature in database
4 changed files with 36 additions and 7 deletions

View file

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

View file

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

View file

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

View file

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