Add contacts, rework comms schema

This in a pretty huge change. At a high level we're adding the concept
of a 'contact' which is a person or organization that has zero or more
contact methods (email, phone). This ended up cascading a number of
changes, including critically to the publicreprt schema. In the end it
seemed safer to get to the point where I'm confident we aren't using any
of the old fields for storing reporter information (though I haven't
deleted the columns yet) so I removed the code for defining those
columns.

At this point I think it's not possible for me to regenerate the bob
schema due to the interdependencies between my various schemas, so the
migration is well-and-truly happening.
This commit is contained in:
Eli Ribble 2026-05-15 16:58:28 +00:00
parent 085935fa66
commit f1fe8b4d2b
No known key found for this signature in database
46 changed files with 1127 additions and 633 deletions

View file

@ -5,9 +5,9 @@ import (
"fmt"
"github.com/Gleipnir-Technology/nidus-sync/db"
querypublicreport "github.com/Gleipnir-Technology/nidus-sync/db/query/publicreport"
"github.com/Gleipnir-Technology/nidus-sync/lint"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/report"
"github.com/Gleipnir-Technology/nidus-sync/platform/publicreport"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
//"github.com/rs/zerolog/log"
)
@ -23,31 +23,33 @@ type PublicreportNotification struct {
}
func PublicreportNotificationCreate(ctx context.Context, pn PublicreportNotification) error {
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
txn, err := db.BeginTxn(ctx)
if err != nil {
return fmt.Errorf("begin txn: %w", err)
}
defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback")
rep, err := models.PublicreportReports.Query(
models.SelectWhere.PublicreportReports.PublicID.EQ(pn.ReportID),
).One(ctx, db.PGInstance.BobDB)
report, err := querypublicreport.ReportFromPublicID(ctx, txn, pn.ReportID)
if err != nil {
return fmt.Errorf("find report '%s': %w", pn.ReportID, err)
}
if report == nil {
return fmt.Errorf("no such report '%s'", pn.ReportID)
}
err = report.SaveReporter(ctx, txn, pn.ReportID, pn.Name, pn.Email, pn.Phone, pn.Consent)
contact, err := publicreport.SaveReporter(ctx, txn, *report, pn.Name)
if err != nil {
return fmt.Errorf("save reporter: %w", err)
}
if pn.Email != "" {
if pn.Subscription {
err = report.RegisterSubscriptionEmail(ctx, txn, pn.Email)
err = publicreport.RegisterSubscriptionEmail(ctx, txn, contact, pn.Email)
if err != nil {
return fmt.Errorf("register subscription email: %w", err)
}
}
if pn.Notification {
err = report.RegisterNotificationEmail(ctx, txn, pn.ReportID, pn.Email)
err = publicreport.RegisterNotificationEmail(ctx, txn, *report, contact, pn.Email)
if err != nil {
return fmt.Errorf("register notification email: %w", err)
}
@ -55,13 +57,13 @@ func PublicreportNotificationCreate(ctx context.Context, pn PublicreportNotifica
}
if pn.Phone != nil {
if pn.Subscription {
err = report.RegisterSubscriptionPhone(ctx, txn, *pn.Phone)
err = publicreport.RegisterSubscriptionPhone(ctx, txn, contact, *pn.Phone)
if err != nil {
return fmt.Errorf("register subscription phone: %w", err)
}
}
if pn.Notification {
err = report.RegisterNotificationPhone(ctx, txn, pn.ReportID, *pn.Phone)
err = publicreport.RegisterNotificationPhone(ctx, txn, *report, contact, *pn.Phone)
if err != nil {
return fmt.Errorf("register notification phone: %w", err)
}
@ -70,6 +72,6 @@ func PublicreportNotificationCreate(ctx context.Context, pn PublicreportNotifica
if err := txn.Commit(ctx); err != nil {
return fmt.Errorf("commit: %w", err)
}
PublicReportReporterUpdated(ctx, rep.OrganizationID, pn.ReportID)
PublicReportReporterUpdated(ctx, report.OrganizationID, pn.ReportID)
return nil
}