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.

Use atomic updateMany when copying discount to live

Kiran K3b1245568557
diff --git a/apps/web/lib/sandbox/copy-discount-to-live.ts b/apps/web/lib/sandbox/copy-discount-to-live.ts
index dc69ca54f7..b91aaf6a17 100644
--- a/apps/web/lib/sandbox/copy-discount-to-live.ts
+++ b/apps/web/lib/sandbox/copy-discount-to-live.ts
@@ -93,10 +93,6 @@ export const copyDiscountToLiveAction = authActionClient
programId,
});
- if (targetGroup.discountId) {
- throw new Error("The target group already has a discount.");
- }
-
const discountProvider = getDiscountProvider(discount.provider);
await discountProvider.assertDiscountIntegrationAvailable({
@@ -134,15 +130,23 @@ export const copyDiscountToLiveAction = authActionClient
},
});
- await tx.partnerGroup.update({
+ // updateMany (not update) so we only attach when discountId is null and can
+ // detect a concurrent copy via count === 0 without a Prisma not-found error
+ const { count } = await tx.partnerGroup.updateMany({
where: {
id: targetGroupId,
+ discountId: null,
},
data: {
discountId: newDiscount.id,
},
});
+ // This will revert the transaction if the target group already has a discount
+ if (count === 0) {
+ throw new Error("The target group already has a discount.");
+ }
+
await tx.programEnrollment.updateMany({
where: {
groupId: targetGroupId,