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.

Bounty endpoint updates

Tim Wilson8a7535ae068d
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index 3b9ea711fa..68dbeef0b7 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -1,7 +1,7 @@
-import { getBountyOrThrow } from "@/lib/api/bounty/get-bounty-or-throw";
+import { getBountyOrThrow } from "@/lib/api/bounties/get-bounty-or-throw";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { withWorkspace } from "@/lib/auth";
-import { BountySchema } from "@/lib/zod/schemas/bounties";
+import { BountySchema, BountySchemaExtended } from "@/lib/zod/schemas/bounties";
import { NextResponse } from "next/server";
// GET /api/bounties/[bountyId] - get a bounty
@@ -14,7 +14,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
programId,
});
- return NextResponse.json(BountySchema.parse(bounty));
+ return NextResponse.json(BountySchemaExtended.parse(bounty));
});
// PATCH /api/bounties/[bountyId] - update a bounty
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index 9a294f7f78..d2ae22426e 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -1,27 +1,30 @@
+import { getBounties } from "@/lib/api/bounties/get-bounties";
import { createId } from "@/lib/api/create-id";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { createWorkflow } from "@/lib/api/workflows/create-workflow";
import { withWorkspace } from "@/lib/auth";
-import { BountySchema, createBountySchema } from "@/lib/zod/schemas/bounties";
+import {
+ BountySchema,
+ BountySchemaExtended,
+ createBountySchema,
+ getBountiesQuerySchema,
+} from "@/lib/zod/schemas/bounties";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
import { z } from "zod";
// GET /api/bounties - get all bounties for a program
-export const GET = withWorkspace(async ({ workspace }) => {
+export const GET = withWorkspace(async ({ workspace, searchParams }) => {
const programId = getDefaultProgramIdOrThrow(workspace);
+ const parsedInput = getBountiesQuerySchema.parse(searchParams);
- const bounties = await prisma.bounty.findMany({
- where: {
- programId,
- },
- orderBy: {
- createdAt: "desc",
- },
+ const bounties = await getBounties({
+ ...parsedInput,
+ programId,
});
- return NextResponse.json(z.array(BountySchema).parse(bounties));
+ return NextResponse.json(z.array(BountySchemaExtended).parse(bounties));
});
// POST /api/bounties - create a bounty
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.tsx
index 0645369fb1..8cde7f8d80 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,10 +1,10 @@
-import { BountyProps } from "@/lib/types";
+import { BountyExtendedProps } from "@/lib/types";
import { ProgramOverviewCard } from "@/ui/partners/overview/program-overview-card";
import { CalendarDays, Users } from "@dub/ui/icons";
import { formatDate } from "@dub/utils";
import Link from "next/link";
-export function BountyCard({ bounty }: { bounty: BountyProps }) {
+export function BountyCard({ bounty }: { bounty: BountyExtendedProps }) {
return (
<ProgramOverviewCard className="cursor-pointer border-neutral-200 p-5 transition-all hover:border-neutral-300 hover:shadow-lg">
<Link
@@ -50,9 +50,7 @@ export function BountyCard({ bounty }: { bounty: BountyProps }) {
<Users className="size-3.5" />
<div>
<span className="text-content-default">N</span> of{" "}
- <span className="text-content-default">
- {/* {bounty.submissionsCount} */} X
- </span>{" "}
+ <span className="text-content-default">{bounty.partners}</span>{" "}
partners completed
</div>
</div>
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.tsx
index 769efacba6..ca4edfb0ed 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 { BountyExtendedProps } from "@/lib/types";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
import { Button } from "@dub/ui";
import { fetcher } from "@dub/utils";
@@ -24,9 +24,9 @@ export function BountyList() {
data: bounties,
isLoading,
error,
- } = useSWR<BountyProps[]>(
+ } = useSWR<BountyExtendedProps[]>(
workspaceId && defaultProgramId
- ? `/api/bounties?workspaceId=${workspaceId}&programId=${defaultProgramId}`
+ ? `/api/bounties?workspaceId=${workspaceId}&programId=${defaultProgramId}&includeExpandedFields=true`
: null,
fetcher,
{
diff --git a/apps/web/lib/api/bounties/get-bounties.ts b/apps/web/lib/api/bounties/get-bounties.ts
new file mode 100644
index 0000000000..5cf2a9c696
--- /dev/null
+++ b/apps/web/lib/api/bounties/get-bounties.ts
@@ -0,0 +1,61 @@
+import { getBountiesQuerySchema } from "@/lib/zod/schemas/bounties";
+import { prisma } from "@dub/prisma";
+import { Prisma } from "@prisma/client";
+import { z } from "zod";
+
+type BountyFilters = z.infer<typeof getBountiesQuerySchema> & {
+ programId: string;
+};
+
+const sortColumnsMap = {
+ createdAt: "b.createdAt",
+};
+
+export async function getBounties(filters: BountyFilters) {
+ const {
+ page,
+ pageSize,
+ sortBy,
+ sortOrder,
+ programId,
+ includeExpandedFields,
+ } = filters;
+
+ const bounties = (await prisma.$queryRaw`
+ SELECT
+ b.id,
+ b.name,
+ b.description,
+ b.type,
+ b.startsAt,
+ b.endsAt,
+ b.rewardAmount,
+ ${
+ includeExpandedFields
+ ? Prisma.sql`
+ COUNT(DISTINCT pe.partnerId) as partners
+ `
+ : Prisma.sql`
+ 0 as partners
+ `
+ }
+ FROM Bounty b
+ ${
+ includeExpandedFields
+ ? Prisma.sql`
+ LEFT JOIN BountyGroup bg ON bg.bountyId = b.id
+ LEFT JOIN ProgramEnrollment pe ON pe.groupId = bg.groupId AND pe.status IN ('approved', 'invited')
+ `
+ : Prisma.sql``
+ }
+ WHERE b.programId = ${programId}
+ GROUP BY b.id
+ ORDER BY ${Prisma.raw(sortColumnsMap[sortBy])} ${Prisma.raw(sortOrder)}
+ LIMIT ${pageSize} OFFSET ${(page - 1) * pageSize}
+ `) satisfies Array<any>;
+
+ return bounties.map((bounty) => ({
+ ...bounty,
+ partners: Number(bounty.partners),
+ }));
+}
diff --git a/apps/web/lib/api/bounties/get-bounty-or-throw.ts b/apps/web/lib/api/bounties/get-bounty-or-throw.ts
new file mode 100644
index 0000000000..9797674f2a
--- /dev/null
+++ b/apps/web/lib/api/bounties/get-bounty-or-throw.ts
@@ -0,0 +1,58 @@
+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,
+ ${
+ includeExpandedFields
+ ? Prisma.sql`
+ COUNT(DISTINCT pe.partnerId) as partners
+ `
+ : Prisma.sql`
+ 0 as partners
+ `
+ }
+ FROM Bounty b
+ ${
+ includeExpandedFields
+ ? Prisma.sql`
+ LEFT JOIN BountyGroup bg ON bg.bountyId = b.id
+ LEFT JOIN ProgramEnrollment pe ON pe.groupId = bg.groupId AND pe.status IN ('approved', 'invited')
+ `
+ : Prisma.sql``
+ }
+ WHERE b.programId = ${programId}
+ AND b.id = ${bountyId}
+ GROUP BY b.id
+ LIMIT 1
+ `) satisfies Array<any>;
+
+ if (!bounties.length) {
+ throw new DubApiError({
+ code: "not_found",
+ message: `Bounty ${bountyId} not found.`,
+ });
+ }
+
+ return {
+ ...bounties[0],
+ partners: Number(bounties[0].partners),
+ };
+};
diff --git a/apps/web/lib/api/bounty/get-bounty-or-throw.ts b/apps/web/lib/api/bounty/get-bounty-or-throw.ts
deleted file mode 100644
index 2eaa88fcb7..0000000000
--- a/apps/web/lib/api/bounty/get-bounty-or-throw.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { DubApiError } from "../errors";
-
-export const getBountyOrThrow = async ({
- bountyId,
- programId,
-}: {
- bountyId: string;
- programId: string;
-}) => {
- const bounty = await prisma.bounty.findUnique({
- where: {
- id: bountyId,
- },
- include: {
- _count: {
- select: {
- submissions: true,
- },
- },
- },
- });
-
- if (!bounty) {
- throw new DubApiError({
- code: "not_found",
- message: `Bounty ${bountyId} not found.`,
- });
- }
-
- if (bounty.programId !== programId) {
- throw new DubApiError({
- code: "forbidden",
- message: `Bounty ${bountyId} does not belong to the program.`,
- });
- }
-
- return {
- ...bounty,
- submissionsCount: bounty._count.submissions,
- };
-};
diff --git a/apps/web/lib/swr/use-bounty.ts b/apps/web/lib/swr/use-bounty.ts
index 2df02c9aca..eeb354e35a 100644
--- a/apps/web/lib/swr/use-bounty.ts
+++ b/apps/web/lib/swr/use-bounty.ts
@@ -1,6 +1,6 @@
import { fetcher } from "@dub/utils";
import useSWR from "swr";
-import { BountyProps } from "../types";
+import { BountyExtendedProps } from "../types";
import useWorkspace from "./use-workspace";
export default function useBounty({ bountyId }: { bountyId: string }) {
@@ -10,9 +10,9 @@ export default function useBounty({ bountyId }: { bountyId: string }) {
data: bounty,
error,
isLoading,
- } = useSWR<BountyProps>(
+ } = useSWR<BountyExtendedProps>(
workspaceId && bountyId
- ? `/api/bounties/${bountyId}?workspaceId=${workspaceId}`
+ ? `/api/bounties/${bountyId}?workspaceId=${workspaceId}&includeExpandedFields=true`
: undefined,
fetcher,
);
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 440fc84bb4..ed4ce72443 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -23,7 +23,11 @@ import {
FOLDER_WORKSPACE_ACCESS,
} from "./folder/constants";
import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "./webhook/constants";
-import { BountySchema, BountySubmissionSchema } from "./zod/schemas/bounties";
+import {
+ BountySchema,
+ BountySchemaExtended,
+ BountySubmissionSchema,
+} from "./zod/schemas/bounties";
import {
clickEventResponseSchema,
clickEventSchemaTB,
@@ -513,4 +517,6 @@ export type GroupExtendedProps = z.infer<typeof GroupSchemaExtended>;
export type BountyProps = z.infer<typeof BountySchema>;
+export type BountyExtendedProps = z.infer<typeof BountySchemaExtended>;
+
export type BountySubmissionProps = z.infer<typeof BountySubmissionSchema>;
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index b07984821e..444076c450 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -1,6 +1,7 @@
import { BountySubmissionStatus, BountyType } from "@dub/prisma/client";
import { z } from "zod";
import { CommissionSchema } from "./commissions";
+import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
import { PartnerSchema } from "./partners";
import { UserSchema } from "./users";
import { parseDateSchema } from "./utils";
@@ -40,6 +41,10 @@ export const BountySchema = z.object({
rewardAmount: z.number(),
});
+export const BountySchemaExtended = BountySchema.extend({
+ partners: z.number().default(0),
+});
+
export const BountySubmissionSchema = z.object({
id: z.string(),
description: z.string().nullable(),
@@ -69,3 +74,13 @@ export const BountySubmissionSchema = z.object({
image: true,
}).nullable(),
});
+
+export const BOUNTIES_MAX_PAGE_SIZE = 100;
+
+export const getBountiesQuerySchema = z
+ .object({
+ sortBy: z.enum(["createdAt"]).default("createdAt"),
+ sortOrder: z.enum(["asc", "desc"]).default("desc"),
+ includeExpandedFields: booleanQuerySchema.optional(),
+ })
+ .merge(getPaginationQuerySchema({ pageSize: BOUNTIES_MAX_PAGE_SIZE }));