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 workflow support to bounties API

Kiran K31b2a459f213
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index bca2df24fc..329a6049ba 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -1,17 +1,60 @@
+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 { 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, searchParams }) => {
+export const GET = withWorkspace(async ({ workspace }) => {
const programId = getDefaultProgramIdOrThrow(workspace);
- return NextResponse.json({});
+ const bounties = await prisma.bounty.findMany({
+ where: {
+ programId,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ return NextResponse.json(z.array(BountySchema).parse(bounties));
});
// POST /api/bounties - create a bounty
-export const POST = withWorkspace(async ({ workspace, searchParams }) => {
+export const POST = withWorkspace(async ({ workspace, req }) => {
const programId = getDefaultProgramIdOrThrow(workspace);
- return NextResponse.json({});
+ const { name, description, type, rewardAmount, startsAt, endsAt } =
+ createBountySchema.parse(await parseRequestBody(req));
+
+ const workflow = await createWorkflow({
+ program: {
+ id: programId,
+ },
+ workflow: {
+ trigger: "clicks",
+ triggerConditions: {},
+ actions: {},
+ },
+ });
+
+ const bounty = await prisma.bounty.create({
+ data: {
+ id: createId({ prefix: "bounty_" }),
+ programId,
+ workflowId: workflow.id,
+ name,
+ description,
+ type,
+ startsAt,
+ endsAt,
+ rewardAmount,
+ },
+ });
+
+ return NextResponse.json(BountySchema.parse(bounty));
});
diff --git a/apps/web/lib/api/create-id.ts b/apps/web/lib/api/create-id.ts
index 006d651ea9..a0642c5ee5 100644
--- a/apps/web/lib/api/create-id.ts
+++ b/apps/web/lib/api/create-id.ts
@@ -28,6 +28,8 @@ const prefixes = [
"dub_embed_",
"audit_",
"import_",
+ "bounty_",
+ "wf_",
] as const;
// ULID uses base32 encoding
diff --git a/apps/web/lib/api/workflows/create-workflow.ts b/apps/web/lib/api/workflows/create-workflow.ts
new file mode 100644
index 0000000000..e5b6b77ba7
--- /dev/null
+++ b/apps/web/lib/api/workflows/create-workflow.ts
@@ -0,0 +1,23 @@
+import { ProgramProps } from "@/lib/types";
+import { createWorkflowSchema } from "@/lib/zod/schemas/workflows";
+import { prisma } from "@dub/prisma";
+import { z } from "zod";
+import { createId } from "../create-id";
+
+export const createWorkflow = async ({
+ workflow,
+ program,
+}: {
+ workflow: z.input<typeof createWorkflowSchema>;
+ program: Pick<ProgramProps, "id">;
+}) => {
+ return await prisma.workflow.create({
+ data: {
+ id: createId({ prefix: "wf_" }),
+ programId: program.id,
+ trigger: workflow.trigger,
+ triggerConditions: workflow.triggerConditions,
+ actions: workflow.actions,
+ },
+ });
+};
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index d1c3d78d31..91ca8bea7d 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -3,10 +3,28 @@ import { z } from "zod";
import { parseDateSchema } from "./utils";
export const createBountySchema = z.object({
+ name: z
+ .string()
+ .trim()
+ .max(190, "Name must be less than 190 characters")
+ .nullish(),
+ description: z
+ .string()
+ .trim()
+ .max(5000, "Description must be less than 5000 characters")
+ .nullish(),
type: z.nativeEnum(BountyType),
startsAt: parseDateSchema,
endsAt: parseDateSchema.nullish(),
- rewardAmount: z.number().min(0),
- name: z.string().trim().max(190).nullish(),
- description: z.string().trim().max(5000).nullish(),
+ rewardAmount: z.number().min(1, "Reward amount must be greater than 1"),
+});
+
+export const BountySchema = z.object({
+ id: z.string(),
+ name: z.string().nullable(),
+ description: z.string().nullable(),
+ type: z.nativeEnum(BountyType),
+ startsAt: z.date(),
+ endsAt: z.date().nullable(),
+ rewardAmount: z.number(),
});
diff --git a/apps/web/lib/zod/schemas/workflows.ts b/apps/web/lib/zod/schemas/workflows.ts
new file mode 100644
index 0000000000..f5a6b456cc
--- /dev/null
+++ b/apps/web/lib/zod/schemas/workflows.ts
@@ -0,0 +1,8 @@
+import { WorkflowTrigger } from "@dub/prisma/client";
+import { z } from "zod";
+
+export const createWorkflowSchema = z.object({
+ trigger: z.nativeEnum(WorkflowTrigger),
+ triggerConditions: z.any(),
+ actions: z.any(),
+});
diff --git a/packages/prisma/schema/workflow.prisma b/packages/prisma/schema/workflow.prisma
index 69a4f0c8a8..b629c1e465 100644
--- a/packages/prisma/schema/workflow.prisma
+++ b/packages/prisma/schema/workflow.prisma
@@ -7,11 +7,10 @@ enum WorkflowTrigger {
model Workflow {
id String @id @default(cuid())
programId String
- name String
+ name String?
trigger WorkflowTrigger
triggerConditions Json?
actions Json?
- disabledAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt