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 }