fix race condition by setting email id (#188)
This commit is contained in:
@@ -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