The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
BountySchema + BountyListSchema, getBountyWithDetails
Steven Tey998db263d7db
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.tsindex 8b0a2ac815..da1fca2e90 100644--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts@@ -1,4 +1,4 @@-import { getBountyOrThrow } from "@/lib/api/bounties/get-bounty-or-throw";+import { getBountyWithDetails } from "@/lib/api/bounties/get-bounty-with-details";import { DubApiError } from "@/lib/api/errors";import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";import { parseRequestBody } from "@/lib/api/utils";@@ -22,16 +22,14 @@ const schema = z.object({// GET /api/bounties/[bountyId] - get a bountyexport const GET = withWorkspace(- async ({ workspace, params, searchParams }) => {+ async ({ workspace, params }) => {const { bountyId } = params;const programId = getDefaultProgramIdOrThrow(workspace);- const { includeExpandedFields } = schema.parse(searchParams);- const bounty = await getBountyOrThrow({+ const bounty = await getBountyWithDetails({bountyId,programId,- includeExpandedFields,});return NextResponse.json(BountySchemaExtended.parse(bounty));@@ -71,9 +69,11 @@ export const PATCH = withWorkspace(code: "bad_request",});}- const bounty = await getBountyOrThrow({- bountyId,- programId,+ const bounty = await prisma.bounty.findUniqueOrThrow({+ where: {+ id: bountyId,+ programId,+ },});// TODO:@@ -158,13 +158,21 @@ export const DELETE = withWorkspace(const { bountyId } = params;const programId = getDefaultProgramIdOrThrow(workspace);- const bounty = await getBountyOrThrow({- bountyId,- programId,- includeExpandedFields: true,+ const bounty = await prisma.bounty.findUniqueOrThrow({+ where: {+ id: bountyId,+ programId,+ },+ include: {+ _count: {+ select: {+ submissions: true,+ },+ },+ },});- if (bounty.submissions.length > 0) {+ if (bounty._count.submissions > 0) {throw new DubApiError({message:"Bounties with submissions cannot be deleted. You can archive them instead.",diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.tsindex 5285bef725..52c6420f2d 100644--- a/apps/web/app/(ee)/api/bounties/route.ts+++ b/apps/web/app/(ee)/api/bounties/route.ts@@ -6,7 +6,11 @@ import { withWorkspace } from "@/lib/auth";import { qstash } from "@/lib/cron";import { WorkflowAction } from "@/lib/types";import { sendWorkspaceWebhook } from "@/lib/webhook/publish";-import { BountySchema, createBountySchema } from "@/lib/zod/schemas/bounties";+import {+ BountyListSchema,+ BountySchema,+ createBountySchema,+} from "@/lib/zod/schemas/bounties";import {WORKFLOW_ACTION_TYPES,WORKFLOW_ATTRIBUTE_TRIGGER_MAP,@@ -41,7 +45,7 @@ export const GET = withWorkspace(});const data = bounties.map((bounty) =>- BountySchema.parse({+ BountyListSchema.parse({...bounty,groups: bounty.groups.map(({ groupId }) => ({ id: groupId })),submissionsCount: bounty._count.submissions,@@ -123,7 +127,7 @@ export const POST = withWorkspace(}// Create a bounty- const bounty = await tx.bounty.create({+ return await tx.bounty.create({data: {id: bountyId,programId,@@ -149,11 +153,6 @@ export const POST = withWorkspace(}),},});-- return {- ...bounty,- performanceCondition,- };});waitUntil(diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsxindex de1cd62e3b..2026c46f78 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-card.tsx@@ -1,13 +1,13 @@import usePartnersCount from "@/lib/swr/use-partners-count";import useWorkspace from "@/lib/swr/use-workspace";-import { BountyProps } from "@/lib/types";+import { BountyListProps } from "@/lib/types";import { BountyThumbnailImage } from "@/ui/partners/bounties/bounty-thumbnail-image";import { Calendar6, Users } from "@dub/ui/icons";import { formatDate, pluralize } from "@dub/utils";import Link from "next/link";import { useMemo } from "react";-export function BountyCard({ bounty }: { bounty: BountyProps }) {+export function BountyCard({ bounty }: { bounty: BountyListProps }) {const { slug: workspaceSlug } = useWorkspace();const { partnersCount: groupCount } = usePartnersCount<| {diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-list.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-list.tsxindex fa07cb78ce..8eb89b22d1 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-list.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/bounty-list.tsx@@ -1,7 +1,7 @@"use client";import useWorkspace from "@/lib/swr/use-workspace";-import { BountyProps } from "@/lib/types";+import { BountyListProps } from "@/lib/types";import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";import { Button } from "@dub/ui";import { fetcher } from "@dub/utils";@@ -18,7 +18,7 @@ export function BountyList() {data: bounties,isLoading,error,- } = useSWR<BountyProps[]>(+ } = useSWR<BountyListProps[]>(workspaceId ? `/api/bounties?workspaceId=${workspaceId}` : null,fetcher,{diff --git a/apps/web/lib/api/bounties/get-bounty-or-throw.ts b/apps/web/lib/api/bounties/get-bounty-or-throw.tsdeleted file mode 100644index 63fee011a0..0000000000--- a/apps/web/lib/api/bounties/get-bounty-or-throw.ts+++ /dev/null@@ -1,116 +0,0 @@-import { prisma } from "@dub/prisma";-import { Prisma } from "@prisma/client";-import { DubApiError } from "../errors";--export const getBountyOrThrow = async ({- bountyId,- programId,- includeExpandedFields = false,-}: {- bountyId: string;- programId: string;- includeExpandedFields?: boolean;-}) => {- const bounties = (await prisma.$queryRaw`- SELECT- b.id,- b.name,- b.description,- b.type,- b.startsAt,- b.endsAt,- b.rewardAmount,- b.submissionRequirements,- wf.triggerConditions,- ${- includeExpandedFields- ? Prisma.sql`- -- Partners count- COALESCE(- (- SELECT COUNT(pe.partnerId)- FROM ProgramEnrollment pe- WHERE pe.status IN ('approved', 'invited')- AND (- -- If bounty has specific groups, count only partners in those groups- EXISTS (- SELECT 1- FROM BountyGroup bg- WHERE bg.bountyId = b.id AND bg.groupId = pe.groupId- )- OR- -- If bounty has no groups, count all approved/invited partners- NOT EXISTS (- SELECT 1- FROM BountyGroup bg- WHERE bg.bountyId = b.id- )- )- ),- 0- ) AS partners,-- -- Bounty groups- COALESCE(- (- SELECT JSON_ARRAYAGG(- JSON_OBJECT('id', groupId)- )- FROM BountyGroup- WHERE bountyId = b.id- ),- JSON_ARRAY()- ) AS groups,-- -- Bounty submissions count by status- COALESCE(- (- SELECT JSON_OBJECT(- 'pending', COALESCE(SUM(status = 'pending'), 0),- 'approved', COALESCE(SUM(status = 'approved'), 0),- 'rejected', COALESCE(SUM(status = 'rejected'), 0)- )- FROM BountySubmission- WHERE bountyId = b.id- ),- JSON_OBJECT('pending', 0, 'approved', 0, 'rejected', 0)- ) AS submissions- `- : Prisma.sql`- 0 AS partners,- JSON_ARRAY() AS groups,- JSON_OBJECT('pending', 0, 'approved', 0, 'rejected', 0) AS submissions- `- }- FROM Bounty b- LEFT JOIN Workflow wf ON wf.id = b.workflowId- WHERE b.id = ${bountyId} AND b.programId = ${programId}- LIMIT 1- `) satisfies Array<any>;-- if (!bounties.length) {- throw new DubApiError({- code: "not_found",- message: `Bounty ${bountyId} not found.`,- });- }-- const bounty = bounties[0];- const performanceCondition =- bounty.triggerConditions?.length > 0 ? bounty.triggerConditions[0] : null;-- return {- id: bounty.id,- name: bounty.name,- description: bounty.description,- type: bounty.type,- startsAt: bounty.startsAt,- endsAt: bounty.endsAt,- rewardAmount: bounty.rewardAmount,- submissionRequirements: bounty.submissionRequirements,- performanceCondition,- groups: bounty.groups.filter((group) => group !== null) ?? [],- partners: Number(bounty.partners),- submissions: bounty.submissions,- };-};diff --git a/apps/web/lib/api/bounties/get-bounty-with-details.ts b/apps/web/lib/api/bounties/get-bounty-with-details.tsnew file mode 100644index 0000000000..e0ffac1817--- /dev/null+++ b/apps/web/lib/api/bounties/get-bounty-with-details.ts@@ -0,0 +1,105 @@+import { prisma } from "@dub/prisma";+import { DubApiError } from "../errors";++export const getBountyWithDetails = async ({+ bountyId,+ programId,+}: {+ bountyId: string;+ programId: string;+}) => {+ const bounties = (await prisma.$queryRaw`+ SELECT+ b.id,+ b.name,+ b.description,+ b.type,+ b.startsAt,+ b.endsAt,+ b.rewardAmount,+ b.submissionRequirements,+ wf.triggerConditions,++ -- Partners count+ COALESCE(+ (+ SELECT COUNT(pe.partnerId)+ FROM ProgramEnrollment pe+ WHERE pe.status IN ('approved', 'invited')+ AND (+ -- If bounty has specific groups, count only partners in those groups+ EXISTS (+ SELECT 1+ FROM BountyGroup bg+ WHERE bg.bountyId = b.id AND bg.groupId = pe.groupId+ )+ OR+ -- If bounty has no groups, count all approved/invited partners+ NOT EXISTS (+ SELECT 1+ FROM BountyGroup bg+ WHERE bg.bountyId = b.id+ )+ )+ ),+ 0+ ) AS partners,++ -- Bounty groups+ COALESCE(+ (+ SELECT JSON_ARRAYAGG(+ JSON_OBJECT('id', groupId)+ )+ FROM BountyGroup+ WHERE bountyId = b.id+ ),+ JSON_ARRAY()+ ) AS groups,++ -- Bounty submissions count by status+ COALESCE(+ (+ SELECT JSON_OBJECT(+ 'pending', COALESCE(SUM(status = 'pending'), 0),+ 'approved', COALESCE(SUM(status = 'approved'), 0),+ 'rejected', COALESCE(SUM(status = 'rejected'), 0)+ )+ FROM BountySubmission+ WHERE bountyId = b.id+ ),+ JSON_OBJECT('pending', 0, 'approved', 0, 'rejected', 0)+ ) AS submissions++ FROM Bounty b+ LEFT JOIN Workflow wf ON wf.id = b.workflowId+ WHERE b.id = ${bountyId} AND b.programId = ${programId}+ LIMIT 1+ `) satisfies Array<any>;++ if (!bounties.length) {+ throw new DubApiError({+ code: "not_found",+ message: `Bounty ${bountyId} not found.`,+ });+ }++ const bounty = bounties[0];+ const performanceCondition =+ bounty.triggerConditions?.length > 0 ? bounty.triggerConditions[0] : null;++ return {+ id: bounty.id,+ name: bounty.name,+ description: bounty.description,+ type: bounty.type,+ startsAt: bounty.startsAt,+ endsAt: bounty.endsAt,+ rewardAmount: bounty.rewardAmount,+ submissionRequirements: bounty.submissionRequirements,+ performanceCondition,+ groups: bounty.groups.filter((group) => group !== null) ?? [],+ partners: Number(bounty.partners),+ submissions: bounty.submissions,+ };+};diff --git a/apps/web/lib/swr/use-bounty.ts b/apps/web/lib/swr/use-bounty.tsindex ded030fd95..b93b749c56 100644--- a/apps/web/lib/swr/use-bounty.ts+++ b/apps/web/lib/swr/use-bounty.ts@@ -10,7 +10,7 @@ export default function useBounty() {const { data: bounty, error } = useSWR<BountyExtendedProps>(workspaceId && bountyId- ? `/api/bounties/${bountyId}?workspaceId=${workspaceId}&includeExpandedFields=true`+ ? `/api/bounties/${bountyId}?workspaceId=${workspaceId}`: undefined,fetcher,);diff --git a/apps/web/lib/swr/use-partner-analytics.ts b/apps/web/lib/swr/use-partner-analytics.tsindex deb655e372..7bd50512d8 100644--- a/apps/web/lib/swr/use-partner-analytics.ts+++ b/apps/web/lib/swr/use-partner-analytics.ts@@ -60,6 +60,9 @@ export default function usePartnerAnalytics(return {data,error,- loading: programIdToUse && !data && !error ? true : false,+ loading:+ programIdToUse && params.enabled !== false && !data && !error+ ? true+ : false,};}diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 9332b4bafb..1d395eda21 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -25,6 +25,7 @@ import {} from "./folder/constants";import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "./webhook/constants";import {+ BountyListSchema,BountySchema,BountySchemaExtended,BountySubmissionExtendedSchema,@@ -526,7 +527,7 @@ export type GroupProps = z.infer<typeof GroupSchema>;export type GroupExtendedProps = z.infer<typeof GroupSchemaExtended>;export type BountyProps = z.infer<typeof BountySchema>;-+export type BountyListProps = z.infer<typeof BountyListSchema>;export type BountyExtendedProps = z.infer<typeof BountySchemaExtended>;export type PartnerBountyProps = z.infer<typeof PartnerBountySchema>;diff --git a/apps/web/lib/webhook/sample-events/bounty-created.json b/apps/web/lib/webhook/sample-events/bounty-created.jsonindex d432cb20f3..a41173c4f5 100644--- a/apps/web/lib/webhook/sample-events/bounty-created.json+++ b/apps/web/lib/webhook/sample-events/bounty-created.json@@ -6,10 +6,5 @@"startsAt": "2025-08-01T17:34:00.000Z","endsAt": "2025-09-01T17:34:00.000Z","rewardAmount": 10000,- "submissionRequirements": null,- "performanceCondition": {- "attribute": "totalConversions",- "operator": "gte",- "value": 20- }+ "submissionRequirements": null}diff --git a/apps/web/lib/webhook/sample-events/bounty-updated.json b/apps/web/lib/webhook/sample-events/bounty-updated.jsonindex d432cb20f3..a41173c4f5 100644--- a/apps/web/lib/webhook/sample-events/bounty-updated.json+++ b/apps/web/lib/webhook/sample-events/bounty-updated.json@@ -6,10 +6,5 @@"startsAt": "2025-08-01T17:34:00.000Z","endsAt": "2025-09-01T17:34:00.000Z","rewardAmount": 10000,- "submissionRequirements": null,- "performanceCondition": {- "attribute": "totalConversions",- "operator": "gte",- "value": 20- }+ "submissionRequirements": null}diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.tsindex bc465c5ba8..6d26fdd001 100644--- a/apps/web/lib/zod/schemas/bounties.ts+++ b/apps/web/lib/zod/schemas/bounties.ts@@ -53,6 +53,7 @@ export const BountySubmissionFileSchema = z.object({size: z.number(),});+// used in POST, PATCH, DELETE /bounties + bounty.created, bounty.updated webhooksexport const BountySchema = z.object({id: z.string(),name: z.string().nullable(),@@ -62,14 +63,18 @@ export const BountySchema = z.object({endsAt: z.date().nullable(),rewardAmount: z.number(),submissionRequirements: submissionRequirementsSchema.nullable(),+});++// used in GET /bounties+export const BountyListSchema = BountySchema.extend({groups: z.array(GroupSchema.pick({ id: true })),submissionsCount: z.number().default(0),});export const BountySchemaExtended = BountySchema.extend({- performanceCondition: workflowConditionSchema.nullable().default(null),partners: z.number().default(0),submissions: z.record(z.nativeEnum(BountySubmissionStatus), z.number()),+ performanceCondition: workflowConditionSchema.nullable().default(null),});export const BountySubmissionSchema = z.object({diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.tsindex e137b7f926..8b95f7fd38 100644--- a/apps/web/lib/zod/schemas/partner-profile.ts+++ b/apps/web/lib/zod/schemas/partner-profile.ts@@ -134,10 +134,7 @@ export const partnerNotificationTypes = z.enum(["applicationApproved",]);-export const PartnerBountySchema = BountySchema.omit({- groups: true,- submissionsCount: true,-}).extend({+export const PartnerBountySchema = BountySchema.extend({submissions: z.array(BountySubmissionSchema).nullable(),performanceCondition: workflowConditionSchema.nullable().default(null),partner: z.object({