The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor discount code creation logic and improve error handling
Kiran Kc0af060f96e8
diff --git a/apps/web/app/(ee)/api/discount-codes/route.ts b/apps/web/app/(ee)/api/discount-codes/route.tsindex 5a65c644d1..cecdb3971b 100644--- a/apps/web/app/(ee)/api/discount-codes/route.ts+++ b/apps/web/app/(ee)/api/discount-codes/route.ts@@ -50,7 +50,7 @@ export const POST = withWorkspace(async ({ workspace, req, session }) => {const programId = getDefaultProgramIdOrThrow(workspace);- let { partnerId, linkId, code } = createDiscountCodeSchema.parse(+ const { partnerId, linkId, code } = createDiscountCodeSchema.parse(await parseRequestBody(req),);@@ -88,24 +88,23 @@ export const POST = withWorkspace(});}- // Use the link.key as the code if no code is provided- code = code || link.key;-// Check for duplicate by code- const duplicateByCode = await prisma.discountCode.findUnique({- where: {- programId_code: {- programId,- code,+ if (code) {+ const duplicateByCode = await prisma.discountCode.findUnique({+ where: {+ programId_code: {+ programId,+ code,+ },},- },- });-- if (duplicateByCode) {- throw new DubApiError({- code: "bad_request",- message: `A discount with the code ${code} already exists in the program. Please choose a different code.`,});++ if (duplicateByCode) {+ throw new DubApiError({+ code: "bad_request",+ message: `A discount with the code ${code} already exists in the program. Please choose a different code.`,+ });+ }}// A link can have only one discount code@@ -120,11 +119,15 @@ export const POST = withWorkspace(});}+ // Use the link.key as the code if no code is provided+ const finalCode = code || link.key;+try {const stripeDiscountCode = await createStripeDiscountCode({stripeConnectId: workspace.stripeConnectId,discount,- code,+ code: finalCode,+ shouldRetry: !code,});if (!stripeDiscountCode?.code) {diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsxindex 1b6e0f6714..bed6ea8025 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsx@@ -206,57 +206,12 @@ const PartnerDiscountCodes = ({tdClassName: (id) => cn(id === "total" && "text-right", "border-l-0"),className: "[&_tr:last-child>td]:border-b-transparent",scrollWrapperClassName: "min-h-[40px]",+ loading,+ error: error ? "Failed to load discount codes" : undefined,} as any);- if (loading) {- return (- <>- <AddDiscountCodeModal />- <div className="flex items-end justify-between gap-4">- <h2 className="text-content-emphasis text-lg font-semibold">- Discount codes- </h2>- <Button- variant="secondary"- text="Create code"- className="h-8 w-fit rounded-lg px-3 py-2 font-medium"- onClick={() => setShowAddDiscountCodeModal(true)}- />- </div>- <div className="mt-4 flex justify-center py-16">- <LoadingSpinner />- </div>- </>- );- }-- if (error) {- return (- <>- <AddDiscountCodeModal />- <div className="flex items-end justify-between gap-4">- <h2 className="text-content-emphasis text-lg font-semibold">- Discount codes- </h2>- <Button- variant="secondary"- text="Create code"- className="h-8 w-fit rounded-lg px-3 py-2 font-medium"- onClick={() => setShowAddDiscountCodeModal(true)}- />- </div>- <div className="mt-4 flex justify-center py-16">- <span className="text-content-subtle text-sm">- Failed to load discount codes- </span>- </div>- </>- );- }-return (<>- <AddDiscountCodeModal /><div className="flex items-end justify-between gap-4"><h2 className="text-content-emphasis text-lg font-semibold">Discount codes@@ -266,11 +221,18 @@ const PartnerDiscountCodes = ({text="Create code"className="h-8 w-fit rounded-lg px-3 py-2 font-medium"onClick={() => setShowAddDiscountCodeModal(true)}+ disabled={!partner.discountId}+ disabledTooltip={+ !partner.discountId+ ? "No discount assigned to this partner group. Please add a discount before you can create a discount code."+ : undefined+ }/></div><div className="mt-4"><Table {...table} /></div>+ <AddDiscountCodeModal /></>);};diff --git a/apps/web/lib/api/partners/get-partner-for-program.ts b/apps/web/lib/api/partners/get-partner-for-program.tsindex fa890b000f..d3f537e3ca 100644--- a/apps/web/lib/api/partners/get-partner-for-program.ts+++ b/apps/web/lib/api/partners/get-partner-for-program.ts@@ -15,6 +15,7 @@ export async function getPartnerForProgram({pe.programId,pe.partnerId,pe.groupId,+ pe.discountId,pe.tenantId,pe.applicationId,pe.createdAt as enrollmentCreatedAt,diff --git a/apps/web/lib/stripe/create-stripe-discount-code.ts b/apps/web/lib/stripe/create-stripe-discount-code.tsindex 210a6de862..da705cd26b 100644--- a/apps/web/lib/stripe/create-stripe-discount-code.ts+++ b/apps/web/lib/stripe/create-stripe-discount-code.ts@@ -12,10 +12,12 @@ export async function createStripeDiscountCode({stripeConnectId,discount,code,+ shouldRetry = true,}: {stripeConnectId: string;discount: Pick<DiscountProps, "id" | "couponId" | "amount" | "type">;code: string;+ shouldRetry?: boolean; // we don't retry if the code is provided by the user}) {if (!stripeConnectId) {throw new Error(@@ -49,6 +51,10 @@ export async function createStripeDiscountCode({throw error;}+ if (!shouldRetry) {+ throw error;+ }+attempt++;if (attempt >= MAX_ATTEMPTS) {diff --git a/apps/web/lib/swr/use-discount-codes.ts b/apps/web/lib/swr/use-discount-codes.tsindex 06d14dd80c..cb82fbc66e 100644--- a/apps/web/lib/swr/use-discount-codes.ts+++ b/apps/web/lib/swr/use-discount-codes.ts@@ -19,6 +19,7 @@ export default function useDiscountCodes({fetcher,{dedupingInterval: 60000,+ keepPreviousData: true,},);