From a479cf343ade614ee332da05a63f959a4c85d8c2 Mon Sep 17 00:00:00 2001 From: valentinkolb Date: Fri, 25 Oct 2024 16:53:06 +0200 Subject: [PATCH] feat(emailApi): added sendTo Field --- emailApi/main.go | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/emailApi/main.go b/emailApi/main.go index ce61b27..5e78bde 100644 --- a/emailApi/main.go +++ b/emailApi/main.go @@ -13,10 +13,10 @@ import ( ) // sendEmailToUser sends an email notification to the user -func sendEmailToUser(app *pocketbase.PocketBase, registry *template.Registry, recipient *models.Record, sender *models.Record, emailRecord *models.Record) { +func sendEmailToUser(app *pocketbase.PocketBase, registry *template.Registry, recipient *models.Record, sender *models.Record, emailRecord *models.Record) error { // check if recipient and message are set and recipient is not the sender if recipient == nil || emailRecord == nil || sender == nil { - return + return fmt.Errorf("unable to send email to recipient: recipient, sender or email record is nil") } // sanitize email content @@ -43,10 +43,7 @@ func sendEmailToUser(app *pocketbase.PocketBase, registry *template.Registry, re "SUBJECT": subject, }) if err != nil { - currentErrors := emailRecord.GetString("errors") - emailRecord.Set("errors", fmt.Sprintf("%s\nThe email was not sent to the user '%s' due to an error.", currentErrors, recipient.Username())) - logger.LogErrorF("Error rendering email notification to recipient with username '%s': %v", recipient.GetString("username"), err) - return + return fmt.Errorf("error rendering email template for user with id '%s': %v", recipient.Id, err) } // set reply-to header @@ -65,10 +62,9 @@ func sendEmailToUser(app *pocketbase.PocketBase, registry *template.Registry, re Headers: headers, } if err := app.NewMailClient().Send(email); err != nil { - currentErrors := emailRecord.GetString("errors") - emailRecord.Set("errors", fmt.Sprintf("%s\nThe email was not sent to the user '%s' due to an error.", currentErrors, recipient.Username())) - logger.LogErrorF("Error sending email notification to recipient with username '%s': %v", recipient.GetString("username"), err) + return fmt.Errorf("error sending email to user with id '%s': %v", recipient.Id, err) } + return nil } func sendEmails(app *pocketbase.PocketBase, emailRecord *models.Record) { @@ -82,10 +78,34 @@ func sendEmails(app *pocketbase.PocketBase, emailRecord *models.Record) { return } + // get sender and recipients sender := emailRecord.ExpandedOne("sender") recipients := emailRecord.ExpandedAll("recipients") + + // this is the list of user ids that the email was sent to + sendToIds := make([]string, 0) + + // send email to each recipient for _, recipient := range recipients { - sendEmailToUser(app, registry, recipient, sender, emailRecord) + + // send email to user + err := sendEmailToUser(app, registry, recipient, sender, emailRecord) + + // check if there was an error sending the email + if err != nil { + logger.LogErrorF("%v", err) + } else { + sendToIds = append(sendToIds, recipient.Id) + } + } + + // set email record fields + emailRecord.Set("sentTo", sendToIds) + emailRecord.Set("wasSent", true) + + // save email record + if err := app.Dao().SaveRecord(emailRecord); err != nil { + logger.LogErrorF("Error saving email record: %v", err) } return