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.

Add API endpoint to delete discount codes and refactor related logic

Kiran K961196b0547e
diff --git a/apps/web/app/(ee)/api/cron/discounts/disable-stripe-code/route.ts b/apps/web/app/(ee)/api/cron/discounts/delete-discount-code/route.ts
similarity index 97%
rename from apps/web/app/(ee)/api/cron/discounts/disable-stripe-code/route.ts
rename to apps/web/app/(ee)/api/cron/discounts/delete-discount-code/route.ts
index 76fc9503c3..fe827976e8 100644
--- a/apps/web/app/(ee)/api/cron/discounts/disable-stripe-code/route.ts
+++ b/apps/web/app/(ee)/api/cron/discounts/delete-discount-code/route.ts
@@ -11,7 +11,7 @@ const schema = z.object({
discountCodeId: z.string(),
});
-// POST /api/cron/discounts/disable-stripe-code
+// POST /api/cron/discounts/delete-discount-code
export async function POST(req: Request) {
try {
const rawBody = await req.text();
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
index 6ee7fc3d5b..abcbac61fb 100644
--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
@@ -1,7 +1,7 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import {
queueDiscountCodeDeletion,
- shouldKeepStripeDiscountCode,
+ shouldKeepDiscountCodes,
} from "@/lib/api/discounts/queue-discount-code-deletion";
import { DubApiError } from "@/lib/api/errors";
import { getGroupOrThrow } from "@/lib/api/groups/get-group-or-throw";
@@ -255,22 +255,24 @@ export const DELETE = withWorkspace(
});
}
- const keepDiscountCodes = shouldKeepStripeDiscountCode({
+ const keepDiscountCodes = shouldKeepDiscountCodes({
groupDiscount: group.discount,
defaultGroupDiscount: defaultGroup.discount,
});
// Cache discount codes to delete them later
- let discountCodes: DiscountCode[] = [];
+ let discountCodesToDelete: DiscountCode[] = [];
if (group.discountId && !keepDiscountCodes) {
- discountCodes = await prisma.discountCode.findMany({
+ discountCodesToDelete = await prisma.discountCode.findMany({
where: {
discountId: group.discountId,
},
});
}
- await prisma.$transaction(async (tx) => {
+ console.log({ discountCodesToDelete, keepDiscountCodes });
+
+ const deletedGroup = await prisma.$transaction(async (tx) => {
// 1. Update all partners in the group to the default group
await tx.programEnrollment.updateMany({
where: {
@@ -300,8 +302,18 @@ export const DELETE = withWorkspace(
});
}
- // 3. Delete the group's discount
if (group.discountId) {
+ // 3. Update the discount codes
+ await tx.discountCode.updateMany({
+ where: {
+ discountId: group.discountId,
+ },
+ data: {
+ discountId: keepDiscountCodes ? defaultGroup.discountId : null,
+ },
+ });
+
+ // 4. Delete the group's discount
await tx.discount.delete({
where: {
id: group.discountId,
@@ -309,59 +321,51 @@ export const DELETE = withWorkspace(
});
}
- // 4. Delete the group
+ // 5. Delete the group
await tx.partnerGroup.delete({
where: {
id: group.id,
},
});
- // 5. Update the discount codes
- if (keepDiscountCodes) {
- await tx.discountCode.updateMany({
- where: {
- discountId: group.discountId,
- },
- data: {
- discountId: defaultGroup.discountId,
- },
- });
- }
+ return true;
});
- waitUntil(
- Promise.allSettled([
- qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/groups/remap-default-links`,
- body: {
- programId,
- groupId: defaultGroup.id,
- partnerIds: group.partners.map(({ partnerId }) => partnerId),
- userId: session.user.id,
- isGroupDeleted: true,
- },
- }),
-
- ...discountCodes.map((discountCode) =>
- queueDiscountCodeDeletion(discountCode.id),
- ),
-
- recordAuditLog({
- workspaceId: workspace.id,
- programId,
- action: "group.deleted",
- description: `Group ${group.name} (${group.id}) deleted`,
- actor: session.user,
- targets: [
- {
- type: "group",
- id: group.id,
- metadata: group,
+ if (deletedGroup) {
+ waitUntil(
+ Promise.allSettled([
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/groups/remap-default-links`,
+ body: {
+ programId,
+ groupId: defaultGroup.id,
+ partnerIds: group.partners.map(({ partnerId }) => partnerId),
+ userId: session.user.id,
+ isGroupDeleted: true,
},
- ],
- }),
- ]),
- );
+ }),
+
+ ...discountCodesToDelete.map((discountCode) =>
+ queueDiscountCodeDeletion(discountCode.id),
+ ),
+
+ recordAuditLog({
+ workspaceId: workspace.id,
+ programId,
+ action: "group.deleted",
+ description: `Group ${group.name} (${group.id}) deleted`,
+ actor: session.user,
+ targets: [
+ {
+ type: "group",
+ id: group.id,
+ metadata: group,
+ },
+ ],
+ }),
+ ]),
+ );
+ }
return NextResponse.json({ id: group.id });
},
diff --git a/apps/web/lib/api/bounties/trigger-draft-bounty-submissions.ts b/apps/web/lib/api/bounties/trigger-draft-bounty-submissions.ts
index d3e420b7f4..97c6db94dd 100644
--- a/apps/web/lib/api/bounties/trigger-draft-bounty-submissions.ts
+++ b/apps/web/lib/api/bounties/trigger-draft-bounty-submissions.ts
@@ -4,9 +4,6 @@ import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { Bounty } from "@prisma/client";
import { getBountiesByGroups } from "./get-bounties-by-groups";
-// TODO:
-// Need a better method name
-
// Trigger the creation of draft submissions for performance bounties that uses lifetime stats for the given partners
export async function triggerDraftBountySubmissionCreation({
programId,
diff --git a/apps/web/lib/api/discounts/queue-discount-code-deletion.ts b/apps/web/lib/api/discounts/queue-discount-code-deletion.ts
index 8e64b371cc..0b6e1dd191 100644
--- a/apps/web/lib/api/discounts/queue-discount-code-deletion.ts
+++ b/apps/web/lib/api/discounts/queue-discount-code-deletion.ts
@@ -23,7 +23,7 @@ export async function queueDiscountCodeDeletion(
});
await queue.enqueueJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discounts/disable-stripe-code`,
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discounts/delete-discount-code`,
method: "POST",
body: {
discountCodeId,
@@ -31,46 +31,14 @@ export async function queueDiscountCodeDeletion(
});
}
-// Determine if we should delete the discount code from Stripe
-// Used in the following cases:
-// 1. When a group is deleted
-// 2. When a partner is moved to a different group
-// export function shouldDeleteStripeDiscountCode({
-// groupDiscount,
-// defaultGroupDiscount,
-// }: {
-// groupDiscount: Discount | null | undefined;
-// defaultGroupDiscount: Discount | null | undefined;
-// }): boolean {
-// if (!groupDiscount || !defaultGroupDiscount) {
-// return true;
-// }
-
-// // If both groups use the same Stripe coupon
-// if (groupDiscount.couponId === defaultGroupDiscount.couponId) {
-// return false;
-// }
-
-// // If both discounts are effectively equivalent
-// if (
-// groupDiscount.amount === defaultGroupDiscount.amount &&
-// groupDiscount.type === defaultGroupDiscount.type &&
-// groupDiscount.maxDuration === defaultGroupDiscount.maxDuration
-// ) {
-// return false;
-// }
-
-// return true;
-// }
-
-export function shouldKeepStripeDiscountCode({
+export function shouldKeepDiscountCodes({
groupDiscount,
defaultGroupDiscount,
}: {
groupDiscount: Discount | null | undefined;
defaultGroupDiscount: Discount | null | undefined;
}): boolean {
- if (!groupDiscount || !defaultGroupDiscount) {
+ if (!defaultGroupDiscount || !groupDiscount) {
return false;
}