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 rewards API route and Zod schema for rewards

Kiran K94960f5e9669
diff --git a/apps/web/app/api/programs/[programId]/rewards/route.ts b/apps/web/app/api/programs/[programId]/rewards/route.ts
new file mode 100644
index 0000000000..c180127689
--- /dev/null
+++ b/apps/web/app/api/programs/[programId]/rewards/route.ts
@@ -0,0 +1,75 @@
+import { DubApiError } from "@/lib/api/errors";
+import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
+import { parseRequestBody } from "@/lib/api/utils";
+import { withWorkspace } from "@/lib/auth";
+import { createRewardSchema, RewardSchema } from "@/lib/zod/schemas/rewards";
+import { prisma } from "@dub/prisma";
+import { NextResponse } from "next/server";
+import { z } from "zod";
+
+// GET /api/programs/[programId]/rewards - get all rewards for a program
+export const GET = withWorkspace(async ({ workspace, params }) => {
+ const { programId } = params;
+
+ await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
+
+ const rewards = await prisma.reward.findMany({
+ where: {
+ programId,
+ },
+ include: {
+ programEnrollments: true,
+ },
+ });
+
+ return NextResponse.json(z.array(RewardSchema).parse(rewards));
+});
+
+// POST /api/programs/[programId]/rewards - create a new reward
+export const POST = withWorkspace(async ({ workspace, params, req }) => {
+ const { programId } = params;
+
+ await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
+
+ const { partnerIds, ...data } = createRewardSchema.parse(
+ await parseRequestBody(req),
+ );
+
+ if (partnerIds) {
+ const partners = await prisma.programEnrollment.findMany({
+ where: {
+ programId,
+ id: {
+ in: partnerIds,
+ },
+ },
+ });
+
+ if (partners.length !== partnerIds.length) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Invalid partner IDs provided.",
+ });
+ }
+ }
+
+ const reward = await prisma.reward.create({
+ data: {
+ ...data,
+ programId,
+ programEnrollments: {
+ connect: partnerIds?.map((id) => ({ id })),
+ },
+ },
+ });
+
+ console.log("POST /api/programs/[programId]/rewards", reward);
+
+ return NextResponse.json(RewardSchema.parse(reward));
+});
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
new file mode 100644
index 0000000000..d86eac8d47
--- /dev/null
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -0,0 +1,25 @@
+import { CommissionInterval, CommissionType, EventType } from "@prisma/client";
+import { z } from "zod";
+
+export const RewardSchema = z.object({
+ id: z.string(),
+ programId: z.string(),
+ name: z.string().nullable(),
+ type: z.nativeEnum(EventType),
+ rewardType: z.nativeEnum(CommissionType),
+ amount: z.number(),
+ duration: z.number().nullable(),
+ interval: z.nativeEnum(CommissionInterval).nullable(),
+ createdAt: z.date(),
+ updatedAt: z.date(),
+});
+
+export const createRewardSchema = z.object({
+ name: z.string().max(190).nullish(),
+ type: z.nativeEnum(EventType),
+ rewardType: z.nativeEnum(CommissionType).default(CommissionType.percentage),
+ amount: z.number().int().min(0),
+ duration: z.number().int().positive().nullish(),
+ interval: z.nativeEnum(CommissionInterval).nullish(),
+ partnerIds: z.array(z.string()).nullish(),
+});
diff --git a/packages/prisma/schema/reward.prisma b/packages/prisma/schema/reward.prisma
index 7738713670..949393cb4e 100644
--- a/packages/prisma/schema/reward.prisma
+++ b/packages/prisma/schema/reward.prisma
@@ -2,6 +2,7 @@ model Reward {
id String @id @default(cuid())
programId String
+ name String?
type EventType
rewardType CommissionType @default(percentage) // TODO: Can we do something better here?
amount Int @default(0)
@@ -11,8 +12,8 @@ model Reward {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
- program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
- ProgramEnrollment ProgramEnrollment[]
+ program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
+ programEnrollments ProgramEnrollment[]
@@index([programId])
}