UNPKG

@a11ywatch/core

Version:
118 lines 5.26 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.emailMessager = void 0; const html_1 = require("../../html"); const utils_1 = require("../utils"); const email_templates_1 = require("../email_templates"); const config_1 = require("../../config/config"); const verify_1 = require("./verify"); const updateLastAlertDate = (userId, userCollection) => __awaiter(void 0, void 0, void 0, function* () { try { yield userCollection.findOneAndUpdate({ id: userId }, { $set: { lastAlertDateStamp: new Date() } }); } catch (e) { console.error(e); } }); const sendMail = ({ userId, data, confirmedOnly = false, sendEmail, }) => __awaiter(void 0, void 0, void 0, function* () { const [findUser, userCollection] = yield (0, verify_1.verifyUserSend)({ userId, confirmedOnly: config_1.DEV ? false : confirmedOnly, sendEmail, }); if (findUser) { yield updateLastAlertDate(userId, userCollection); const { pageUrl, domain, issuesInfo } = data; const { errorCount, warningCount, totalIssues: tot, } = issuesInfo !== null && issuesInfo !== void 0 ? issuesInfo : { errorCount: 0, warningCount: 0, totalIssues: 0 }; const totalIssues = Number(errorCount); const totalWarnings = Number(warningCount); const total = Number(tot); const issuesTable = `${(0, email_templates_1.issuesResultsTemplate)({ total, totalIssues, totalWarnings, pageUrl, }, "h2", true, !(findUser === null || findUser === void 0 ? void 0 : findUser.role))}`; utils_1.transporter === null || utils_1.transporter === void 0 ? void 0 : utils_1.transporter.sendMail({ from: utils_1.mailOptions.from, to: findUser.email, text: utils_1.mailOptions.text, subject: `[Report] ${totalIssues} ${(0, utils_1.pluralize)(totalIssues, "error")} found with ${pageUrl || domain}.`, html: `${issuesTable}<br />${html_1.footer.marketing({ userId, email: findUser.email, })}`, }, utils_1.sendMailCallback); } }); const sendMailMultiPage = ({ userId, data, domain, sendEmail = true, }) => __awaiter(void 0, void 0, void 0, function* () { const [user, userCollection] = yield (0, verify_1.verifyUserSend)({ userId, confirmedOnly: true, sendEmail, }); if (user) { yield updateLastAlertDate(userId, userCollection); let total = 0; let totalWarnings = 0; let totalIssues = 0; let issuesTable = ""; let pageUrl = ""; for (const page of data) { const issuesInfo = page === null || page === void 0 ? void 0 : page.issuesInfo; if (!domain) { pageUrl = page.domain; } if (issuesInfo === null || issuesInfo === void 0 ? void 0 : issuesInfo.totalIssues) { totalIssues += issuesInfo.errorCount; totalWarnings += issuesInfo.warningCount; } } total = totalWarnings + totalIssues; issuesTable = `<br />${(0, email_templates_1.issuesResultsTemplate)({ totalIssues, totalWarnings, total, pageUrl, }, "h3", true, !(user === null || user === void 0 ? void 0 : user.role))}`; if (Number(totalIssues) >= 1) { utils_1.transporter.sendMail({ from: utils_1.mailOptions.from, text: utils_1.mailOptions.text, to: user.email, subject: `[Report] ${totalIssues} ${(0, utils_1.pluralize)(totalIssues, "issue")} found with ${domain}.`, html: ` <div style="margin-bottom: 12px; margin-top: 8px;">Login to see the full report.</div> ${issuesTable}<br />${html_1.footer.marketing({ userId, email: user.email, })}`, }, utils_1.sendMailCallback); } } }); exports.emailMessager = { sendFollowupEmail: ({ email, emailConfirmed, subject = "", html }) => { if (emailConfirmed && email && subject && html) { utils_1.transporter.sendMail({ html, from: utils_1.mailOptions.from, text: utils_1.mailOptions.text, to: email, subject: subject, }, utils_1.sendMailCallback); } }, sendMail, sendMailMultiPage, }; //# sourceMappingURL=email.js.map