The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add discount code API endpoints and schemas
Kiran K108ca0981855
diff --git a/apps/web/app/(ee)/api/discount-codes/[discountCodeId]/route.ts b/apps/web/app/(ee)/api/discount-codes/[discountCodeId]/route.tsnew file mode 100644index 0000000000..a27bc4ec22--- /dev/null+++ b/apps/web/app/(ee)/api/discount-codes/[discountCodeId]/route.ts@@ -0,0 +1,47 @@+import { DubApiError } from "@/lib/api/errors";+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";+import { withWorkspace } from "@/lib/auth";+import { prisma } from "@dub/prisma";+import { NextResponse } from "next/server";++// DELETE /api/discount-codes/[discountCodeId] - delete a discount code+export const DELETE = withWorkspace(+ async ({ workspace, params, session }) => {+ const { discountCodeId } = params;+ const programId = getDefaultProgramIdOrThrow(workspace);++ const discountCode = await prisma.promoCode.findUniqueOrThrow({+ where: {+ id: discountCodeId,+ },+ });++ if (discountCode.programId !== programId) {+ throw new DubApiError({+ message: "Discount code not found.",+ code: "bad_request",+ });+ }++ // TODO:+ // Remove from the Stripe first++ await prisma.promoCode.delete({+ where: {+ id: discountCodeId,+ },+ });++ return NextResponse.json({ id: discountCode.id });+ },+ {+ requiredPlan: [+ "business",+ "business plus",+ "business extra",+ "business max",+ "advanced",+ "enterprise",+ ],+ },+);diff --git a/apps/web/app/(ee)/api/discount-codes/route.ts b/apps/web/app/(ee)/api/discount-codes/route.tsnew file mode 100644index 0000000000..e0e27655c9--- /dev/null+++ b/apps/web/app/(ee)/api/discount-codes/route.ts@@ -0,0 +1,139 @@+import { DubApiError } from "@/lib/api/errors";+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";+import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";+import { parseRequestBody } from "@/lib/api/utils";+import { withWorkspace } from "@/lib/auth";+import {+ createDiscountCodeSchema,+ DiscountCodeSchema,+ getDiscountCodesQuerySchema,+} from "@/lib/zod/schemas/discount";+import { prisma } from "@dub/prisma";+import { NextResponse } from "next/server";++// GET /api/discount-codes - get all discount codes for a program+export const GET = withWorkspace(+ async ({ workspace, searchParams }) => {+ const programId = getDefaultProgramIdOrThrow(workspace);++ const { partnerId } = getDiscountCodesQuerySchema.parse(searchParams);++ await getProgramEnrollmentOrThrow({+ partnerId,+ programId,+ });++ const discountCodes = await prisma.promoCode.findMany({+ where: {+ programId,+ partnerId,+ },+ orderBy: {+ createdAt: "desc",+ },+ });++ const response = DiscountCodeSchema.array().parse(discountCodes);++ return NextResponse.json(response);+ },+ {+ requiredPlan: [+ "business",+ "business plus",+ "business extra",+ "business max",+ "advanced",+ "enterprise",+ ],+ },+);++// POST /api/discount-codes - create a discount code+export const POST = withWorkspace(+ async ({ workspace, req, session }) => {+ const programId = getDefaultProgramIdOrThrow(workspace);++ let { partnerId, linkId, code } = createDiscountCodeSchema.parse(+ await parseRequestBody(req),+ );++ const programEnrollment = await getProgramEnrollmentOrThrow({+ partnerId,+ programId,+ includeDiscount: true,+ });++ const link = programEnrollment.links.find((link) => link.id === linkId);++ if (!link) {+ throw new DubApiError({+ code: "bad_request",+ message: "Partner link not found.",+ });+ }++ const discount = programEnrollment.discount;++ if (!discount) {+ throw new DubApiError({+ code: "bad_request",+ message:+ "No discount is assigned to this partner group. Please add a discount before proceeding.",+ });+ }++ if (!discount.couponCodeTrackingEnabledAt) {+ throw new DubApiError({+ code: "bad_request",+ message:+ "Coupon code tracking is not enabled for this discount. Please enable it before creating a discount code.",+ });+ }++ code = code || "DEMO"; // FIx it++ // TODO:+ // Check if the link has an existing discount++ const existingDiscountCode = await prisma.promoCode.findUnique({+ where: {+ programId_code: {+ programId,+ code,+ },+ },+ });++ if (existingDiscountCode) {+ throw new DubApiError({+ code: "bad_request",+ message: `A discount with the code ${code} already exists. Please choose a different code.`,+ });+ }++ const discountCode = await prisma.promoCode.create({+ data: {+ code, // TODO: create a Stripe promotion code+ programId,+ partnerId,+ linkId,+ discountId: discount.id,+ },+ });++ const response = DiscountCodeSchema.parse(discountCode);++ return NextResponse.json(response);+ },+ {+ requiredPlan: [+ "business",+ "business plus",+ "business extra",+ "business max",+ "advanced",+ "enterprise",+ ],+ },+);diff --git a/apps/web/lib/zod/schemas/discount.ts b/apps/web/lib/zod/schemas/discount.tsindex 0cceb43656..4eaaab1bf4 100644--- a/apps/web/lib/zod/schemas/discount.ts+++ b/apps/web/lib/zod/schemas/discount.ts@@ -52,3 +52,24 @@ export const discountPartnersQuerySchema = zpageSize: 25,}),);++// Discount codes+export const DiscountCodeSchema = z.object({+ id: z.string(),+ code: z.string(),+ discountId: z.string(),+ partnerId: z.string(),+ linkId: z.string(),+ createdAt: z.date(),+ updatedAt: z.date(),+});++export const getDiscountCodesQuerySchema = z.object({+ partnerId: z.string(),+});++export const createDiscountCodeSchema = z.object({+ code: z.string().optional(),+ partnerId: z.string(),+ linkId: z.string(),+});diff --git a/packages/prisma/schema/discount.prisma b/packages/prisma/schema/discount.prismaindex b879fb7204..17d39a09d1 100644--- a/packages/prisma/schema/discount.prisma+++ b/packages/prisma/schema/discount.prisma@@ -34,7 +34,8 @@ model PromoCode {createdAt DateTime @default(now())updatedAt DateTime @updatedAt- @@index(programId)+ @@unique([programId, code])+ @@unique([programId, partnerId])@@index(discountId)@@index(partnerId)}