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.

handle the coupon code management in link update

Kiran K4d814f03204e
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 8f55c57fad..6528f3f744 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
@@ -58,7 +58,7 @@ export async function POST(req: Request) {
);
}
- const { stripeConnectId } = await prisma.project.findUniqueOrThrow({
+ const workspace = await prisma.project.findUniqueOrThrow({
where: {
defaultProgramId: programId,
},
@@ -67,7 +67,7 @@ export async function POST(req: Request) {
},
});
- if (!stripeConnectId) {
+ if (!workspace.stripeConnectId) {
return new Response("stripeConnectId doesn't exist for the workspace.");
}
@@ -122,9 +122,9 @@ export async function POST(req: Request) {
const results = await Promise.allSettled(
linksChunk.map((link) =>
createStripePromotionCode({
+ workspace,
link,
couponId,
- stripeConnectId,
}),
),
);
diff --git a/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts b/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts
index 73389cdf20..5b1c2f5926 100644
--- a/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts
+++ b/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts
@@ -77,6 +77,7 @@ export const PATCH = withReferralsEmbedToken(
domain: link.domain,
key: link.key,
image: link.image,
+ couponCode: link.couponCode,
},
updatedLink: processedLink,
});
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.ts
index ec90b2a1d1..28cda05d7b 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.ts
@@ -102,6 +102,7 @@ export const PATCH = withPartnerProfile(
domain: link.domain,
key: link.key,
image: link.image,
+ couponCode: link.couponCode,
},
updatedLink: processedLink,
});
diff --git a/apps/web/app/(ee)/api/partners/links/upsert/route.ts b/apps/web/app/(ee)/api/partners/links/upsert/route.ts
index 399162012f..f6fdfba1d3 100644
--- a/apps/web/app/(ee)/api/partners/links/upsert/route.ts
+++ b/apps/web/app/(ee)/api/partners/links/upsert/route.ts
@@ -155,6 +155,7 @@ export const PUT = withWorkspace(
domain: link.domain,
key: link.key,
image: link.image,
+ couponCode: link.couponCode,
},
updatedLink: processedLink,
});
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 3b97169cd9..b3b3a31768 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -179,6 +179,7 @@ export const PATCH = withWorkspace(
domain: link.domain,
key: link.key,
image: link.image,
+ couponCode: link.couponCode,
},
updatedLink: processedLink,
});
diff --git a/apps/web/app/api/links/upsert/route.ts b/apps/web/app/api/links/upsert/route.ts
index 5789fb1d6d..9f9fa9df1a 100644
--- a/apps/web/app/api/links/upsert/route.ts
+++ b/apps/web/app/api/links/upsert/route.ts
@@ -149,6 +149,7 @@ export const PUT = withWorkspace(
domain: link.domain,
key: link.key,
image: link.image,
+ couponCode: link.couponCode,
},
updatedLink: processedLink,
});
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.ts
index 640fe670e1..7c2686bbeb 100644
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ b/apps/web/lib/api/links/bulk-delete-links.ts
@@ -49,8 +49,8 @@ export async function bulkDeleteLinks({
.filter((link) => link.couponCode)
.map((link) =>
disableStripePromotionCode({
- code: link.couponCode,
- stripeConnectId: workspace.stripeConnectId,
+ link,
+ workspace,
}),
),
]);
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index 5a8060c053..98e79b5702 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -253,9 +253,9 @@ export async function createLink(link: CreateLinkProps) {
shouldCreateCouponCode &&
stripeConnectId &&
createStripePromotionCode({
+ workspace: { stripeConnectId },
link: response,
couponId: discount?.couponId!,
- stripeConnectId,
}),
]),
);
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index 9f3a44b042..1f1c48ea8a 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -52,8 +52,8 @@ export async function deleteLink(linkId: string) {
link.couponCode &&
link.project &&
disableStripePromotionCode({
- code: link.couponCode,
- stripeConnectId: link.project.stripeConnectId,
+ link,
+ workspace: link.project,
}),
]),
);
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index 90e0e68123..2e222046e6 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -1,5 +1,7 @@
import { getPartnerAndDiscount } from "@/lib/planetscale/get-partner-discount";
import { isNotHostedImage, storage } from "@/lib/storage";
+import { createStripePromotionCode } from "@/lib/stripe/create-stripe-promotion-code";
+import { disableStripePromotionCode } from "@/lib/stripe/disable-stripe-promotion-code";
import { recordLink } from "@/lib/tinybird";
import { LinkProps, ProcessedLinkProps } from "@/lib/types";
import { propagateWebhookTriggerChanges } from "@/lib/webhook/update-webhook";
@@ -30,6 +32,7 @@ export async function updateLink({
key: string;
image?: string | null;
testCompletedAt?: Date | null;
+ couponCode: string | null;
};
updatedLink: ProcessedLinkProps &
Pick<LinkProps, "id" | "clicks" | "lastClicked" | "updatedAt">;
@@ -208,8 +211,71 @@ export async function updateLink({
testVariants &&
testCompletedAt &&
scheduleABTestCompletion(response),
+
+ changedKey &&
+ updateStripePromotionCode({
+ oldLink,
+ updatedLink: response,
+ }),
]),
);
return transformLink(response);
}
+
+export async function updateStripePromotionCode({
+ oldLink,
+ updatedLink,
+}: {
+ oldLink: Pick<LinkProps, "couponCode">;
+ updatedLink: Pick<LinkProps, "id" | "key" | "partnerId" | "programId">;
+}) {
+ if (!updatedLink.partnerId || !updatedLink.programId) {
+ return;
+ }
+
+ const { program, discount } =
+ await prisma.programEnrollment.findUniqueOrThrow({
+ where: {
+ partnerId_programId: {
+ partnerId: updatedLink.partnerId,
+ programId: updatedLink.programId,
+ },
+ },
+ select: {
+ program: {
+ select: {
+ couponCodeTrackingEnabledAt: true,
+ workspace: {
+ select: {
+ stripeConnectId: true,
+ },
+ },
+ },
+ },
+ discount: {
+ select: {
+ couponId: true,
+ },
+ },
+ },
+ });
+
+ const { couponCodeTrackingEnabledAt, workspace } = program;
+
+ await Promise.allSettled([
+ oldLink.couponCode &&
+ disableStripePromotionCode({
+ workspace,
+ link: oldLink,
+ }),
+
+ couponCodeTrackingEnabledAt &&
+ discount?.couponId &&
+ createStripePromotionCode({
+ workspace,
+ link: updatedLink,
+ couponId: discount.couponId,
+ }),
+ ]);
+}
diff --git a/apps/web/lib/stripe/create-stripe-promotion-code.ts b/apps/web/lib/stripe/create-stripe-promotion-code.ts
index 21e83776bb..dde8a4b867 100644
--- a/apps/web/lib/stripe/create-stripe-promotion-code.ts
+++ b/apps/web/lib/stripe/create-stripe-promotion-code.ts
@@ -1,6 +1,6 @@
import { prisma } from "@dub/prisma";
import { stripeAppClient } from ".";
-import { LinkProps } from "../types";
+import { LinkProps, WorkspaceProps } from "../types";
const stripe = stripeAppClient({
...(process.env.VERCEL_ENV && { livemode: true }),
@@ -10,13 +10,13 @@ const MAX_RETRIES = 2;
// Create a promotion code on Stripe for connected accounts
export async function createStripePromotionCode({
+ workspace,
link,
couponId,
- stripeConnectId,
}: {
+ workspace: Pick<WorkspaceProps, "stripeConnectId">;
link: Pick<LinkProps, "id" | "key">;
couponId: string | null;
- stripeConnectId: string | null;
}) {
if (!couponId) {
console.error(
@@ -25,7 +25,7 @@ export async function createStripePromotionCode({
return;
}
- if (!stripeConnectId) {
+ if (!workspace.stripeConnectId) {
console.error(
"stripeConnectId not found for the workspace. Stripe promotion code creation skipped.",
);
@@ -46,7 +46,7 @@ export async function createStripePromotionCode({
code: couponCode.toUpperCase(),
},
{
- stripeAccount: stripeConnectId,
+ stripeAccount: workspace.stripeConnectId,
},
);
diff --git a/apps/web/lib/stripe/disable-stripe-promotion-code.ts b/apps/web/lib/stripe/disable-stripe-promotion-code.ts
index 34ff734f4d..572abc5b7d 100644
--- a/apps/web/lib/stripe/disable-stripe-promotion-code.ts
+++ b/apps/web/lib/stripe/disable-stripe-promotion-code.ts
@@ -1,27 +1,23 @@
import { stripeAppClient } from ".";
+import { LinkProps, WorkspaceProps } from "../types";
const stripe = stripeAppClient({
...(process.env.VERCEL_ENV && { livemode: true }),
});
-// Disable a promotion code on Stripe for connected accounts
export async function disableStripePromotionCode({
- code,
- stripeConnectId,
+ workspace,
+ link,
}: {
- code: string | null;
- stripeConnectId: string | null;
+ workspace: Pick<WorkspaceProps, "stripeConnectId">;
+ link: Pick<LinkProps, "couponCode">;
}) {
- if (!code) {
- return;
- }
-
- if (!stripeConnectId) {
+ if (!link.couponCode || !workspace.stripeConnectId) {
return;
}
const promotionCodes = await stripe.promotionCodes.list({
- code,
+ code: link.couponCode,
limit: 1,
});
@@ -38,12 +34,12 @@ export async function disableStripePromotionCode({
active: false,
},
{
- stripeAccount: stripeConnectId,
+ stripeAccount: workspace.stripeConnectId,
},
);
} catch (error) {
console.error(
- `Failed to disable Stripe promotion code ${code} for ${stripeConnectId}: ${error}`,
+ `Failed to disable Stripe promotion code ${link.couponCode} for ${workspace.stripeConnectId}: ${error}`,
);
throw new Error(error instanceof Error ? error.message : "Unknown error");