Add email queue (#1)
* Add pgboss queue support * Implement queue for sending emails * Add migrations
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user