From 46b2b648474a1fe889ccaff026427f71a4503317 Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Sun, 10 Aug 2025 08:58:17 +1000 Subject: [PATCH] add teamId to email events (#194) --- .../migration.sql | 14 ++++++++++++++ apps/web/prisma/schema.prisma | 6 +++++- apps/web/src/server/service/campaign-service.ts | 1 + apps/web/src/server/service/email-queue-service.ts | 1 + apps/web/src/server/service/email-service.ts | 5 +++++ apps/web/src/server/service/ses-hook-parser.ts | 1 + 6 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 apps/web/prisma/migrations/20250809224636_add_team_id_to_email_events/migration.sql diff --git a/apps/web/prisma/migrations/20250809224636_add_team_id_to_email_events/migration.sql b/apps/web/prisma/migrations/20250809224636_add_team_id_to_email_events/migration.sql new file mode 100644 index 0000000..1ee92e6 --- /dev/null +++ b/apps/web/prisma/migrations/20250809224636_add_team_id_to_email_events/migration.sql @@ -0,0 +1,14 @@ +/* + Warnings: + + - Added the required column `teamId` to the `EmailEvent` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "EmailEvent" ADD COLUMN "teamId" INTEGER NOT NULL; + +-- CreateIndex +CREATE INDEX "EmailEvent_teamId_idx" ON "EmailEvent"("teamId"); + +-- AddForeignKey +ALTER TABLE "EmailEvent" ADD CONSTRAINT "EmailEvent_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma index 14d09b1..17061be 100644 --- a/apps/web/prisma/schema.prisma +++ b/apps/web/prisma/schema.prisma @@ -117,7 +117,8 @@ model Team { dailyEmailUsages DailyEmailUsage[] subscription Subscription[] invites TeamInvite[] - SuppressionList SuppressionList[] + suppressionList SuppressionList[] + emailEvents EmailEvent[] } model TeamInvite { @@ -261,10 +262,13 @@ model EmailEvent { emailId String status EmailStatus data Json? + teamId Int createdAt DateTime @default(now()) email Email @relation(fields: [emailId], references: [id], onDelete: Cascade) + team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) @@index([emailId]) + @@index([teamId]) } model ContactBook { diff --git a/apps/web/src/server/service/campaign-service.ts b/apps/web/src/server/service/campaign-service.ts index fa6a397..52c7962 100644 --- a/apps/web/src/server/service/campaign-service.ts +++ b/apps/web/src/server/service/campaign-service.ts @@ -331,6 +331,7 @@ async function processContactEmail(jobData: CampaignEmailJob) { data: { error: "Contact email is suppressed. No email sent.", }, + teamId: emailConfig.teamId, }, }); diff --git a/apps/web/src/server/service/email-queue-service.ts b/apps/web/src/server/service/email-queue-service.ts index 9886486..1de53cd 100644 --- a/apps/web/src/server/service/email-queue-service.ts +++ b/apps/web/src/server/service/email-queue-service.ts @@ -403,6 +403,7 @@ async function executeEmail(job: QueueEmailJob) { data: { error: error.toString(), }, + teamId: email.teamId, }, }); await db.email.update({ diff --git a/apps/web/src/server/service/email-service.ts b/apps/web/src/server/service/email-service.ts index 7c048d3..8c2b178 100644 --- a/apps/web/src/server/service/email-service.ts +++ b/apps/web/src/server/service/email-service.ts @@ -132,6 +132,7 @@ export async function sendEmail( data: { error: "All TO recipients are suppressed. No emails to send.", }, + teamId, }, }); @@ -260,6 +261,7 @@ export async function sendEmail( data: { error: error.toString(), }, + teamId, }, }); await db.email.update({ @@ -327,6 +329,7 @@ export async function cancelEmail(emailId: string) { data: { emailId, status: "CANCELLED", + teamId: email.teamId, }, }); } @@ -543,6 +546,7 @@ export async function sendBulkEmails( data: { error: "All TO recipients are suppressed. No emails to send.", }, + teamId, }, }); @@ -733,6 +737,7 @@ export async function sendBulkEmails( data: { error: error.toString(), }, + teamId: email.email.teamId, }, }); await db.email.update({ diff --git a/apps/web/src/server/service/ses-hook-parser.ts b/apps/web/src/server/service/ses-hook-parser.ts index 07fcfb3..cbcd2ce 100644 --- a/apps/web/src/server/service/ses-hook-parser.ts +++ b/apps/web/src/server/service/ses-hook-parser.ts @@ -249,6 +249,7 @@ export async function parseSesHook(data: SesEvent) { emailId: email.id, status: mailStatus, data: mailData as any, + teamId: email.teamId, }, });