saleor-apps-redis_apl/apps/emails-and-messages/src/modules/mjml/send-mjml.ts
Lukasz Ostrowski 830cfe92ce
Extract logger (#439)
* Extract logger

* Replace logger with shared one

* Replace CRM logger with shared one

* Replace E&M logger with shared one

* Replace invoices logger with shared one

* Replace Products Feed logger with shared one

* Replace Search logger with shared one

* Replace Taxes logger with shared one

* Uninstall pino from apps direct dependency

* Update docs

* Update changeset

* Bumped Klaviyo typescript version to hopefully unblock the build

* Change packageManager field to pnpm 8.2.0

* removed package manager field from klaviyo package.json
2023-05-05 08:15:47 +02:00

159 lines
4.2 KiB
TypeScript

import { createLogger } from "@saleor/apps-shared";
import { compileMjml } from "./compile-mjml";
import { compileHandlebarsTemplate } from "./compile-handlebars-template";
import { sendEmailWithSmtp, SendMailArgs } from "./send-email-with-smtp";
import { MessageEventTypes } from "../event-handlers/message-event-types";
import { htmlToPlaintext } from "./html-to-plaintext";
import { MjmlConfiguration } from "./configuration/mjml-config";
interface SendMjmlArgs {
mjmlConfiguration: MjmlConfiguration;
recipientEmail: string;
event: MessageEventTypes;
payload: any;
}
export interface EmailServiceResponse {
errors?: {
code: number;
message: string;
}[];
}
export const sendMjml = async ({
payload,
recipientEmail,
event,
mjmlConfiguration,
}: SendMjmlArgs) => {
const logger = createLogger({
fn: "sendMjml",
event,
});
const eventSettings = mjmlConfiguration.events.find((e) => e.eventType === event);
if (!eventSettings) {
logger.debug("No active settings for this event, skipping");
return {
errors: [
{
message: "No active settings for this event",
},
],
};
}
if (!eventSettings.active) {
logger.debug("Event settings are not active, skipping");
return {
errors: [
{
message: "Event settings are not active",
},
],
};
}
logger.debug("Sending an email using MJML");
const { template: rawTemplate, subject } = eventSettings;
const { template: emailSubject, errors: handlebarsSubjectErrors } = compileHandlebarsTemplate(
subject,
payload
);
logger.warn(`email subject ${emailSubject} ${subject}`);
if (handlebarsSubjectErrors?.length) {
logger.error("Error during the handlebars subject template compilation");
return {
errors: [{ message: "Error during the handlebars subject template compilation" }],
};
}
if (!emailSubject || !emailSubject?.length) {
logger.error("Mjml subject message is empty, skipping");
return {
errors: [{ message: "Mjml subject message is empty, skipping" }],
};
}
const { template: mjmlTemplate, errors: handlebarsErrors } = compileHandlebarsTemplate(
rawTemplate,
payload
);
if (handlebarsErrors?.length) {
logger.error("Error during the handlebars template compilation");
return {
errors: [{ message: "Error during the handlebars template compilation" }],
};
}
if (!mjmlTemplate || !mjmlTemplate?.length) {
logger.error("Mjml template message is empty, skipping");
return {
errors: [{ message: "Mjml template message is empty, skipping" }],
};
}
const { html: emailBodyHtml, errors: mjmlCompilationErrors } = compileMjml(mjmlTemplate);
if (mjmlCompilationErrors.length) {
logger.error("Error during the MJML compilation");
logger.error(mjmlCompilationErrors);
return {
errors: [
{
message: "Error during the MJML compilation. Please Validate your MJML template",
},
],
};
}
if (!emailBodyHtml || !emailBodyHtml?.length) {
logger.error("No MJML template returned after the compilation");
return {
errors: [{ message: "No MJML template returned after the compilation" }],
};
}
const { plaintext: emailBodyPlaintext } = htmlToPlaintext(emailBodyHtml);
if (!emailBodyPlaintext || !emailBodyPlaintext?.length) {
logger.error("Email body could not be converted to plaintext");
return {
errors: [{ message: "Email body could not be converted to plaintext" }],
};
}
const sendEmailSettings: SendMailArgs = {
mailData: {
text: emailBodyPlaintext,
html: emailBodyHtml,
from: `${mjmlConfiguration.senderName} <${mjmlConfiguration.senderEmail}>`,
to: recipientEmail,
subject: emailSubject,
},
smtpSettings: {
host: mjmlConfiguration.smtpHost,
port: parseInt(mjmlConfiguration.smtpPort, 10),
},
};
if (mjmlConfiguration.smtpUser) {
sendEmailSettings.smtpSettings.auth = {
user: mjmlConfiguration.smtpUser,
pass: mjmlConfiguration.smtpPassword,
};
}
const { response, errors: smtpErrors } = await sendEmailWithSmtp(sendEmailSettings);
if (smtpErrors?.length) {
return { errors: smtpErrors };
}
logger.debug(response?.response);
};