nidus-sync/platform/publicreport_notification.go
Eli Ribble 7237f5f666
Some checks failed
/ golint (push) Failing after 3m50s
Move internal references to new source hosting
2026-05-19 15:33:57 +00:00

77 lines
2.2 KiB
Go

package platform
import (
"context"
"fmt"
"source.gleipnir.technology/Gleipnir/nidus-sync/db"
querypublicreport "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/publicreport"
"source.gleipnir.technology/Gleipnir/nidus-sync/lint"
"source.gleipnir.technology/Gleipnir/nidus-sync/platform/publicreport"
"source.gleipnir.technology/Gleipnir/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
}