diff --git a/background/email.go b/background/email.go index ccdd464c..352adabb 100644 --- a/background/email.go +++ b/background/email.go @@ -10,7 +10,7 @@ import ( var channelJobEmail chan email.Job func ReportSubscriptionConfirmationEmail(destination, report_id string) { - enqueueJobEmail(email.NewJobReportSubscriptionConfirmation( + enqueueJobEmail(email.NewJobReportNotificationConfirmation( destination, report_id, )) @@ -36,6 +36,7 @@ func startWorkerEmail(ctx context.Context, channel chan email.Job) { case job := <-channel: err := email.Handle(ctx, job) if err != nil { + log.Error().Err(err).Msg("Failed to handle email message") } } } diff --git a/comms/email/job.go b/comms/email/job.go index b2f3fec7..73f9f313 100644 --- a/comms/email/job.go +++ b/comms/email/job.go @@ -24,8 +24,11 @@ type jobEmailBase struct { func Handle(ctx context.Context, job Job) error { var err error + log.Debug().Str("dest", job.destination()).Str("type", string(job.messageType())).Msg("Handling email job") switch job.messageType() { case enums.CommsMessagetypeemailReportSubscriptionConfirmation: + return errors.New("ReportSubscription has been deprecated.") + case enums.CommsMessagetypeemailReportNotificationConfirmation: err = sendEmailReportConfirmation(ctx, job) default: return errors.New("not implemented") @@ -35,10 +38,4 @@ func Handle(ctx context.Context, job Job) error { return fmt.Errorf("Failed to handle email: %w", err) } return nil - /* - case enums.CommsMessagetypeemailReportStatusScheduled: - case enums.CommsMessagetypeemailReportStatusComplete: - - } - */ } diff --git a/comms/email/report_notification_confirmation.go b/comms/email/report_notification_confirmation.go new file mode 100644 index 00000000..efefd502 --- /dev/null +++ b/comms/email/report_notification_confirmation.go @@ -0,0 +1,85 @@ +package email + +import ( + "context" + "fmt" + + "github.com/Gleipnir-Technology/nidus-sync/config" + "github.com/Gleipnir-Technology/nidus-sync/db/enums" + "github.com/rs/zerolog/log" +) + +func NewJobReportNotificationConfirmation(destination, report_id string) Job { + return jobEmailReportNotificationConfirmation{ + dest: destination, + reportID: report_id, + } +} + +type jobEmailReportNotificationConfirmation struct { + dest string + reportID string +} + +func (job jobEmailReportNotificationConfirmation) destination() string { + return job.dest +} +func (job jobEmailReportNotificationConfirmation) messageType() enums.CommsMessagetypeemail { + return enums.CommsMessagetypeemailReportNotificationConfirmation +} +func (job jobEmailReportNotificationConfirmation) renderHTML() (string, error) { + _ = newContentEmailNotificationConfirmation(job) + return "", nil +} +func (job jobEmailReportNotificationConfirmation) renderTXT() (string, error) { + return "fake txt", nil +} +func (job jobEmailReportNotificationConfirmation) subject() string { + return "" +} + +func sendEmailReportConfirmation(ctx context.Context, job Job) error { + j, ok := job.(jobEmailReportNotificationConfirmation) + if !ok { + return fmt.Errorf("job is not for report subscription confirmation") + } + err := maybeSendInitialEmail(ctx, j.destination()) + if err != nil { + return fmt.Errorf("Failed to handle initial email: %w", err) + } + data := make(map[string]string, 0) + public_id := generatePublicId(enums.CommsMessagetypeemailInitialContact, data) + data["report_id"] = j.reportID + report_id_str := publicReportID(j.reportID) + data["ReportIDStr"] = report_id_str + data["URLLogo"] = config.MakeURLReport("/static/img/nidus-logo-no-lettering-64.png") + data["URLReportStatus"] = config.MakeURLReport("/foo") + data["URLReportUnsubscribe"] = config.MakeURLReport("/email/unsubscribe") + data["URLViewInBrowser"] = config.MakeURLReport("/email?id=%s", public_id) + text, html, err := renderEmailTemplates(templateReportNotificationConfirmationID, data) + if err != nil { + return fmt.Errorf("Failed to render email report notification template: %w", err) + } + subject := fmt.Sprintf("Mosquito Report Submission - %s", report_id_str) + err = insertEmailLog(ctx, data, j.destination(), public_id, config.ForwardEmailReportAddress, subject, templateReportNotificationConfirmationID) + if err != nil { + return fmt.Errorf("Failed to store email log: %w", err) + } + resp, err := sendEmail(ctx, emailRequest{ + From: config.ForwardEmailReportAddress, + HTML: html, + Subject: subject, + Text: text, + To: j.destination(), + }, enums.CommsMessagetypeemailReportNotificationConfirmation) + if err != nil { + return fmt.Errorf("Failed to send email report confirmation to %s for report %s: %w", j.dest, j.reportID, err) + } + log.Info().Str("id", resp.ID).Str("dest", j.dest).Str("report_id", j.reportID).Msg("Sent report confirmation email") + return nil +} + +func newContentEmailNotificationConfirmation(job jobEmailReportNotificationConfirmation) (result contentEmailReportConfirmation) { + result.URLReportStatus = config.MakeURLReport("/status/%s", job.reportID) + return result +} diff --git a/comms/email/report_subscription_confirmation.go b/comms/email/report_subscription_confirmation.go deleted file mode 100644 index c689859d..00000000 --- a/comms/email/report_subscription_confirmation.go +++ /dev/null @@ -1,80 +0,0 @@ -package email - -import ( - "context" - "fmt" - - "github.com/Gleipnir-Technology/nidus-sync/config" - "github.com/Gleipnir-Technology/nidus-sync/db/enums" - //"github.com/rs/zerolog/log" -) - -func NewJobReportSubscriptionConfirmation(destination, report_id string) Job { - return jobEmailReportSubscriptionConfirmation{ - dest: destination, - reportID: report_id, - } -} - -type jobEmailReportSubscriptionConfirmation struct { - dest string - reportID string -} - -func (job jobEmailReportSubscriptionConfirmation) destination() string { - return job.dest -} -func (job jobEmailReportSubscriptionConfirmation) messageType() enums.CommsMessagetypeemail { - return enums.CommsMessagetypeemailReportSubscriptionConfirmation -} -func (job jobEmailReportSubscriptionConfirmation) renderHTML() (string, error) { - _ = newContentEmailSubscriptionConfirmation(job) - return "", nil -} -func (job jobEmailReportSubscriptionConfirmation) renderTXT() (string, error) { - return "fake txt", nil -} -func (job jobEmailReportSubscriptionConfirmation) subject() string { - return "" -} - -func sendEmailReportConfirmation(ctx context.Context, job Job) error { - j, ok := job.(jobEmailReportSubscriptionConfirmation) - if !ok { - return fmt.Errorf("job is not for report subscription confirmation") - } - err := maybeSendInitialEmail(ctx, j.destination()) - if err != nil { - return fmt.Errorf("Failed to handle initial email: %w", err) - } - return nil - /* - report_id_str := publicReportID(report_id) - content := newContentEmailSubscriptionConfirmation(report_id) - text, html, err := renderEmailTemplates(reportConfirmationT, content) - if err != nil { - return fmt.Errorf("Failed to render template %s: %w", reportConfirmationT.name, err) - } - resp, err := sendEmail(ctx, emailRequest{ - From: config.ForwardEmailReportAddress, - HTML: html, - Subject: fmt.Sprintf("Mosquito Report Submission - %s", report_id_str), - Text: text, - To: to, - }, enums.CommsMessagetypeemailReportSubscriptionConfirmation) - if err != nil { - return fmt.Errorf("Failed to send email report confirmation to %s for report %s: %w", to, report_id, err) - } - log.Info().Str("id", resp.ID).Str("to", to).Str("report_id", report_id).Msg("Sent report confirmation email") - return nil - */ -} - -func newContentEmailSubscriptionConfirmation(job jobEmailReportSubscriptionConfirmation) (result contentEmailReportConfirmation) { - /*newContentBase( - &result.Base, - config.MakeURLReport("/email/report/%s/subscription-confirmation", job.reportID), - )*/ - result.URLReportStatus = config.MakeURLReport("/status/%s", job.reportID) - return result -} diff --git a/comms/email/template.go b/comms/email/template.go index adb6446f..a59bbfe4 100644 --- a/comms/email/template.go +++ b/comms/email/template.go @@ -31,8 +31,9 @@ import ( var embeddedFiles embed.FS var ( - templateByID map[int32]*builtTemplate - templateInitialID int32 + templateByID map[int32]*builtTemplate + templateInitialID int32 + templateReportNotificationConfirmationID int32 ) type templatePair struct { @@ -81,6 +82,10 @@ func LoadTemplates() error { if err != nil { return fmt.Errorf("Failed to load template ID: %s", err) } + templateReportNotificationConfirmationID, err = loadTemplateID(ctx, tx, enums.CommsMessagetypeemailReportNotificationConfirmation) + if err != nil { + return fmt.Errorf("Failed to load report-notification-confirmation template ID: %s", err) + } tx.Commit(ctx) return nil } diff --git a/comms/email/template/report-subscription-confirmation.html b/comms/email/template/report-notification-confirmation.html similarity index 95% rename from comms/email/template/report-subscription-confirmation.html rename to comms/email/template/report-notification-confirmation.html index 88ef306a..0eef1e71 100644 --- a/comms/email/template/report-subscription-confirmation.html +++ b/comms/email/template/report-notification-confirmation.html @@ -76,7 +76,7 @@

Thank You for Your Report

-

We've received your mosquito report. Thanks! We appreciate you taking the time to submit it.

+

We've received your mosquito report {{.ReportIDStr}}. Thanks! We appreciate you taking the time to submit it.

You can check the current status of your report at any time by clicking the button below:

diff --git a/comms/email/template/report-subscription-confirmation.txt b/comms/email/template/report-notification-confirmation.txt similarity index 100% rename from comms/email/template/report-subscription-confirmation.txt rename to comms/email/template/report-notification-confirmation.txt diff --git a/db/enums/enums.bob.go b/db/enums/enums.bob.go index 14d12cac..77800db7 100644 --- a/db/enums/enums.bob.go +++ b/db/enums/enums.bob.go @@ -199,6 +199,7 @@ const ( CommsMessagetypeemailReportSubscriptionConfirmation CommsMessagetypeemail = "report-subscription-confirmation" CommsMessagetypeemailReportStatusScheduled CommsMessagetypeemail = "report-status-scheduled" CommsMessagetypeemailReportStatusComplete CommsMessagetypeemail = "report-status-complete" + CommsMessagetypeemailReportNotificationConfirmation CommsMessagetypeemail = "report-notification-confirmation" ) func AllCommsMessagetypeemail() []CommsMessagetypeemail { @@ -207,6 +208,7 @@ func AllCommsMessagetypeemail() []CommsMessagetypeemail { CommsMessagetypeemailReportSubscriptionConfirmation, CommsMessagetypeemailReportStatusScheduled, CommsMessagetypeemailReportStatusComplete, + CommsMessagetypeemailReportNotificationConfirmation, } } @@ -221,7 +223,8 @@ func (e CommsMessagetypeemail) Valid() bool { case CommsMessagetypeemailInitialContact, CommsMessagetypeemailReportSubscriptionConfirmation, CommsMessagetypeemailReportStatusScheduled, - CommsMessagetypeemailReportStatusComplete: + CommsMessagetypeemailReportStatusComplete, + CommsMessagetypeemailReportNotificationConfirmation: return true default: return false diff --git a/db/migrations/00051_messagetypeemail_report_notification.sql b/db/migrations/00051_messagetypeemail_report_notification.sql new file mode 100644 index 00000000..fd85441b --- /dev/null +++ b/db/migrations/00051_messagetypeemail_report_notification.sql @@ -0,0 +1,3 @@ +-- +goose Up +ALTER TYPE comms.MessageTypeEmail ADD VALUE 'report-notification-confirmation' AFTER 'report-status-complete'; +UPDATE comms.email_log SET source = 'report-notification-confirmation' WHERE source = 'report-subscription-confirmation';