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

@ -0,0 +1,105 @@
package publicreport
import (
"context"
"fmt"
"time"
"github.com/Gleipnir-Technology/nidus-sync/db"
modelcomms "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model"
modelpublic "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
modelpublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
querycomms "github.com/Gleipnir-Technology/nidus-sync/db/query/comms"
querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public"
querypublicreport "github.com/Gleipnir-Technology/nidus-sync/db/query/publicreport"
"github.com/Gleipnir-Technology/nidus-sync/platform/email"
"github.com/Gleipnir-Technology/nidus-sync/platform/text"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
//"github.com/rs/zerolog/log"
)
func DistrictForReport(ctx context.Context, report_id string) (modelpublic.Organization, error) {
report, err := querypublicreport.ReportFromPublicID(ctx, db.PGInstance.PGXPool, report_id)
if err != nil {
return modelpublic.Organization{}, fmt.Errorf("Failed to find report %s: %w", report_id, err)
}
result, e := querypublic.OrganizationFromID(ctx, db.PGInstance.PGXPool, int64(report.OrganizationID))
if e != nil {
return modelpublic.Organization{}, fmt.Errorf("Failed to load organization %d: %w", report.OrganizationID, e)
}
return result, nil
}
func RegisterNotificationEmail(ctx context.Context, txn db.Ex, report modelpublicreport.Report, contact modelcomms.Contact, destination string) error {
err := email.EnsureInDB(ctx, txn, contact, destination)
if err != nil {
return fmt.Errorf("Failed to ensure phone is in DB: %w", err)
}
_, err = querypublicreport.NotifyEmailCreate(ctx, txn, report.ID, destination)
if err != nil {
return err
}
return email.SendReportConfirmation(ctx, txn, contact, destination, report.PublicID)
}
func RegisterNotificationPhone(ctx context.Context, txn db.Ex, report modelpublicreport.Report, contact modelcomms.Contact, phone types.E164) error {
err := text.EnsureInDB(ctx, txn, contact, phone)
if err != nil {
return fmt.Errorf("Failed to ensure phone is in DB: %w", err)
}
_, err = querypublicreport.NotifyPhoneCreate(ctx, txn, report.ID, phone.PhoneString())
if err != nil {
return err
}
return text.ReportSubscriptionConfirmationText(ctx, db.PGInstance.PGXPool, phone, report.PublicID)
}
func RegisterSubscriptionEmail(ctx context.Context, txn db.Ex, contact modelcomms.Contact, destination string) error {
_, err := querypublicreport.SubscribeEmailInsert(ctx, txn, modelpublicreport.SubscribeEmail{
Created: time.Now(),
Deleted: nil,
EmailAddress: destination,
})
if err != nil {
return fmt.Errorf("Failed to save new subscription email row: %w", err)
}
return nil
}
func RegisterSubscriptionPhone(ctx context.Context, txn db.Ex, contact modelcomms.Contact, phone types.E164) error {
_, err := querypublicreport.SubscribePhoneInsert(ctx, txn, modelpublicreport.SubscribePhone{
Created: time.Now(),
Deleted: nil,
PhoneE164: phone.PhoneString(),
})
if err != nil {
return fmt.Errorf("Failed to save new subscription phone row: %w", err)
}
return nil
}
func SaveReporter(ctx context.Context, txn db.Ex, report modelpublicreport.Report, name string) (contact modelcomms.Contact, err error) {
if report.ReporterContactID == nil {
contact = modelcomms.Contact{
Created: time.Now(),
Name: name,
OrganizationID: report.OrganizationID,
}
contact, err = querycomms.ContactInsert(ctx, txn, contact)
if err != nil {
return contact, fmt.Errorf("contact insert: %w", err)
}
} else {
contact, err = querycomms.ContactFromID(ctx, txn, int64(*report.ReporterContactID))
if err != nil {
return contact, fmt.Errorf("contact query: %w", err)
}
if name != "" && contact.Name != name {
err = querycomms.ContactUpdateName(ctx, txn, int64(contact.ID), name)
if err != nil {
return contact, fmt.Errorf("contact update name: %w", err)
}
}
}
return contact, nil
}

View file

@ -148,7 +148,7 @@ func reportQueryToRows(ctx context.Context, reports []modelpublicreport.Report,
Email: &row.ReporterEmail,
HasEmail: row.ReporterEmail != "",
HasPhone: row.ReporterPhone != "",
Name: &row.ReporterName,
Name: row.ReporterName,
Phone: &row.ReporterPhone,
},
Status: row.Status.String(),