The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Keep create discount code optional in OpenAPI and treat empty strings as omitted.
Kiran Ka41865314feb
diff --git a/apps/web/app/(ee)/api/discount-codes/route.ts b/apps/web/app/(ee)/api/discount-codes/route.tsindex 23c1ad95f9..fd7248181a 100644--- a/apps/web/app/(ee)/api/discount-codes/route.ts+++ b/apps/web/app/(ee)/api/discount-codes/route.ts@@ -68,9 +68,13 @@ export const POST = withWorkspace(async ({ workspace, req, session }) => {const programId = getDefaultProgramIdOrThrow(workspace);- const { partnerId, linkId, code } = createDiscountCodeSchema.parse(- await parseRequestBody(req),- );+ const body = await parseRequestBody(req);++ if (typeof body.code === "string" && body.code.trim() === "") {+ delete body.code;+ }++ const { partnerId, linkId, code } = createDiscountCodeSchema.parse(body);const programEnrollment = await getProgramEnrollmentOrThrow({partnerId,diff --git a/apps/web/lib/zod/schemas/discount.ts b/apps/web/lib/zod/schemas/discount.tsindex f0d70ed15b..e872c8aa61 100644--- a/apps/web/lib/zod/schemas/discount.ts+++ b/apps/web/lib/zod/schemas/discount.ts@@ -90,24 +90,19 @@ export const DiscountCodeSchema = ztitle: "DiscountCode",});-const discountCodeValueSchema = z- .string()- .trim()- .max(100, "Code must be 100 characters or fewer.")- .regex(- /^[a-zA-Z0-9\-_]+$/,- "Code can only contain letters, numbers, dashes, and underscores.",- );-export const createDiscountCodeSchema = z.object({- code: z.preprocess(- (val) => (typeof val === "string" && val.trim() === "" ? undefined : val),- discountCodeValueSchema- .optional()- .describe(- "The discount code to create. If omitted, a unique code will be generated automatically from the partner's name.",- ),- ),+ code: z+ .string()+ .trim()+ .max(100, "Code must be 100 characters or fewer.")+ .regex(+ /^[a-zA-Z0-9\-_]+$/,+ "Code can only contain letters, numbers, dashes, and underscores.",+ )+ .optional()+ .describe(+ "The discount code to create. If omitted, a unique code will be generated automatically from the partner's name.",+ ),partnerId: z.string().describe("The ID of the partner to create a discount code for."),diff --git a/apps/web/playwright/api/discount-codes/discount-codes.spec.ts b/apps/web/playwright/api/discount-codes/discount-codes.spec.tsindex fc305b1711..110b4bcd3a 100644--- a/apps/web/playwright/api/discount-codes/discount-codes.spec.ts+++ b/apps/web/playwright/api/discount-codes/discount-codes.spec.ts@@ -226,6 +226,32 @@ test("POST /discount-codes – omits code and auto-generates", async ({}});+test("POST /discount-codes – empty code auto-generates", async ({ api }) => {+ let partnerId: string | undefined;++ try {+ const { data: partner } = await createPartner(api);+ partnerId = partner.id;+ const linkId = partner.links?.[0]?.id;++ const { status, data } = await api.post<DiscountCode>(+ "/api/discount-codes",+ {+ partnerId: partner.id,+ linkId,+ code: "",+ },+ );++ expect(status).toEqual(200);+ expect(data.code).toEqual(expect.any(String));+ expect(data.code.length).toBeGreaterThan(0);+ expect(data.partnerId).toEqual(partner.id);+ } finally {+ await deletePartner(partnerId);+ }+});+test("POST /discount-codes – auto-generated first-name collision retries", async ({api,}) => {