The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
run first round of migrations + add unique index on emailId
Steven Teya0ac9b3e5770
diff --git a/apps/web/app/api/resend/webhook/email-opened.ts b/apps/web/app/api/resend/webhook/email-opened.tsindex 3a7824b5fc..c449ae8f5d 100644--- a/apps/web/app/api/resend/webhook/email-opened.ts+++ b/apps/web/app/api/resend/webhook/email-opened.ts@@ -18,7 +18,7 @@ export async function emailOpened({const res = await prisma.$transaction(async (tx) => {// TODO: refactor this to use findUnique once we add `emailId` as a unique index- await tx.notificationEmail.updateMany({+ const notificationEmail = await tx.notificationEmail.update({where: {emailId,openedAt: null,@@ -27,22 +27,13 @@ export async function emailOpened({openedAt: new Date(),},});- const notificationEmail = await tx.notificationEmail.findFirst({- where: {- emailId,- },- });console.log(- `Found notification email: ${JSON.stringify(notificationEmail)}`,+ `Updated notification email ${notificationEmail.id} with Resend email id ${emailId} to opened at ${new Date()}`,);- if (- !notificationEmail ||- !notificationEmail.programId ||- !notificationEmail.partnerId- ) {- return;+ if (!notificationEmail.programId || !notificationEmail.partnerId) {+ return notificationEmail;}return await tx.message.updateMany({diff --git a/apps/web/scripts/migrations/remove-duplicate-notification-emails.ts b/apps/web/scripts/migrations/remove-duplicate-notification-emails.tsnew file mode 100644index 0000000000..20318d62cf--- /dev/null+++ b/apps/web/scripts/migrations/remove-duplicate-notification-emails.ts@@ -0,0 +1,67 @@+import { prisma } from "@dub/prisma";+import "dotenv-flow/config";++// one time script to make sure notificationEmail entries are unique by emailId+async function main() {+ const emails = await prisma.notificationEmail.groupBy({+ by: ["emailId"],+ _count: {+ emailId: true,+ },+ orderBy: {+ _count: {+ emailId: "desc",+ },+ },+ });++ const emailsWithDuplicates = emails.filter(+ (email) => email._count.emailId > 1,+ );++ if (emailsWithDuplicates.length === 0) {+ console.log("No more duplicate emails found");+ return;+ }++ console.log(`Found ${emailsWithDuplicates.length} emails with duplicates`);++ const emailIdsToDelete: string[] = [];+ const notificationEmailIdsToPreserve: string[] = [];++ for (const e of emailsWithDuplicates.slice(0, 100)) {+ const finalEmail = await prisma.notificationEmail.findFirst({+ where: {+ emailId: e.emailId,+ },+ orderBy: {+ id: "desc",+ },+ });+ if (!finalEmail) {+ console.log(`No final email found for ${e.emailId}`);+ continue;+ }+ emailIdsToDelete.push(e.emailId);+ notificationEmailIdsToPreserve.push(finalEmail.id);+ }++ console.log({+ emailIdsToDelete,+ notificationEmailIdsToPreserve,+ });++ const res = await prisma.notificationEmail.deleteMany({+ where: {+ emailId: {+ in: emailIdsToDelete,+ },+ id: {+ notIn: notificationEmailIdsToPreserve,+ },+ },+ });+ console.log(`Deleted ${res.count} notification emails`);+}++main();diff --git a/packages/prisma/schema/bounty.prisma b/packages/prisma/schema/bounty.prismaindex b85acfa53a..59576d9a43 100644--- a/packages/prisma/schema/bounty.prisma+++ b/packages/prisma/schema/bounty.prisma@@ -45,6 +45,7 @@ model Bounty {groups BountyGroup[]program Program @relation(fields: [programId], references: [id], onDelete: Cascade)workflow Workflow? @relation(fields: [workflowId], references: [id], onDelete: Cascade)+ emails NotificationEmail[]@@index(programId)}diff --git a/packages/prisma/schema/notification.prisma b/packages/prisma/schema/notification.prismaindex 6122117c4f..8c00125e86 100644--- a/packages/prisma/schema/notification.prisma+++ b/packages/prisma/schema/notification.prisma@@ -5,7 +5,7 @@ enum NotificationEmailType {model NotificationEmail {id String @id @default(cuid())- emailId String // Resend email id+ emailId String @unique // Resend email idtype NotificationEmailTypemessageId String?bountyId String?@@ -18,8 +18,9 @@ model NotificationEmail {createdAt DateTime @default(now())message Message? @relation(fields: [messageId], references: [id], onDelete: Cascade)+ bounty Bounty? @relation(fields: [bountyId], references: [id], onDelete: Cascade)- @@index([emailId, openedAt])@@index(type)@@index(messageId)+ @@index(bountyId)}