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 group API endpoints and update group schema

Kiran K5e36205f3ec0
diff --git a/apps/web/app/(ee)/api/groups/[groupId]/route.ts b/apps/web/app/(ee)/api/groups/[groupId]/route.ts
index e69de29bb2..2bb9af88dd 100644
--- a/apps/web/app/(ee)/api/groups/[groupId]/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupId]/route.ts
@@ -0,0 +1,131 @@
+import { DubApiError } from "@/lib/api/errors";
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { getGroupOrThrow } from "@/lib/api/programs/get-group-or-throw";
+import { withWorkspace } from "@/lib/auth";
+import { GroupSchema } from "@/lib/zod/schemas/groups";
+import { prisma } from "@dub/prisma";
+import { NextResponse } from "next/server";
+
+// TODO:
+// Should we use slug instead of id?
+
+// GET /api/groups/[groupId] - get information about a group
+export const GET = withWorkspace(
+ async ({ params, workspace }) => {
+ const { groupId } = params;
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const group = await getGroupOrThrow({
+ programId,
+ groupId,
+ });
+
+ return NextResponse.json(GroupSchema.parse(group));
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business extra",
+ "business max",
+ "business plus",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
+
+// PATCH /api/groups/[groupId] – update a group for a workspace
+export const PATCH = withWorkspace(
+ async ({ req, params, workspace, session }) => {
+ const { groupId } = params;
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const group = await getGroupOrThrow({
+ programId,
+ groupId,
+ });
+
+ // TODO:
+ // Add PATCH logic
+
+ return NextResponse.json(GroupSchema.parse(group));
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business extra",
+ "business max",
+ "business plus",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
+
+// DELETE /api/groups/[groupId] – delete a group for a workspace
+export const DELETE = withWorkspace(
+ async ({ params, workspace }) => {
+ const { groupId } = params;
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const group = await getGroupOrThrow({
+ programId,
+ groupId,
+ });
+
+ const program = await prisma.program.findUniqueOrThrow({
+ where: {
+ id: programId,
+ },
+ select: {
+ defaultGroupId: true,
+ },
+ });
+
+ if (group.id === program.defaultGroupId) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "You cannot delete the default group of your program.",
+ });
+ }
+
+ const defaultGroup = await getGroupOrThrow({
+ programId,
+ groupId: program.defaultGroupId!,
+ });
+
+ await prisma.$transaction(async (tx) => {
+ await tx.partnerGroup.delete({
+ where: {
+ id: groupId,
+ },
+ });
+
+ await tx.programEnrollment.updateMany({
+ where: {
+ partnerGroupId: groupId,
+ programId,
+ },
+ data: {
+ partnerGroupId: defaultGroup.id,
+ clickRewardId: defaultGroup.clickRewardId,
+ leadRewardId: defaultGroup.leadRewardId,
+ saleRewardId: defaultGroup.saleRewardId,
+ discountId: defaultGroup.discountId,
+ },
+ });
+ });
+
+ return NextResponse.json({ id: groupId });
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business extra",
+ "business max",
+ "business plus",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
diff --git a/apps/web/lib/api/programs/get-group-or-throw.ts b/apps/web/lib/api/programs/get-group-or-throw.ts
new file mode 100644
index 0000000000..a4a845a099
--- /dev/null
+++ b/apps/web/lib/api/programs/get-group-or-throw.ts
@@ -0,0 +1,33 @@
+import { GroupSchema } from "@/lib/zod/schemas/groups";
+import { prisma } from "@dub/prisma";
+import { DubApiError } from "../errors";
+
+export const getGroupOrThrow = async ({
+ programId,
+ groupId,
+}: {
+ programId: string;
+ groupId: string;
+}) => {
+ const group = await prisma.partnerGroup.findUnique({
+ where: {
+ id: groupId,
+ },
+ });
+
+ if (!group) {
+ throw new DubApiError({
+ code: "not_found",
+ message: `Group with ID ${groupId} not found.`,
+ });
+ }
+
+ if (group.programId !== programId) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: `Group with ID ${groupId} not found in your program.`,
+ });
+ }
+
+ return GroupSchema.parse(group);
+};
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index 262356ffe6..8f1f2c1cc1 100644
--- a/apps/web/lib/zod/schemas/groups.ts
+++ b/apps/web/lib/zod/schemas/groups.ts
@@ -12,11 +12,15 @@ export const getGroupsQuerySchema = z
.merge(getPaginationQuerySchema({ pageSize: 100 }));
export const GroupSchema = z.object({
- id: z.string().describe("The unique ID of the group."),
- name: z.string().describe("The name of the group."),
- slug: z.string().describe("The slug of the group."),
- icon: z.string().describe("The icon of the group."),
- color: z.string().describe("The color of the group."),
+ id: z.string(),
+ name: z.string(),
+ slug: z.string(),
+ icon: z.string(),
+ color: z.string(),
+ clickRewardId: z.string().nullable(),
+ leadRewardId: z.string().nullable(),
+ saleRewardId: z.string().nullable(),
+ discountId: z.string().nullable(),
});
export const createGroupSchema = z.object({
diff --git a/packages/prisma/schema/program.prisma b/packages/prisma/schema/program.prisma
index 53752f6e20..940924f42c 100644
--- a/packages/prisma/schema/program.prisma
+++ b/packages/prisma/schema/program.prisma
@@ -110,7 +110,7 @@ model ProgramEnrollment {
@@index(clickRewardId)
@@index(leadRewardId)
@@index(saleRewardId)
- @@index(partnerGroupId)
+ @@index([partnerGroupId, programId])
}
model ProgramApplication {
@@ -148,4 +148,4 @@ model PartnerGroup {
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
@@unique([programId, slug])
-}
\ No newline at end of file
+}