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 promotion code creation logic

Kiran K56daee40f268
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 2e8b19bb93..0c63f35ee0 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
@@ -18,8 +18,7 @@ export async function POST(req: Request) {
const rawBody = await req.text();
await verifyQstashSignature({ req, rawBody });
- const body = schema.parse(JSON.parse(rawBody));
- const { discountId } = body;
+ const { discountId } = schema.parse(JSON.parse(rawBody));
const discount = await prisma.discount.findUnique({
where: {
@@ -31,33 +30,27 @@ export async function POST(req: Request) {
return new Response("Discount not found.");
}
- const { provider, programId, couponId } = discount;
-
- if (provider !== "stripe") {
- return new Response("Discount is not a link-based coupon code.");
- }
-
- if (!couponId) {
- return new Response("Discount coupon ID not found.");
+ if (!discount.couponId) {
+ return new Response("couponId doesn't set for the discount.");
}
- const workspace = await prisma.project.findUnique({
+ const workspace = await prisma.project.findUniqueOrThrow({
where: {
- defaultProgramId: programId,
+ defaultProgramId: discount.programId,
},
select: {
stripeConnectId: true,
},
});
- if (!workspace?.stripeConnectId) {
- return new Response("Workspace not found.");
+ if (!workspace.stripeConnectId) {
+ return new Response("stripeConnectId doesn't exist for the workspace.");
}
const enrollments = await prisma.programEnrollment.findMany({
where: {
- programId,
- couponId: discount.id,
+ programId: discount.programId,
+ discountId: discount.id,
},
select: {
partnerId: true,
@@ -70,7 +63,7 @@ export async function POST(req: Request) {
const links = await prisma.link.findMany({
where: {
- programId,
+ programId: discount.programId,
partnerId: {
in: enrollments.map(({ partnerId }) => partnerId),
},
@@ -84,7 +77,6 @@ export async function POST(req: Request) {
return new Response("No links found.");
}
- const { stripeConnectId } = workspace;
const linksChunks = chunk(links, 20);
const failedRequests: Error[] = [];
@@ -92,9 +84,9 @@ export async function POST(req: Request) {
const results = await Promise.allSettled(
linksChunk.map(({ key }) =>
createStripePromotionCode({
- couponId,
- linkKey: key,
- stripeConnectId,
+ code: key,
+ couponId: discount.couponId!,
+ stripeConnectId: workspace.stripeConnectId,
}),
),
);
diff --git a/apps/web/lib/actions/partners/create-discount.ts b/apps/web/lib/actions/partners/create-discount.ts
index bd1797f008..48e0325061 100644
--- a/apps/web/lib/actions/partners/create-discount.ts
+++ b/apps/web/lib/actions/partners/create-discount.ts
@@ -164,14 +164,13 @@ export const createDiscountAction = authActionClient
}),
// If the coupon code is created on Stripe, create a promotion code for each link
- stripeCoupon
- ? qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/create-promotion-codes`,
- body: {
- discountId: discount.id,
- },
- })
- : Promise.resolve(),
+ stripeCoupon &&
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/create-promotion-codes`,
+ body: {
+ discountId: discount.id,
+ },
+ }),
]);
})(),
);
diff --git a/apps/web/lib/stripe/create-promotion-code.ts b/apps/web/lib/stripe/create-promotion-code.ts
index 032d2280da..2d58694f27 100644
--- a/apps/web/lib/stripe/create-promotion-code.ts
+++ b/apps/web/lib/stripe/create-promotion-code.ts
@@ -3,11 +3,11 @@ import { stripeAppClient } from ".";
// Create a promotion code on Stripe for connected accounts
export async function createStripePromotionCode({
couponId,
- linkKey,
+ code,
stripeConnectId,
}: {
couponId: string;
- linkKey: string;
+ code: string;
stripeConnectId: string | null;
}) {
if (!stripeConnectId) {
@@ -25,7 +25,7 @@ export async function createStripePromotionCode({
return await stripe.promotionCodes.create(
{
coupon: couponId,
- code: linkKey.toUpperCase(),
+ code: code.toUpperCase(),
},
{
stripeAccount: stripeConnectId,