Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update resend webhooks, notification tables

Steven Teyd7de37a63f79
diff --git a/apps/web/app/api/resend/webhook/contact-updated.ts b/apps/web/app/api/resend/webhook/contact-updated.ts
index f5b4dd4b17..83a2754ab5 100644
--- a/apps/web/app/api/resend/webhook/contact-updated.ts
+++ b/apps/web/app/api/resend/webhook/contact-updated.ts
@@ -15,12 +15,17 @@ export async function contactUpdated({
`${email} ${unsubscribed ? "unsubscribed from" : "subscribed to"} mailing list. Updating user...`,
);
- await prisma.user.update({
- where: {
- email,
- },
- data: {
- subscribed: !unsubscribed,
- },
- });
+ try {
+ const res = await prisma.user.update({
+ where: {
+ email,
+ },
+ data: {
+ subscribed: !unsubscribed,
+ },
+ });
+ console.log(`Updated user ${email}: ${JSON.stringify(res, null, 2)}`);
+ } catch (error) {
+ console.error(`Error updating user ${email}: ${error.message}`);
+ }
}
diff --git a/apps/web/app/api/resend/webhook/email-opened.ts b/apps/web/app/api/resend/webhook/email-opened.ts
index 6442ce9a78..efc35fce09 100644
--- a/apps/web/app/api/resend/webhook/email-opened.ts
+++ b/apps/web/app/api/resend/webhook/email-opened.ts
@@ -46,6 +46,13 @@ export async function emailOpened({
return;
}
+ if (notificationEmail.openedAt) {
+ console.log(
+ `Ignoring email.opened webhook for email ${emailId} because it already has an openedAt timestamp: ${notificationEmail.openedAt}`,
+ );
+ return;
+ }
+
console.log(
`Updating notification email read statuses for email ${emailId}. Subject: ${subject}`,
);
@@ -54,7 +61,6 @@ export async function emailOpened({
const notificationEmail = await tx.notificationEmail.update({
where: {
emailId,
- openedAt: null,
},
data: {
openedAt: new Date(),
@@ -65,7 +71,6 @@ export async function emailOpened({
`Updated notification email ${notificationEmail.id} with Resend email id ${emailId} to opened at ${new Date()}`,
);
- // TODO: remove this once we make programId and partnerId required
if (!notificationEmail.programId || !notificationEmail.partnerId) {
return notificationEmail;
}
diff --git a/apps/web/scripts/migrations/backfill-notification-email-columns.ts b/apps/web/scripts/migrations/backfill-notification-email-columns.ts
new file mode 100644
index 0000000000..39b8a60772
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-notification-email-columns.ts
@@ -0,0 +1,52 @@
+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 notificationEmailsToBackfill = await prisma.notificationEmail.groupBy({
+ by: ["messageId"],
+ where: {
+ programId: null,
+ partnerId: null,
+ },
+ _count: {
+ messageId: true,
+ },
+ orderBy: {
+ _count: {
+ messageId: "desc",
+ },
+ },
+ take: 50,
+ });
+
+ console.table(notificationEmailsToBackfill);
+
+ for (const notificationEmail of notificationEmailsToBackfill) {
+ if (!notificationEmail.messageId) {
+ console.log(
+ `No messageId found for notification email ${notificationEmail.messageId}`,
+ );
+ continue;
+ }
+ const message = await prisma.message.findUnique({
+ where: { id: notificationEmail.messageId },
+ });
+ if (!message) {
+ console.log(`Message ${notificationEmail.messageId} not found`);
+ continue;
+ }
+ const res = await prisma.notificationEmail.updateMany({
+ where: { messageId: notificationEmail.messageId },
+ data: {
+ programId: message.programId,
+ partnerId: message.partnerId,
+ },
+ });
+ console.log(
+ `Updated ${res.count} notification emails with programId ${message.programId} and partnerId ${message.partnerId}`,
+ );
+ }
+}
+
+main();
diff --git a/packages/prisma/schema/notification.prisma b/packages/prisma/schema/notification.prisma
index 8c00125e86..4bfe076bcb 100644
--- a/packages/prisma/schema/notification.prisma
+++ b/packages/prisma/schema/notification.prisma
@@ -24,3 +24,27 @@ model NotificationEmail {
@@index(messageId)
@@index(bountyId)
}
+
+model NotificationPreference {
+ id String @id @default(cuid())
+ projectUserId String @unique
+ linkUsageSummary Boolean @default(true)
+ domainConfigurationUpdates Boolean @default(true)
+ newPartnerSale Boolean @default(true)
+ newPartnerApplication Boolean @default(true)
+ newBountySubmitted Boolean @default(true)
+ newMessageFromPartner Boolean @default(true)
+
+ projectUser ProjectUsers @relation(fields: [projectUserId], references: [id], onDelete: Cascade)
+}
+
+model PartnerNotificationPreferences {
+ id String @id @default(cuid())
+ partnerUserId String @unique
+
+ commissionCreated Boolean @default(true)
+ applicationApproved Boolean @default(true)
+ newMessageFromProgram Boolean @default(true)
+
+ partnerUser PartnerUser @relation(fields: [partnerUserId], references: [id], onDelete: Cascade)
+}
\ No newline at end of file
diff --git a/packages/prisma/schema/partner.prisma b/packages/prisma/schema/partner.prisma
index 7129a38f58..e20ebb8e9d 100644
--- a/packages/prisma/schema/partner.prisma
+++ b/packages/prisma/schema/partner.prisma
@@ -104,17 +104,6 @@ model PartnerUser {
@@index([partnerId])
}
-model PartnerNotificationPreferences {
- id String @id @default(cuid())
- partnerUserId String @unique
-
- commissionCreated Boolean @default(true)
- applicationApproved Boolean @default(true)
- newMessageFromProgram Boolean @default(true)
-
- partnerUser PartnerUser @relation(fields: [partnerUserId], references: [id], onDelete: Cascade)
-}
-
enum IndustryInterest {
SaaS
DevTool
diff --git a/packages/prisma/schema/workspace.prisma b/packages/prisma/schema/workspace.prisma
index 391ec793ea..370700595a 100644
--- a/packages/prisma/schema/workspace.prisma
+++ b/packages/prisma/schema/workspace.prisma
@@ -109,6 +109,7 @@ model ProjectUsers {
@@index([projectId])
}
+// TODO: could potentially combine this with NotificationEmail in the future?
model SentEmail {
id String @id @default(cuid())
type String
@@ -117,17 +118,4 @@ model SentEmail {
projectId String?
@@index([projectId])
-}
-
-model NotificationPreference {
- id String @id @default(cuid())
- projectUserId String @unique
- linkUsageSummary Boolean @default(true)
- domainConfigurationUpdates Boolean @default(true)
- newPartnerSale Boolean @default(true)
- newPartnerApplication Boolean @default(true)
- newBountySubmitted Boolean @default(true)
- newMessageFromPartner Boolean @default(true)
-
- projectUser ProjectUsers @relation(fields: [projectUserId], references: [id], onDelete: Cascade)
-}
+}
\ No newline at end of file