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.

add bounty webhooks

Kiran K15b63339aaa4
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index 262dd6ec15..9ab108fb58 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -2,6 +2,7 @@ import { getBountyOrThrow } from "@/lib/api/bounties/get-bounty-or-throw";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
BountySchema,
BountySchemaExtended,
@@ -9,6 +10,7 @@ import {
} from "@/lib/zod/schemas/bounties";
import { prisma } from "@dub/prisma";
import { Prisma } from "@prisma/client";
+import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// GET /api/bounties/[bountyId] - get a bounty
@@ -96,6 +98,14 @@ export const PATCH = withWorkspace(async ({ workspace, params, req }) => {
};
});
+ waitUntil(
+ sendWorkspaceWebhook({
+ workspace,
+ trigger: "bounty.updated",
+ data: BountySchema.parse(updatedBounty),
+ }),
+ );
+
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 999e8dd8e4..dc880a0974 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -4,6 +4,7 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progr
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { WorkflowAction } from "@/lib/types";
+import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
BountySchema,
BountySchemaExtended,
@@ -16,6 +17,7 @@ import {
} from "@/lib/zod/schemas/workflows";
import { prisma } from "@dub/prisma";
import { Workflow } from "@dub/prisma/client";
+import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
import { z } from "zod";
@@ -85,7 +87,7 @@ export const POST = withWorkspace(async ({ workspace, req }) => {
}
// Create a bounty
- return await tx.bounty.create({
+ const bounty = await tx.bounty.create({
data: {
id: bountyId,
programId,
@@ -111,7 +113,20 @@ export const POST = withWorkspace(async ({ workspace, req }) => {
}),
},
});
+
+ return {
+ ...bounty,
+ performanceCondition,
+ };
});
+ waitUntil(
+ sendWorkspaceWebhook({
+ workspace,
+ trigger: "bounty.created",
+ data: BountySchema.parse(bounty),
+ }),
+ );
+
return NextResponse.json(BountySchema.parse(bounty));
});
diff --git a/apps/web/lib/integrations/slack/transform.ts b/apps/web/lib/integrations/slack/transform.ts
index e623a6c79b..05fd624eef 100644
--- a/apps/web/lib/integrations/slack/transform.ts
+++ b/apps/web/lib/integrations/slack/transform.ts
@@ -296,6 +296,8 @@ const slackTemplates: Record<WebhookTrigger, any> = {
"sale.created": createSaleTemplate,
"partner.enrolled": enrolledPartnerTemplate,
"commission.created": commissionCreatedTemplate,
+ "bounty.created": () => {},
+ "bounty.updated": () => {},
};
export const formatEventForSlack = (
diff --git a/apps/web/lib/webhook/constants.ts b/apps/web/lib/webhook/constants.ts
index 2c115a2fca..3d567805f6 100644
--- a/apps/web/lib/webhook/constants.ts
+++ b/apps/web/lib/webhook/constants.ts
@@ -8,12 +8,6 @@ export const WEBHOOK_EVENT_ID_PREFIX = "evt_";
export const WEBHOOK_REDIS_KEY = "dub:webhooks";
-// Only available for Dub Partners
-export const PARTNERS_WEBHOOK_TRIGGERS = [
- "partner.enrolled",
- "commission.created",
-] as const;
-
export const WORKSPACE_LEVEL_WEBHOOK_TRIGGERS = [
"link.created",
"link.updated",
@@ -22,6 +16,8 @@ export const WORKSPACE_LEVEL_WEBHOOK_TRIGGERS = [
"sale.created",
"partner.enrolled",
"commission.created",
+ "bounty.created",
+ "bounty.updated",
] as const;
export const LINK_LEVEL_WEBHOOK_TRIGGERS = ["link.clicked"] as const;
@@ -40,6 +36,8 @@ export const WEBHOOK_TRIGGER_DESCRIPTIONS = {
"sale.created": "Sale created",
"partner.enrolled": "Partner enrolled",
"commission.created": "Commission created",
+ "bounty.created": "Bounty created",
+ "bounty.updated": "Bounty updated",
} as const;
export const WEBHOOK_FAILURE_NOTIFY_THRESHOLDS = [5, 10, 15] as const;
diff --git a/apps/web/lib/webhook/sample-events/bounty-created.json b/apps/web/lib/webhook/sample-events/bounty-created.json
new file mode 100644
index 0000000000..1dc496682b
--- /dev/null
+++ b/apps/web/lib/webhook/sample-events/bounty-created.json
@@ -0,0 +1,15 @@
+{
+ "id": "bnty_1K39DGZG3MHY9RP4PD0AS2C5P",
+ "name": "Earn $100 after generating 20 total conversions",
+ "description": "Earn $100 after generating 20 total conversions. This is a test bounty.",
+ "type": "performance",
+ "startsAt": "2025-08-01T17:34:00.000Z",
+ "endsAt": "2025-09-01T17:34:00.000Z",
+ "rewardAmount": 50000,
+ "submissionRequirements": null,
+ "performanceCondition": {
+ "attribute": "totalConversions",
+ "operator": "gte",
+ "value": 20
+ }
+}
diff --git a/apps/web/lib/webhook/sample-events/bounty-updated.json b/apps/web/lib/webhook/sample-events/bounty-updated.json
new file mode 100644
index 0000000000..1dc496682b
--- /dev/null
+++ b/apps/web/lib/webhook/sample-events/bounty-updated.json
@@ -0,0 +1,15 @@
+{
+ "id": "bnty_1K39DGZG3MHY9RP4PD0AS2C5P",
+ "name": "Earn $100 after generating 20 total conversions",
+ "description": "Earn $100 after generating 20 total conversions. This is a test bounty.",
+ "type": "performance",
+ "startsAt": "2025-08-01T17:34:00.000Z",
+ "endsAt": "2025-09-01T17:34:00.000Z",
+ "rewardAmount": 50000,
+ "submissionRequirements": null,
+ "performanceCondition": {
+ "attribute": "totalConversions",
+ "operator": "gte",
+ "value": 20
+ }
+}
diff --git a/apps/web/lib/webhook/sample-events/payload.ts b/apps/web/lib/webhook/sample-events/payload.ts
index a9e1b31baa..e6ff2efdcf 100644
--- a/apps/web/lib/webhook/sample-events/payload.ts
+++ b/apps/web/lib/webhook/sample-events/payload.ts
@@ -1,4 +1,6 @@
import { WebhookTrigger } from "@/lib/types";
+import bountyCreated from "./bounty-created.json";
+import bountyUpdated from "./bounty-updated.json";
import commissionCreated from "./commission-created.json";
import leadCreated from "./lead-created.json";
import linkClicked from "./link-clicked.json";
@@ -17,4 +19,6 @@ export const samplePayload: Record<WebhookTrigger, any> = {
"sale.created": saleCreated,
"partner.enrolled": partnerEnrolled,
"commission.created": commissionCreated,
+ "bounty.created": bountyCreated,
+ "bounty.updated": bountyUpdated,
};
diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.ts
index 19be7221a1..7f72bd7aeb 100644
--- a/apps/web/lib/webhook/types.ts
+++ b/apps/web/lib/webhook/types.ts
@@ -1,4 +1,5 @@
import z from "../zod";
+import { BountySchema } from "../zod/schemas/bounties";
import { CommissionEnrichedSchema } from "../zod/schemas/commissions";
import { linkEventSchema } from "../zod/schemas/links";
import { EnrolledPartnerSchema } from "../zod/schemas/partners";
@@ -23,10 +24,13 @@ export type CommissionEventWebhookPayload = z.infer<
typeof CommissionEnrichedSchema
>;
+export type BountyEventWebhookPayload = z.infer<typeof BountySchema>;
+
export type WebhookEventPayload =
| z.infer<typeof linkEventSchema>
| ClickEventWebhookPayload
| LeadEventWebhookPayload
| SaleEventWebhookPayload
| PartnerEventWebhookPayload
- | CommissionEventWebhookPayload;
+ | CommissionEventWebhookPayload
+ | BountyEventWebhookPayload;
diff --git a/apps/web/tests/webhooks/index.test.ts b/apps/web/tests/webhooks/index.test.ts
index 29789bbdc6..d0b24da99c 100644
--- a/apps/web/tests/webhooks/index.test.ts
+++ b/apps/web/tests/webhooks/index.test.ts
@@ -9,6 +9,7 @@ import {
saleWebhookEventSchema,
} from "@/lib/webhook/schemas";
import z from "@/lib/zod";
+import { BountySchema } from "@/lib/zod/schemas/bounties";
import { CommissionEnrichedSchema } from "@/lib/zod/schemas/commissions";
import { CustomerSchema } from "@/lib/zod/schemas/customers";
import { linkEventSchema } from "@/lib/zod/schemas/links";
@@ -50,6 +51,11 @@ const commissionWebhookEventSchemaExtended = CommissionEnrichedSchema.extend({
customer: customerSchemaExtended,
});
+const bountyWebhookEventSchemaExtended = BountySchema.extend({
+ startsAt: z.string().transform((str) => new Date(str)),
+ endsAt: z.string().transform((str) => (str ? new Date(str) : null)),
+});
+
const eventSchemas: Record<WebhookTrigger, z.ZodSchema> = {
"link.created": linkEventSchema,
"link.updated": linkEventSchema,
@@ -59,6 +65,8 @@ const eventSchemas: Record<WebhookTrigger, z.ZodSchema> = {
"sale.created": saleWebhookEventSchemaExtended,
"partner.enrolled": enrolledPartnerSchemaExtended,
"commission.created": commissionWebhookEventSchemaExtended,
+ "bounty.created": bountyWebhookEventSchemaExtended,
+ "bounty.updated": bountyWebhookEventSchemaExtended,
};
describe("Webhooks", () => {