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.

Fix remap treating null coupon IDs as equivalent and roll back provider codes on linkId conflicts.

Kiran K559db95a3270
diff --git a/apps/web/lib/discounts/create-discount-code.ts b/apps/web/lib/discounts/create-discount-code.ts
index e2a11dd7cb..43b10d3876 100644
--- a/apps/web/lib/discounts/create-discount-code.ts
+++ b/apps/web/lib/discounts/create-discount-code.ts
@@ -156,6 +156,12 @@ async function createDiscountCodeRecord({
error.code === "P2002";
if (isUniqueConflict && canRetry) {
+ await rollbackExternalDiscountCode({
+ discountProvider,
+ workspace,
+ code,
+ });
+
const existingForLink = await prisma.discountCode.findUnique({
where: {
linkId: link.id,
@@ -172,12 +178,6 @@ async function createDiscountCodeRecord({
});
}
- await rollbackExternalDiscountCode({
- discountProvider,
- workspace,
- code,
- });
-
const nextCode = `${code}${nanoid(2)}`;
console.warn(
`Discount code "${code}" already exists. Retrying with "${nextCode}".`,
diff --git a/apps/web/lib/discounts/is-discount-equivalent.ts b/apps/web/lib/discounts/is-discount-equivalent.ts
index fc8d6ec093..b6dc8129fa 100644
--- a/apps/web/lib/discounts/is-discount-equivalent.ts
+++ b/apps/web/lib/discounts/is-discount-equivalent.ts
@@ -1,26 +1,32 @@
import { Discount } from "@prisma/client";
+type DiscountEquivalenceFields = Pick<
+ Discount,
+ "couponId" | "provider" | "amount" | "type" | "maxDuration"
+>;
+
export function isDiscountEquivalent(
- firstDiscount: Discount | null | undefined,
- secondDiscount: Discount | null | undefined,
+ firstDiscount: DiscountEquivalenceFields | null | undefined,
+ secondDiscount: DiscountEquivalenceFields | null | undefined,
): boolean {
if (!firstDiscount || !secondDiscount) {
return false;
}
- // If both groups use the same Stripe coupon
- if (firstDiscount.couponId === secondDiscount.couponId) {
+ // If both groups use the same coupon
+ if (
+ firstDiscount.couponId &&
+ secondDiscount.couponId &&
+ firstDiscount.couponId === secondDiscount.couponId
+ ) {
return true;
}
// If both discounts are effectively equivalent
- if (
+ return (
+ firstDiscount.provider === secondDiscount.provider &&
firstDiscount.amount === secondDiscount.amount &&
firstDiscount.type === secondDiscount.type &&
firstDiscount.maxDuration === secondDiscount.maxDuration
- ) {
- return true;
- }
-
- return false;
+ );
}