Fix email sending for report notification confirmation
The links in the email don't work, but it's a first step
This commit is contained in:
parent
7ee2f72b8e
commit
00a75a556e
9 changed files with 105 additions and 91 deletions
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
|
|||
85
comms/email/report_notification_confirmation.go
Normal file
85
comms/email/report_notification_confirmation.go
Normal file
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@
|
|||
<div class="content">
|
||||
<h1>Thank You for Your Report</h1>
|
||||
|
||||
<p>We've received your mosquito report. Thanks! We appreciate you taking the time to submit it.</p>
|
||||
<p>We've received your mosquito report {{.ReportIDStr}}. Thanks! We appreciate you taking the time to submit it.</p>
|
||||
|
||||
<p>You can check the current status of your report at any time by clicking the button below:</p>
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
Loading…
Add table
Add a link
Reference in a new issue