nidus-sync/platform/publicreport_notification.go
Eli Ribble 7270de2937 lint: fix remaining errcheck issues across multiple files
- Fix renderShim errcheck in district.go, image.go
- Fix txn.Rollback/Commit in publicreport.go, notification, review, signal, upload
- Fix addError calls in csv/flyover.go, csv/pool.go
- Fix cW/write calls in logger.go, recoverer.go, voipms.go
- Fix resendInitialText, handleWaitingTextJobs, setPhoneStatus in text/send.go, text.go
- Fix populateDistrictURI/populateReportURI in resource files
2026-05-09 03:06:56 +00:00

75 lines
2.1 KiB
Go

package platform
import (
"context"
"fmt"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/lint"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/report"
"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.PGInstance.BobDB.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin txn: %w", err)
}
defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback")
rep, err := models.PublicreportReports.Query(
models.SelectWhere.PublicreportReports.PublicID.EQ(pn.ReportID),
).One(ctx, db.PGInstance.BobDB)
if err != nil {
return fmt.Errorf("find report '%s': %w", pn.ReportID, err)
}
err = report.SaveReporter(ctx, txn, pn.ReportID, pn.Name, pn.Email, pn.Phone, pn.Consent)
if err != nil {
return fmt.Errorf("save reporter: %w", err)
}
if pn.Email != "" {
if pn.Subscription {
err = report.RegisterSubscriptionEmail(ctx, txn, pn.Email)
if err != nil {
return fmt.Errorf("register subscription email: %w", err)
}
}
if pn.Notification {
err = report.RegisterNotificationEmail(ctx, txn, pn.ReportID, pn.Email)
if err != nil {
return fmt.Errorf("register notification email: %w", err)
}
}
}
if pn.Phone != nil {
if pn.Subscription {
err = report.RegisterSubscriptionPhone(ctx, txn, *pn.Phone)
if err != nil {
return fmt.Errorf("register subscription phone: %w", err)
}
}
if pn.Notification {
err = report.RegisterNotificationPhone(ctx, txn, pn.ReportID, *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, rep.OrganizationID, pn.ReportID)
return nil
}