2020-04-10 00:43:15 +02:00
|
|
|
import _ from "lodash";
|
2022-08-23 20:08:53 +02:00
|
|
|
import moment, { Moment } from "moment";
|
|
|
|
import { db } from "../db/database";
|
2020-04-10 00:43:15 +02:00
|
|
|
import Logger from "../logger";
|
|
|
|
import * as MailTemplateService from "./mailTemplateService";
|
|
|
|
import * as Resources from "../utils/resources";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { RestParams } from "../utils/resources";
|
2022-07-18 17:49:42 +02:00
|
|
|
import {
|
2022-07-28 14:45:28 +02:00
|
|
|
EmailAddress,
|
|
|
|
isJSONObject,
|
|
|
|
isMailSortField,
|
|
|
|
isMailType,
|
2022-07-18 17:49:42 +02:00
|
|
|
Mail,
|
|
|
|
MailData,
|
|
|
|
MailId,
|
|
|
|
MailSortField,
|
|
|
|
MailType,
|
|
|
|
parseJSON,
|
2022-08-23 20:08:53 +02:00
|
|
|
UnixTimestampSeconds,
|
2022-07-18 17:49:42 +02:00
|
|
|
} from "../types";
|
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { send } from "../mail";
|
2022-07-18 17:49:42 +02:00
|
|
|
|
|
|
|
type EmaiQueueRow = {
|
2022-08-23 20:08:53 +02:00
|
|
|
id: MailId;
|
|
|
|
created_at: UnixTimestampSeconds;
|
|
|
|
data: string;
|
|
|
|
email: string;
|
|
|
|
failures: number;
|
|
|
|
modified_at: UnixTimestampSeconds;
|
|
|
|
recipient: EmailAddress;
|
|
|
|
sender: EmailAddress;
|
2022-07-18 17:49:42 +02:00
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const MAIL_QUEUE_DB_BATCH_SIZE = 50;
|
|
|
|
|
|
|
|
async function sendMail(options: Mail): Promise<void> {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("mail", "queue").info(
|
|
|
|
"Sending pending mail[%d] of type %s. " + "Had %d failures before.",
|
|
|
|
options.id,
|
|
|
|
options.email,
|
|
|
|
options.failures
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const renderedTemplate = await MailTemplateService.render(options);
|
|
|
|
|
|
|
|
const mailOptions = {
|
|
|
|
from: options.sender,
|
|
|
|
to: options.recipient,
|
|
|
|
subject: renderedTemplate.subject,
|
2022-08-23 20:08:53 +02:00
|
|
|
html: renderedTemplate.body,
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
|
2022-07-28 14:45:28 +02:00
|
|
|
await send(mailOptions);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("mail", "queue").info("Mail[%d] has been send.", options.id);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function findPendingMailsBefore(
|
|
|
|
beforeMoment: Moment,
|
|
|
|
limit: number
|
|
|
|
): Promise<Mail[]> {
|
2022-07-18 17:49:42 +02:00
|
|
|
const rows = await db.all<EmaiQueueRow>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"SELECT * FROM email_queue WHERE modified_at < ? AND failures < ? ORDER BY id ASC LIMIT ?",
|
|
|
|
[beforeMoment.unix(), 5, limit]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
return rows.map((row) => {
|
2022-07-18 17:49:42 +02:00
|
|
|
const mailType = row.email;
|
|
|
|
if (!isMailType(mailType)) {
|
|
|
|
throw new Error(`Invalid mailtype in database: ${mailType}`);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
2022-07-18 17:49:42 +02:00
|
|
|
const data = parseJSON(row.data);
|
|
|
|
if (!isJSONObject(data)) {
|
|
|
|
throw new Error(`Invalid email data in database: ${typeof data}`);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
email: mailType,
|
|
|
|
sender: row.sender,
|
|
|
|
recipient: row.recipient,
|
|
|
|
data,
|
|
|
|
failures: row.failures,
|
|
|
|
};
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removePendingMailFromQueue(id: MailId): Promise<void> {
|
2022-08-23 20:08:53 +02:00
|
|
|
await db.run("DELETE FROM email_queue WHERE id = ?", [id]);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function incrementFailureCounterForPendingEmail(
|
|
|
|
id: MailId
|
|
|
|
): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"UPDATE email_queue SET failures = failures + 1, modified_at = ? WHERE id = ?",
|
|
|
|
[moment().unix(), id]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendPendingMail(pendingMail: Mail): Promise<void> {
|
|
|
|
try {
|
|
|
|
await sendMail(pendingMail);
|
2022-07-18 17:49:42 +02:00
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +02:00
|
|
|
// we only log the error and increment the failure counter as we want to continue with pending mails
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("mail", "queue").error(
|
|
|
|
"Error sending pending mail[" + pendingMail.id + "]:",
|
|
|
|
error
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
await incrementFailureCounterForPendingEmail(pendingMail.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await removePendingMailFromQueue(pendingMail.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doGetMail(id: MailId): Promise<Mail> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const row = await db.get<Mail>("SELECT * FROM email_queue WHERE id = ?", [
|
|
|
|
id,
|
|
|
|
]);
|
2022-07-18 17:49:42 +02:00
|
|
|
if (row === undefined) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Mail not found.", type: ErrorTypes.notFound };
|
2022-07-18 17:49:42 +02:00
|
|
|
}
|
|
|
|
return row;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function enqueue(
|
|
|
|
sender: string,
|
|
|
|
recipient: string,
|
|
|
|
email: MailType,
|
|
|
|
data: MailData
|
|
|
|
): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
if (!_.isPlainObject(data)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error("Unexpected data: " + data);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"INSERT INTO email_queue " +
|
|
|
|
"(failures, sender, recipient, email, data) " +
|
|
|
|
"VALUES (?, ?, ?, ?, ?)",
|
|
|
|
[0, sender, recipient, email, JSON.stringify(data)]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export async function getMail(id: MailId): Promise<Mail> {
|
2020-04-10 00:43:15 +02:00
|
|
|
return await doGetMail(id);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function getPendingMails(
|
|
|
|
restParams: RestParams
|
|
|
|
): Promise<{ mails: Mail[]; total: number }> {
|
2022-07-18 17:49:42 +02:00
|
|
|
const row = await db.get<{ total: number }>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"SELECT count(*) AS total FROM email_queue",
|
|
|
|
[]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const total = row?.total || 0;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const filter = Resources.filterClause(
|
|
|
|
restParams,
|
2022-06-23 14:26:15 +02:00
|
|
|
MailSortField.ID,
|
|
|
|
isMailSortField,
|
2022-08-23 20:08:53 +02:00
|
|
|
["id", "failures", "sender", "recipient", "email"]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const mails = await db.all(
|
2022-08-23 20:08:53 +02:00
|
|
|
"SELECT * FROM email_queue WHERE " + filter.query,
|
|
|
|
filter.params
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
mails,
|
2022-08-23 20:08:53 +02:00
|
|
|
total,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export async function deleteMail(id: MailId): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
await removePendingMailFromQueue(id);
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export async function resetFailures(id: MailId): Promise<Mail> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const statement = await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"UPDATE email_queue SET failures = 0, modified_at = ? WHERE id = ?",
|
|
|
|
[moment().unix(), id]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!statement.changes) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error("Error: could not reset failure count for mail: " + id);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return await doGetMail(id);
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export async function sendPendingMails(): Promise<void> {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("mail", "queue").debug("Start sending pending mails...");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const startTime = moment();
|
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
let pendingMails = await findPendingMailsBefore(
|
|
|
|
startTime,
|
|
|
|
MAIL_QUEUE_DB_BATCH_SIZE
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
while (!_.isEmpty(pendingMails)) {
|
|
|
|
Logger.tag("mail", "queue").debug("Sending next batch...");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
for (const pendingMail of pendingMails) {
|
|
|
|
await sendPendingMail(pendingMail);
|
|
|
|
}
|
2022-08-23 21:38:37 +02:00
|
|
|
|
|
|
|
pendingMails = await findPendingMailsBefore(
|
|
|
|
startTime,
|
|
|
|
MAIL_QUEUE_DB_BATCH_SIZE
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
2022-08-23 21:38:37 +02:00
|
|
|
|
|
|
|
Logger.tag("mail", "queue").debug("Done sending pending mails.");
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|