Add email queue (#1)

* Add pgboss queue support

* Implement queue for sending emails

* Add migrations
This commit is contained in:
KM Koushik
2024-05-10 16:21:26 +10:00
committed by GitHub
parent 5931174889
commit 64c7613d8c
11 changed files with 329 additions and 71 deletions

View File

@@ -1,7 +1,7 @@
import { EmailContent } from "~/types";
import { db } from "../db";
import { sendEmailThroughSes, sendEmailWithAttachments } from "../aws/ses";
import { APP_SETTINGS } from "~/utils/constants";
import { UnsendApiError } from "~/server/public-api/api-error";
import { queueEmail } from "./job-service";
export async function sendEmail(
emailContent: EmailContent & { teamId: number }
@@ -15,72 +15,34 @@ export async function sendEmail(
});
if (!domain) {
throw new Error(
"Domain of from email is wrong. Use the email verified by unsend"
);
throw new UnsendApiError({
code: "BAD_REQUEST",
message:
"Domain of from email is wrong. Use the email verified by unsend",
});
}
if (domain.status !== "SUCCESS") {
throw new Error("Domain is not verified");
}
const messageId = attachments
? await sendEmailWithAttachments({
to,
from,
subject,
text,
html,
region: domain.region,
configurationSetName: getConfigurationSetName(
domain.clickTracking,
domain.openTracking
),
attachments,
})
: await sendEmailThroughSes({
to,
from,
subject,
text,
html,
region: domain.region,
configurationSetName: getConfigurationSetName(
domain.clickTracking,
domain.openTracking
),
attachments,
});
if (messageId) {
return await db.email.create({
data: {
to,
from,
subject,
text,
html,
sesEmailId: messageId,
teamId,
domainId: domain.id,
},
throw new UnsendApiError({
code: "BAD_REQUEST",
message: "Domain is not verified",
});
}
}
function getConfigurationSetName(
clickTracking: boolean,
openTracking: boolean
) {
if (clickTracking && openTracking) {
return APP_SETTINGS.SES_CONFIGURATION_FULL;
}
if (clickTracking) {
return APP_SETTINGS.SES_CONFIGURATION_CLICK_TRACKING;
}
if (openTracking) {
return APP_SETTINGS.SES_CONFIGURATION_OPEN_TRACKING;
}
const email = await db.email.create({
data: {
to,
from,
subject,
text,
html,
teamId,
domainId: domain.id,
attachments: attachments ? JSON.stringify(attachments) : undefined,
},
});
return APP_SETTINGS.SES_CONFIGURATION_GENERAL;
queueEmail(email.id);
return email;
}