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.

Persist Bounties emails to Message + NotificationEmail

Steven Tey4396886a65d7
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index 16b471d239..25ec7c0995 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -224,6 +224,7 @@ export const PATCH = withWorkspace(
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/notify-partners`,
body: {
bountyId: updatedBounty.id,
+ userId: session?.user.id,
},
notBefore: Math.floor(updatedBounty.startsAt.getTime() / 1000),
}),
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 738476bbdf..ce8bd7e262 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -253,6 +253,7 @@ export const POST = withWorkspace(
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/notify-partners`,
body: {
bountyId: bounty.id,
+ userId: session?.user.id,
},
notBefore: Math.floor(bounty.startsAt.getTime() / 1000),
}),
diff --git a/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts b/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
index ac5124f231..34bf7bf0a3 100644
--- a/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
@@ -1,3 +1,4 @@
+import { createId } from "@/lib/api/create-id";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { qstash } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
@@ -5,6 +6,7 @@ import { sendBatchEmail } from "@dub/email";
import NewBountyAvailable from "@dub/email/templates/new-bounty-available";
import { prisma } from "@dub/prisma";
import { APP_DOMAIN_WITH_NGROK, log } from "@dub/utils";
+import { NotificationEmailType } from "@prisma/client";
import { differenceInMinutes } from "date-fns";
import { z } from "zod";
import { logAndRespond } from "../../utils";
@@ -13,6 +15,7 @@ export const dynamic = "force-dynamic";
const schema = z.object({
bountyId: z.string(),
+ userId: z.string().nullish(),
page: z.number().optional().default(0),
});
@@ -29,7 +32,7 @@ export async function POST(req: Request) {
rawBody,
});
- const { bountyId, page } = schema.parse(JSON.parse(rawBody));
+ const { bountyId, userId, page } = schema.parse(JSON.parse(rawBody));
// Find bounty
const bounty = await prisma.bounty.findUnique({
@@ -38,7 +41,23 @@ export async function POST(req: Request) {
},
include: {
groups: true,
- program: true,
+ program: {
+ include: {
+ workspace: {
+ select: {
+ users: {
+ select: {
+ userId: true,
+ },
+ where: {
+ role: "owner",
+ },
+ take: 1,
+ },
+ },
+ },
+ },
+ },
},
});
@@ -104,7 +123,7 @@ export async function POST(req: Request) {
console.log(
`Sending emails to ${programEnrollments.length} partners: ${programEnrollments.map(({ partner }) => partner.email).join(", ")}`,
);
- await sendBatchEmail(
+ const { data } = await sendBatchEmail(
programEnrollments.map(({ partner }) => ({
variant: "notifications",
to: partner.email!, // coerce the type here because we've already filtered out partners with no email in the prisma query
@@ -128,6 +147,29 @@ export async function POST(req: Request) {
})),
);
+ if (data) {
+ await prisma.message.createMany({
+ data: programEnrollments.flatMap(({ partner }, idx) => {
+ const messageId = createId({ prefix: "msg_" });
+ return {
+ id: messageId,
+ programId: bounty.programId,
+ partnerId: partner.id,
+ senderUserId: userId ?? bounty.program.workspace.users[0].userId,
+ text: `New bounty available for ${bounty.program.name}`,
+ emails: {
+ create: {
+ type: NotificationEmailType.Bounty,
+ emailId: data.data[idx].id,
+ messageId,
+ bountyId: bounty.id,
+ },
+ },
+ };
+ }),
+ });
+ }
+
if (programEnrollments.length === MAX_PAGE_SIZE) {
const res = await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/notify-partners`,
diff --git a/packages/prisma/schema/notification.prisma b/packages/prisma/schema/notification.prisma
index bc8d534a0a..dc1e517080 100644
--- a/packages/prisma/schema/notification.prisma
+++ b/packages/prisma/schema/notification.prisma
@@ -1,11 +1,13 @@
enum NotificationEmailType {
Message
+ Bounty
}
model NotificationEmail {
id String @id @default(cuid())
emailId String // Resend email id
messageId String?
+ bountyId String?
type NotificationEmailType
openedAt DateTime?
createdAt DateTime @default(now())