nidus-sync/platform/publicreport/notification.go

108 lines
4.2 KiB
Go
Raw Normal View History

package publicreport
import (
"context"
"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"
modelpublicreport "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/publicreport/model"
querycomms "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/comms"
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/platform/email"
"source.gleipnir.technology/Gleipnir/nidus-sync/platform/text"
"source.gleipnir.technology/Gleipnir/nidus-sync/platform/types"
)
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)
}
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 {
return contact, fmt.Errorf("contact update name: %w", err)
}
}
}
return contact, nil
}