Add ses hook parser to capture all the events

This commit is contained in:
KMKoushik
2024-04-16 14:04:30 +10:00
parent 293277ed32
commit 9465960f0a
22 changed files with 712 additions and 105 deletions

View File

@@ -34,6 +34,7 @@ export const apiRouter = createTRPCRouter({
permission: true,
partialToken: true,
lastUsed: true,
createdAt: true,
},
});

View File

@@ -4,7 +4,7 @@ import { APP_SETTINGS } from "~/utils/constants";
import { createTopic, subscribeEndpoint } from "./sns";
import { env } from "~/env";
import { AppSettingsService } from "~/server/service/app-settings-service";
import { addWebhookConfiguration } from "../ses";
import { addWebhookConfiguration } from "./ses";
import { EventType } from "@aws-sdk/client-sesv2";
const GENERAL_EVENTS: EventType[] = [

View File

@@ -1,7 +1,7 @@
import dns from "dns";
import util from "util";
import * as tldts from "tldts";
import * as ses from "~/server/ses";
import * as ses from "~/server/aws/ses";
import { db } from "~/server/db";
const dnsResolveTxt = util.promisify(dns.resolveTxt);

View File

@@ -1,6 +1,6 @@
import { EmailContent } from "~/types";
import { db } from "../db";
import { sendEmailThroughSes } from "../ses";
import { sendEmailThroughSes } from "../aws/ses";
import { APP_SETTINGS } from "~/utils/constants";
export async function sendEmail(
@@ -45,7 +45,7 @@ export async function sendEmail(
subject,
text,
html,
id: messageId,
sesEmailId: messageId,
teamId,
domainId: domain.id,
},

View File

@@ -0,0 +1,91 @@
import { EmailStatus } from "@prisma/client";
import { SesEvent, SesEventDataKey, SesEventType } from "~/types/aws-types";
import { db } from "../db";
export async function parseSesHook(data: SesEvent) {
const mailStatus = getEmailStatus(data);
if (!mailStatus) {
console.error("Unknown email status", data);
return false;
}
const sesEmailId = data.mail.messageId;
const mailData = getEmailData(data);
const email = await db.email.findUnique({
where: {
sesEmailId,
},
});
if (!email) {
console.error("Email not found", data);
return false;
}
await db.email.update({
where: {
id: email.id,
},
data: {
latestStatus: mailStatus,
},
});
await db.emailEvent.upsert({
where: {
emailId_status: {
emailId: email.id,
status: mailStatus,
},
},
update: {
data: mailData as any,
},
create: {
emailId: email.id,
status: mailStatus,
data: mailData as any,
},
});
return true;
}
function getEmailStatus(data: SesEvent) {
const { eventType } = data;
if (eventType === "Send") {
return EmailStatus.SENT;
} else if (eventType === "Delivery") {
return EmailStatus.DELIVERED;
} else if (eventType === "Bounce") {
return EmailStatus.BOUNCED;
} else if (eventType === "Complaint") {
return EmailStatus.COMPLAINED;
} else if (eventType === "Reject") {
return EmailStatus.REJECTED;
} else if (eventType === "Open") {
return EmailStatus.OPENED;
} else if (eventType === "Click") {
return EmailStatus.CLICKED;
} else if (eventType === "Rendering Failure") {
return EmailStatus.RENDERING_FAILURE;
} else if (eventType === "DeliveryDelay") {
return EmailStatus.DELIVERY_DELAYED;
}
}
function getEmailData(data: SesEvent) {
const { eventType } = data;
if (eventType === "Rendering Failure") {
return data.renderingFailure;
} else if (eventType === "DeliveryDelay") {
return data.deliveryDelay;
} else {
return data[eventType.toLowerCase() as SesEventDataKey];
}
}