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.

simplify with notBefore, reenable email sending

Steven Tey081a0fca8052
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index b9425626b8..8cb2286f9a 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -4,6 +4,7 @@ import { throwIfInvalidGroupIds } from "@/lib/api/groups/throw-if-invalid-group-
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { qstash } from "@/lib/cron";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
BountySchema,
@@ -11,7 +12,7 @@ import {
updateBountySchema,
} from "@/lib/zod/schemas/bounties";
import { prisma } from "@dub/prisma";
-import { arrayEqual } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, arrayEqual } from "@dub/utils";
import { PartnerGroup, Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -137,11 +138,24 @@ export const PATCH = withWorkspace(
});
waitUntil(
- sendWorkspaceWebhook({
- workspace,
- trigger: "bounty.updated",
- data: BountySchema.parse(updatedBounty),
- }),
+ Promise.allSettled([
+ sendWorkspaceWebhook({
+ workspace,
+ trigger: "bounty.updated",
+ data: BountySchema.parse(updatedBounty),
+ }),
+
+ // if bounty.startsAt was updated, publish a new message to the queue
+ updatedBounty.startsAt &&
+ updatedBounty.startsAt !== bounty.startsAt &&
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/published`,
+ body: {
+ bountyId: updatedBounty.id,
+ },
+ notBefore: updatedBounty.startsAt.getTime(),
+ }),
+ ]),
);
return NextResponse.json(BountySchema.parse(updatedBounty));
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 008e6dab73..3a60b318ea 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -20,7 +20,6 @@ import { prisma } from "@dub/prisma";
import { Workflow } from "@dub/prisma/client";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
-import { differenceInMinutes } from "date-fns";
import { NextResponse } from "next/server";
// GET /api/bounties - get all bounties for a program
@@ -151,10 +150,6 @@ export const POST = withWorkspace(
});
});
- // Calculate when the bounty will start (in seconds, minus 10 minutes for early trigger)
- let diffMinutes = differenceInMinutes(bounty.startsAt, new Date());
- diffMinutes = Math.max(diffMinutes - 10, 0);
-
waitUntil(
Promise.allSettled([
sendWorkspaceWebhook({
@@ -168,7 +163,8 @@ export const POST = withWorkspace(
body: {
bountyId: bounty.id,
},
- delay: diffMinutes * 60,
+ // unix timestamp in milliseconds
+ notBefore: bounty.startsAt.getTime(),
}),
]),
);
diff --git a/apps/web/app/(ee)/api/cron/bounties/published/route.ts b/apps/web/app/(ee)/api/cron/bounties/published/route.ts
index d1f79a3db5..e0879e19b7 100644
--- a/apps/web/app/(ee)/api/cron/bounties/published/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/published/route.ts
@@ -1,6 +1,9 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { qstash } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { resend } from "@dub/email/resend";
+import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";
+import NewBountyAvailable from "@dub/email/templates/new-bounty-available";
import { prisma } from "@dub/prisma";
import { APP_DOMAIN_WITH_NGROK, log } from "@dub/utils";
import { differenceInMinutes } from "date-fns";
@@ -49,14 +52,6 @@ export async function POST(req: Request) {
let diffMinutes = differenceInMinutes(bounty.startsAt, new Date());
if (diffMinutes >= 10) {
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/published`,
- body: {
- bountyId,
- },
- delay: (diffMinutes - 10) * 60,
- });
-
return logAndRespond(
`Bounty ${bountyId} not started yet, it will start at ${bounty.startsAt.toISOString()}`,
);
@@ -96,31 +91,31 @@ export async function POST(req: Request) {
console.log(
`Sending emails to ${programEnrollments.length} partners: ${programEnrollments.map(({ partner }) => partner.email).join(", ")}`,
);
- // await resend?.batch.send(
- // programEnrollments
- // .filter(({ partner }) => partner.email)
- // .map(({ partner }) => ({
- // from: VARIANT_TO_FROM_MAP.notifications,
- // to: partner.email!,
- // subject: `New bounty available for ${bounty.program.name}`,
- // react: NewBountyAvailable({
- // email: partner.email!,
- // bounty: {
- // name: bounty.name!,
- // type: bounty.type,
- // endsAt: bounty.endsAt,
- // description: bounty.description,
- // },
- // program: {
- // name: bounty.program.name,
- // slug: bounty.program.slug,
- // },
- // }),
- // headers: {
- // "Idempotency-Key": `${bountyId}-${partner.id}`,
- // },
- // })),
- // );
+ await resend?.batch.send(
+ programEnrollments
+ .filter(({ partner }) => partner.email)
+ .map(({ partner }) => ({
+ from: VARIANT_TO_FROM_MAP.notifications,
+ to: partner.email!,
+ subject: `New bounty available for ${bounty.program.name}`,
+ react: NewBountyAvailable({
+ email: partner.email!,
+ bounty: {
+ name: bounty.name!,
+ type: bounty.type,
+ endsAt: bounty.endsAt,
+ description: bounty.description,
+ },
+ program: {
+ name: bounty.program.name,
+ slug: bounty.program.slug,
+ },
+ }),
+ headers: {
+ "Idempotency-Key": `${bountyId}-${partner.id}`,
+ },
+ })),
+ );
// Trigger next page
const nextPage = page + 1;