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.

skip coupon code creation if existing code exists

Kiran Kad1097d13f3c
diff --git a/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts b/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts
index 3fe22e689a..932a1fcf37 100644
--- a/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts
+++ b/apps/web/app/(ee)/api/cron/links/create-promotion-codes/route.ts
@@ -97,6 +97,9 @@ export async function POST(req: Request) {
const enrollments = await prisma.programEnrollment.findMany({
where: {
groupId: group.id,
+ status: {
+ in: ["approved"],
+ },
},
orderBy: {
id: "desc",
@@ -122,6 +125,7 @@ export async function POST(req: Request) {
select: {
id: true,
key: true,
+ couponCode: true,
},
});
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index fc23a3e486..efc99c86b5 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -268,11 +268,8 @@ export async function createLink(link: CreateLinkProps) {
link.id &&
createStripePromotionCode({
workspace,
- link: {
- id: link.id,
- key: link.key,
- },
- discount
+ link: response,
+ discount,
}),
]);
})(),
diff --git a/apps/web/lib/stripe/create-stripe-promotion-code.ts b/apps/web/lib/stripe/create-stripe-promotion-code.ts
index dad2a267ed..711e2a4171 100644
--- a/apps/web/lib/stripe/create-stripe-promotion-code.ts
+++ b/apps/web/lib/stripe/create-stripe-promotion-code.ts
@@ -15,8 +15,15 @@ export async function createStripePromotionCode({
}: {
workspace: Pick<WorkspaceProps, "id" | "stripeConnectId">;
discount: Pick<DiscountProps, "id" | "couponId" | "amount" | "type">;
- link: Pick<LinkProps, "id" | "key">;
+ link: Pick<LinkProps, "id" | "key" | "couponCode">;
}) {
+ if (link.couponCode) {
+ console.log(
+ `Promotion code ${link.couponCode} already exists for link ${link.id}. Stripe promotion code creation skipped.`,
+ );
+ return;
+ }
+
if (!workspace.stripeConnectId) {
console.error(
`stripeConnectId not found for the workspace ${workspace.id}. Stripe promotion code creation skipped.`,
diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.ts
index 62c7084b7f..eb682d0bf0 100644
--- a/apps/web/lib/zod/schemas/programs.ts
+++ b/apps/web/lib/zod/schemas/programs.ts
@@ -86,6 +86,7 @@ export const ProgramPartnerLinkSchema = LinkSchema.pick({
leads: true,
sales: true,
saleAmount: true,
+ couponCode: true,
});
export const ProgramEnrollmentSchema = z.object({