@opengis/fastify-table
Version:
core-plugins
97 lines (96 loc) • 3.74 kB
JavaScript
import nodemailer from "nodemailer";
import config from "../../../../config.js";
import { handlebars } from "../../../helpers/index.js";
import pgClients from "../../pg/pgClients.js";
import getTemplate from "../../table/funcs/getTemplate.js";
import getRedis from "../../redis/funcs/getRedis.js";
import logger from "../../logger/getLogger.js";
const rclient = getRedis();
// eslint-disable-next-line max-len, no-control-regex
const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
async function generateNotificationContent({ pg, table, template, id, message, data: data1, }) {
if (template) {
const data = table && id && config.pg
? await pg
.query(`select * from ${table} where ${pg.pk[table]}=$1`, [id])
.then((el) => el.rows?.[0] || {})
: data1;
const body = await getTemplate("pt", template);
const html = handlebars.compile(body || template || "template not found")(data || {});
return html;
}
return message;
}
async function sendEmail({ to, from, subject, html, attachments }) {
if (!to?.length) {
throw new Error("empty to list");
}
const { mailSetting = {} } = config;
/*= == check service and setting === */
if (!mailSetting.service) {
logger.file("notification/warn", {
to,
from,
message: "service is not defined in config",
});
return null;
}
Object.assign(mailSetting, { rejectUnauthorized: false });
if (mailSetting.port === 465) {
Object.assign(mailSetting, { secure: true });
}
const transport = nodemailer.createTransport(mailSetting);
const result = await transport.sendMail({
from: from || mailSetting.from,
to,
subject,
html,
attachments,
});
return result;
}
export default async function notification({ pg = pgClients.client, provider = ["email"], from, to, template, table, message, title, data, id, nocache, }, unittest) {
if (pg?.readonly) {
return null;
}
const keyTo = `${pg?.options?.database}:mail:${provider[0]}:${to || ""}${id || ""}${table || ""}${title || ""}`;
const uniqueTo = config.redis ? await rclient.setnx(keyTo, 1) : true;
if (!uniqueTo && !nocache) {
logger.file("notification/sent", { keyTo, send: uniqueTo, nocache });
return `already sent: ${keyTo}`;
}
if (config.redis) {
await rclient.expire(keyTo, 1000 * 600);
}
if (!to.length) {
return null;
}
if (!(Array.isArray(provider) && provider.length)) {
return "notification provider - must be array and not empty";
}
const html = await generateNotificationContent({
pg,
table,
template,
id,
message,
data,
});
if (provider.includes("email")) {
const toEmail = Array.isArray(to)
? to.map((emails) => emails.match(emailReg)?.join(","))
: to;
const result = await sendEmail({
attachments: [],
html,
subject: title,
from,
to: unittest && config.mailSetting?.to ? config.mailSetting?.to : toEmail,
});
if (config.debug) {
console.log(result);
}
return result;
}
return null;
}