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.

Sync message read statuses

Tim Wilson8fbe1dd6bba9
diff --git a/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts b/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts
index 3e67f378fb..fdcb2881f5 100644
--- a/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts
+++ b/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts
@@ -127,6 +127,7 @@ export async function POST(req: Request) {
})),
email,
}),
+ tags: [{ name: "type", value: "message-notification" }],
})),
);
diff --git a/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts b/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts
index b379af5579..d36d350672 100644
--- a/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts
+++ b/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts
@@ -129,6 +129,7 @@ export async function POST(req: Request) {
})),
email,
}),
+ tags: [{ name: "type", value: "message-notification" }],
})),
);
diff --git a/apps/web/app/api/resend/webhook/route.ts b/apps/web/app/api/resend/webhook/route.ts
index 2a62904ffb..138053c536 100644
--- a/apps/web/app/api/resend/webhook/route.ts
+++ b/apps/web/app/api/resend/webhook/route.ts
@@ -28,10 +28,27 @@ export const POST = async (req: Request) => {
return NextResponse.json({ message: "Invalid signature" }, { status: 400 });
}
- const {
- data: { email, unsubscribed },
- } = JSON.parse(rawBody) || {};
+ const { type, data } = JSON.parse(rawBody) || {};
+ switch (type) {
+ case "contact.updated":
+ await handleContactUpdated(data);
+ break;
+ case "email.opened":
+ await handleEmailOpened(data);
+ break;
+ }
+
+ return NextResponse.json({ message: "Webhook received" });
+};
+
+async function handleContactUpdated({
+ email,
+ unsubscribed,
+}: {
+ email: string;
+ unsubscribed: boolean;
+}) {
console.log(
`${email} ${unsubscribed ? "unsubscribed from" : "subscribed to"} mailing list. Updating user...`,
);
@@ -46,6 +63,44 @@ export const POST = async (req: Request) => {
},
});
}
+}
- return NextResponse.json({ message: "Webhook received" });
-};
+async function handleEmailOpened({
+ tags,
+ email_id: emailId,
+}: {
+ tags?: Record<string, string>;
+ email_id: string;
+}) {
+ // Ignore if not a message notification
+ if (!tags || tags.type !== "message-notification") return;
+
+ console.log(
+ `Updating notification email read statuses for email ${emailId}...`,
+ );
+
+ await Promise.all([
+ prisma.notificationEmail.updateMany({
+ where: {
+ emailId,
+ openedAt: null,
+ },
+ data: {
+ openedAt: new Date(),
+ },
+ }),
+ prisma.message.updateMany({
+ where: {
+ readInEmail: null,
+ emails: {
+ some: {
+ emailId,
+ },
+ },
+ },
+ data: {
+ readInEmail: new Date(),
+ },
+ }),
+ ]);
+}
diff --git a/packages/prisma/schema/message.prisma b/packages/prisma/schema/message.prisma
index 5c4b6186db..9fb822f128 100644
--- a/packages/prisma/schema/message.prisma
+++ b/packages/prisma/schema/message.prisma
@@ -24,4 +24,5 @@ model Message {
@@index([partnerId, programId])
@@index(senderPartnerId)
@@index(senderUserId)
+ @@index(readInEmail)
}
\ No newline at end of file
diff --git a/packages/prisma/schema/notification-email.prisma b/packages/prisma/schema/notification-email.prisma
index bccb6cf3b8..260bbb6cc2 100644
--- a/packages/prisma/schema/notification-email.prisma
+++ b/packages/prisma/schema/notification-email.prisma
@@ -12,6 +12,8 @@ model NotificationEmail {
messageId String?
message Message? @relation(fields: [messageId], references: [id], onDelete: Cascade)
- @@index(messageId)
@@index(emailId)
+ @@index(type)
+ @@index(messageId)
+ @@index([emailId, openedAt])
}
\ No newline at end of file