nidus-sync/platform/publicreport_notification.go
Eli Ribble f1fe8b4d2b
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.
2026-05-15 16:58:28 +00:00

77 lines
2.1 KiB
Go

package platform
import (
"context"
"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/platform/publicreport"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
//"github.com/rs/zerolog/log"
)
type PublicreportNotification struct {
Consent bool
Email string
Name string
Notification bool
Phone *types.E164
ReportID string
Subscription bool
}
func PublicreportNotificationCreate(ctx context.Context, pn PublicreportNotification) error {
txn, err := db.BeginTxn(ctx)
if err != nil {
return fmt.Errorf("begin txn: %w", err)
}
defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback")
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)
}
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 = publicreport.RegisterSubscriptionEmail(ctx, txn, contact, pn.Email)
if err != nil {
return fmt.Errorf("register subscription email: %w", err)
}
}
if pn.Notification {
err = publicreport.RegisterNotificationEmail(ctx, txn, *report, contact, pn.Email)
if err != nil {
return fmt.Errorf("register notification email: %w", err)
}
}
}
if pn.Phone != nil {
if pn.Subscription {
err = publicreport.RegisterSubscriptionPhone(ctx, txn, contact, *pn.Phone)
if err != nil {
return fmt.Errorf("register subscription phone: %w", err)
}
}
if pn.Notification {
err = publicreport.RegisterNotificationPhone(ctx, txn, *report, contact, *pn.Phone)
if err != nil {
return fmt.Errorf("register notification phone: %w", err)
}
}
}
if err := txn.Commit(ctx); err != nil {
return fmt.Errorf("commit: %w", err)
}
PublicReportReporterUpdated(ctx, report.OrganizationID, pn.ReportID)
return nil
}