Skip to content

Commit

Permalink
feat: allow a null message if sg template exist
Browse files Browse the repository at this point in the history
  • Loading branch information
pr-Mais committed Dec 22, 2023
1 parent fa83910 commit 3ef66bb
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions firestore-send-email/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,26 @@ async function preparePayload(payload: QueuePayload): Promise<QueuePayload> {
return payload;
}

/**
* If the SMTP provider is SendGrid, we need to check if the payload contains
* either a text or html content, or if the payload contains a SendGrid Dynamic Template.
*
* Throws an error if all of the above are not provided.
*
* @param payload the payload from Firestore.
*/
function verifySendGridContent(payload: QueuePayload) {
if (
transport.transporter.name === "nodemailer-sendgrid" &&
!payload.message.text &&
!payload.message.html
!payload.message?.text &&
!payload.message?.html
) {
if (typeof payload.sendGridDynamicTemplate !== "object") {
throw new Error(
"`sendGridDynamicTemplate` must be a valid Firestore map."
);
}

if (!payload.sendGridDynamicTemplate?.templateId) {
logs.invalidSendGridTemplateId();
throw new Error(
Expand Down Expand Up @@ -291,7 +305,7 @@ async function deliver(
}

const result = await transport.sendMail({
...Object.assign(payload.message, {
...Object.assign(payload.message ?? {}, {
from: payload.from || config.defaultFrom,
replyTo: payload.replyTo || config.defaultReplyTo,
to: payload.to,
Expand Down Expand Up @@ -374,8 +388,13 @@ async function processWrite(
const payload = snapshot.data();

// We expect the payload to contain a message object describing the email
// to be sent. If it doesn't and is not a template, we can't do anything.
if (typeof payload.message !== "object" && !payload.template) {
// to be sent, or a template, or a SendGrid template.
// If it doesn't and is not a template, we can't do anything.
if (
typeof payload.message !== "object" &&
!payload.template &&
typeof payload.sendGridDynamicTemplate !== "object"
) {
logs.invalidMessage(payload.message);
return false;
}
Expand Down

0 comments on commit 3ef66bb

Please sign in to comment.