fix race condition by setting email id (#188)
This commit is contained in:
@@ -123,6 +123,7 @@ export async function sendRawEmail({
|
||||
unsubUrl,
|
||||
isBulk,
|
||||
inReplyToMessageId,
|
||||
emailId,
|
||||
}: Partial<EmailContent> & {
|
||||
region: string;
|
||||
configurationSetName: string;
|
||||
@@ -134,6 +135,7 @@ export async function sendRawEmail({
|
||||
unsubUrl?: string;
|
||||
isBulk?: boolean;
|
||||
inReplyToMessageId?: string;
|
||||
emailId?: string;
|
||||
}) {
|
||||
const sesClient = getSesClient(region);
|
||||
|
||||
@@ -155,6 +157,7 @@ export async function sendRawEmail({
|
||||
bcc,
|
||||
headers: {
|
||||
"X-Entity-Ref-ID": nanoid(),
|
||||
...(emailId ? { "X-Unsend-Email-ID": emailId } : {}),
|
||||
...(unsubUrl
|
||||
? {
|
||||
"List-Unsubscribe": `<${unsubUrl}>`,
|
||||
|
@@ -381,6 +381,7 @@ async function executeEmail(job: QueueEmailJob) {
|
||||
unsubUrl,
|
||||
isBulk,
|
||||
inReplyToMessageId,
|
||||
emailId: email.id,
|
||||
});
|
||||
|
||||
logger.info(
|
||||
|
@@ -32,12 +32,39 @@ export async function parseSesHook(data: SesEvent) {
|
||||
|
||||
const mailData = getEmailData(data);
|
||||
|
||||
const email = await db.email.findUnique({
|
||||
let email = await db.email.findUnique({
|
||||
where: {
|
||||
sesEmailId,
|
||||
},
|
||||
});
|
||||
|
||||
// Handle race condition: If email not found by sesEmailId, try to find by custom header
|
||||
if (!email) {
|
||||
const emailIdHeader = data.mail.headers.find(
|
||||
(h) => h.name === "X-Unsend-Email-ID"
|
||||
);
|
||||
|
||||
if (emailIdHeader?.value) {
|
||||
email = await db.email.findUnique({
|
||||
where: {
|
||||
id: emailIdHeader.value,
|
||||
},
|
||||
});
|
||||
|
||||
// If found, update the sesEmailId to fix the missing reference
|
||||
if (email) {
|
||||
await db.email.update({
|
||||
where: { id: email.id },
|
||||
data: { sesEmailId },
|
||||
});
|
||||
logger.info(
|
||||
{ emailId: email.id, sesEmailId },
|
||||
"Updated email with sesEmailId from webhook (race condition resolved)"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.setBindings({
|
||||
sesEmailId,
|
||||
mailId: email?.id,
|
||||
|
Reference in New Issue
Block a user