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.

Refactor discount actions to remove unused provider parameter and streamline coupon handling

Kiran K0c8937eff10d
diff --git a/apps/web/lib/actions/partners/create-discount.ts b/apps/web/lib/actions/partners/create-discount.ts
index 09538853bc..bd1797f008 100644
--- a/apps/web/lib/actions/partners/create-discount.ts
+++ b/apps/web/lib/actions/partners/create-discount.ts
@@ -3,7 +3,6 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { createId } from "@/lib/api/create-id";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
-import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { qstash } from "@/lib/cron";
import { createStripeCoupon } from "@/lib/stripe/create-coupon";
import { createDiscountSchema } from "@/lib/zod/schemas/discount";
@@ -26,7 +25,6 @@ export const createDiscountAction = authActionClient
isDefault,
includedPartnerIds,
excludedPartnerIds,
- provider,
} = parsedInput;
includedPartnerIds = includedPartnerIds || [];
@@ -34,11 +32,6 @@ export const createDiscountAction = authActionClient
const programId = getDefaultProgramIdOrThrow(workspace);
- const program = await getProgramOrThrow({
- workspaceId: workspace.id,
- programId,
- });
-
// A program can have only one default discount
if (isDefault) {
const defaultDiscount = await prisma.discount.findFirst({
@@ -84,10 +77,10 @@ export const createDiscountAction = authActionClient
}
}
- // Create Stripe coupon for link-based coupon codes
let stripeCoupon: Stripe.Coupon | null = null;
+ const shouldCreateCouponOnStripe = !couponId && !couponTestId;
- if (provider === "stripe") {
+ if (shouldCreateCouponOnStripe) {
if (!workspace.stripeConnectId) {
throw new Error(
"Make sure you have connected your Stripe account to your workspace to create a coupon.",
@@ -95,19 +88,17 @@ export const createDiscountAction = authActionClient
}
const response = await createStripeCoupon({
+ stripeConnectId: workspace.stripeConnectId,
coupon: {
amount,
type,
maxDuration: maxDuration ?? null,
},
- stripeConnectId: workspace.stripeConnectId,
});
- if (!response) {
- throw new Error("Failed to create a coupon on Stripe.");
+ if (response) {
+ stripeCoupon = response;
}
-
- stripeCoupon = response;
}
const discount = await prisma.discount.create({
@@ -120,7 +111,6 @@ export const createDiscountAction = authActionClient
couponId: stripeCoupon?.id ?? couponId,
couponTestId,
default: isDefault,
- provider,
},
});
@@ -182,18 +172,6 @@ export const createDiscountAction = authActionClient
},
})
: Promise.resolve(),
-
- // Enable the coupon-code tracking if not already enabled
- stripeCoupon && !program.couponCodeTrackingEnabledAt
- ? prisma.program.update({
- where: {
- id: programId,
- },
- data: {
- couponCodeTrackingEnabledAt: new Date(),
- },
- })
- : Promise.resolve(),
]);
})(),
);
diff --git a/apps/web/lib/actions/partners/delete-discount.ts b/apps/web/lib/actions/partners/delete-discount.ts
index 4ded83f04c..386939b623 100644
--- a/apps/web/lib/actions/partners/delete-discount.ts
+++ b/apps/web/lib/actions/partners/delete-discount.ts
@@ -3,7 +3,6 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { getDiscountOrThrow } from "@/lib/api/partners/get-discount-or-throw";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
-import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { qstash } from "@/lib/cron";
import { deleteStripeCoupon } from "@/lib/stripe/delete-coupon";
import { redis } from "@/lib/upstash";
@@ -26,11 +25,6 @@ export const deleteDiscountAction = authActionClient
const programId = getDefaultProgramIdOrThrow(workspace);
- await getProgramOrThrow({
- workspaceId: workspace.id,
- programId,
- });
-
const discount = await getDiscountOrThrow({
programId,
discountId,
@@ -130,12 +124,11 @@ export const deleteDiscountAction = authActionClient
}),
// Remove the Stripe coupon if it exists
- discount.provider === "stripe"
- ? deleteStripeCoupon({
- coupon: discount,
- stripeConnectId: workspace.stripeConnectId,
- })
- : Promise.resolve(),
+ discount.couponId &&
+ deleteStripeCoupon({
+ couponId: discount.couponId,
+ stripeConnectId: workspace.stripeConnectId,
+ }),
]),
);
}
diff --git a/apps/web/lib/actions/partners/update-discount.ts b/apps/web/lib/actions/partners/update-discount.ts
index 31460f090f..4ed8607325 100644
--- a/apps/web/lib/actions/partners/update-discount.ts
+++ b/apps/web/lib/actions/partners/update-discount.ts
@@ -6,7 +6,6 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progr
import { qstash } from "@/lib/cron";
import { updateDiscountSchema } from "@/lib/zod/schemas/discount";
import { prisma } from "@dub/prisma";
-import { Discount } from "@dub/prisma/client";
import { APP_DOMAIN_WITH_NGROK, deepEqual } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { authActionClient } from "../safe-action";
@@ -62,23 +61,18 @@ export const updateDiscountAction = authActionClient
}
}
- let updatedDiscount: Discount | undefined = undefined;
-
- // Stripe doesn't support updating the standard coupon fields
- if (discount.provider !== "stripe") {
- updatedDiscount = await prisma.discount.update({
- where: {
- id: discountId,
- },
- data: {
- amount,
- type,
- maxDuration,
- couponId,
- couponTestId,
- },
- });
- }
+ const updatedDiscount = await prisma.discount.update({
+ where: {
+ id: discountId,
+ },
+ data: {
+ amount,
+ type,
+ maxDuration,
+ couponId,
+ couponTestId,
+ },
+ });
// Update partners associated with the discount
if (discount.default) {
diff --git a/apps/web/lib/stripe/create-coupon.ts b/apps/web/lib/stripe/create-coupon.ts
index 82e417dc6c..749b72a5ce 100644
--- a/apps/web/lib/stripe/create-coupon.ts
+++ b/apps/web/lib/stripe/create-coupon.ts
@@ -25,27 +25,19 @@ export async function createStripeCoupon({
const duration =
maxDuration === null ? "forever" : maxDuration === 1 ? "once" : "repeating";
- try {
- return await stripe.coupons.create(
- {
- currency: "usd",
- duration,
- ...(duration === "repeating" && {
- duration_in_months: maxDuration!,
- }),
- ...(type === "percentage"
- ? { percent_off: amount }
- : { amount_off: amount }),
- },
- {
- stripeAccount: stripeConnectId,
- },
- );
- } catch (error) {
- console.error(
- `Failed to create Stripe coupon for ${stripeConnectId}: ${error}`,
- );
-
- throw new Error(error instanceof Error ? error.message : "Unknown error");
- }
+ return await stripe.coupons.create(
+ {
+ currency: "usd",
+ duration,
+ ...(duration === "repeating" && {
+ duration_in_months: maxDuration!,
+ }),
+ ...(type === "percentage"
+ ? { percent_off: amount }
+ : { amount_off: amount }),
+ },
+ {
+ stripeAccount: stripeConnectId,
+ },
+ );
}
diff --git a/apps/web/lib/stripe/delete-coupon.ts b/apps/web/lib/stripe/delete-coupon.ts
index 413d566474..1c8b877065 100644
--- a/apps/web/lib/stripe/delete-coupon.ts
+++ b/apps/web/lib/stripe/delete-coupon.ts
@@ -1,12 +1,11 @@
-import { Discount } from "@prisma/client";
import { stripeAppClient } from ".";
// Delete a coupon on Stripe for connected accounts
export async function deleteStripeCoupon({
- coupon,
+ couponId,
stripeConnectId,
}: {
- coupon: Pick<Discount, "couponId">;
+ couponId: string;
stripeConnectId: string | null;
}) {
if (!stripeConnectId) {
@@ -16,15 +15,6 @@ export async function deleteStripeCoupon({
return;
}
- const { couponId } = coupon;
-
- if (!couponId) {
- console.error(
- "couponId not found for the discount. Stripe coupon deletion skipped.",
- );
- return;
- }
-
const stripe = stripeAppClient({
livemode: process.env.NODE_ENV === "production",
});
diff --git a/apps/web/lib/zod/schemas/discount.ts b/apps/web/lib/zod/schemas/discount.ts
index b793240d3c..38e57bb9eb 100644
--- a/apps/web/lib/zod/schemas/discount.ts
+++ b/apps/web/lib/zod/schemas/discount.ts
@@ -10,7 +10,6 @@ export const DiscountSchema = z.object({
couponId: z.string().nullable(),
couponTestId: z.string().nullable(),
default: z.boolean(),
- provider: z.enum(["stripe"]).nullable(),
description: z.string().nullish(),
partnersCount: z.number().nullish(),
});
@@ -39,7 +38,6 @@ export const createDiscountSchema = z.object({
.array(z.string())
.nullish()
.describe("Only applicable for default discounts"),
- provider: z.enum(["stripe"]).nullish(),
});
export const updateDiscountSchema = createDiscountSchema.extend({