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.

Check if bounty start date is at least 10 minutes in the past

Kiran K5df2481fcca0
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index e5021a2161..d4f4e01c60 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -20,6 +20,7 @@ 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 { differenceInMilliseconds } from "date-fns";
import { NextResponse } from "next/server";
// GET /api/bounties - get all bounties for a program
@@ -150,6 +151,11 @@ export const POST = withWorkspace(
});
});
+ // Calculate when the bounty will start (in seconds, minus 10 minutes for early trigger)
+ const diffMs = differenceInMilliseconds(bounty.startsAt, new Date());
+ let totalSeconds = Math.ceil(diffMs / 1000) - 10 * 60;
+ totalSeconds = Math.max(totalSeconds, 0);
+
waitUntil(
Promise.allSettled([
sendWorkspaceWebhook({
@@ -162,8 +168,8 @@ export const POST = withWorkspace(
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/published`,
body: {
bountyId: bounty.id,
- page: 1,
},
+ delay: totalSeconds,
}),
]),
);
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 37ce804918..eb346d4ddc 100644
--- a/apps/web/app/(ee)/api/cron/bounties/published/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/published/route.ts
@@ -10,7 +10,7 @@ export const dynamic = "force-dynamic";
const schema = z.object({
bountyId: z.string(),
- page: z.number().optional().default(1),
+ page: z.number().optional().default(0),
});
const PAGE_SIZE = 100;
@@ -45,6 +45,24 @@ export async function POST(req: Request) {
});
}
+ // Check if bounty start date is at least 10 minutes in the past
+ const diffMs = Date.now() - bounty.startsAt.getTime();
+ const diffMinutes = diffMs / (1000 * 60);
+
+ if (diffMinutes >= 10) {
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/published`,
+ body: {
+ bountyId,
+ },
+ delay: "5m",
+ });
+
+ return logAndRespond(
+ `Bounty ${bountyId} not started yet, it will start at ${bounty.startsAt.toISOString()}`,
+ );
+ }
+
// Find groupIds
const groupIds = bounty.groups.map(({ groupId }) => groupId);