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/cron/discounts/remap-discount-codes

Kiran Kee5a7c391477
diff --git a/apps/web/app/(ee)/api/cron/discounts/delete-discount-code/route.ts b/apps/web/app/(ee)/api/cron/discounts/delete-discount-code/route.ts
index fe827976e8..d2c2c2915f 100644
--- a/apps/web/app/(ee)/api/cron/discounts/delete-discount-code/route.ts
+++ b/apps/web/app/(ee)/api/cron/discounts/delete-discount-code/route.ts
@@ -15,7 +15,11 @@ const schema = z.object({
export async function POST(req: Request) {
try {
const rawBody = await req.text();
- await verifyQstashSignature({ req, rawBody });
+
+ await verifyQstashSignature({
+ req,
+ rawBody,
+ });
const { discountCodeId } = schema.parse(JSON.parse(rawBody));
diff --git a/apps/web/app/(ee)/api/cron/discounts/remap-discount-codes/route.ts b/apps/web/app/(ee)/api/cron/discounts/remap-discount-codes/route.ts
new file mode 100644
index 0000000000..05cfce144a
--- /dev/null
+++ b/apps/web/app/(ee)/api/cron/discounts/remap-discount-codes/route.ts
@@ -0,0 +1,119 @@
+import {
+ isDiscountEquivalent,
+ queueDiscountCodeDeletion,
+} from "@/lib/api/discounts/queue-discount-code-deletion";
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { prisma } from "@dub/prisma";
+import { z } from "zod";
+import { logAndRespond } from "../../utils";
+
+export const dynamic = "force-dynamic";
+
+const schema = z.object({
+ programId: z.string(),
+ partnerIds: z.array(z.string()),
+ groupId: z.string(),
+});
+
+// POST /api/cron/discounts/remap-discount-codes
+export async function POST(req: Request) {
+ try {
+ const rawBody = await req.text();
+
+ await verifyQstashSignature({
+ req,
+ rawBody,
+ });
+
+ const { programId, partnerIds, groupId } = schema.parse(
+ JSON.parse(rawBody),
+ );
+
+ if (partnerIds.length === 0) {
+ return logAndRespond("No partner IDs provided.");
+ }
+
+ const programEnrollments = await prisma.programEnrollment.findMany({
+ where: {
+ partnerId: {
+ in: partnerIds,
+ },
+ programId,
+ },
+ include: {
+ discountCodes: {
+ include: {
+ discount: true,
+ },
+ },
+ },
+ });
+
+ if (programEnrollments.length === 0) {
+ return logAndRespond("No program enrollments found.");
+ }
+
+ const group = await prisma.partnerGroup.findUnique({
+ where: {
+ id: groupId,
+ },
+ include: {
+ discount: true,
+ },
+ });
+
+ if (!group) {
+ return logAndRespond("Group not found.");
+ }
+
+ const discountCodes = programEnrollments.flatMap(
+ ({ discountCodes }) => discountCodes,
+ );
+
+ const discountCodesToUpdate: string[] = [];
+ const discountCodesToRemove: string[] = [];
+
+ for (const discountCode of discountCodes) {
+ const keepDiscountCode = isDiscountEquivalent(
+ group.discount,
+ discountCode.discount,
+ );
+
+ if (keepDiscountCode) {
+ discountCodesToUpdate.push(discountCode.id);
+ } else {
+ discountCodesToRemove.push(discountCode.id);
+ }
+ }
+
+ console.log({ discountCodesToUpdate, discountCodesToRemove });
+
+ if (discountCodesToUpdate.length > 0) {
+ await prisma.discountCode.updateMany({
+ where: {
+ id: {
+ in: discountCodesToUpdate,
+ },
+ },
+ data: {
+ discountId: group.discount?.id,
+ },
+ });
+ }
+
+ if (discountCodesToRemove.length > 0) {
+ await Promise.allSettled(
+ discountCodesToRemove.map((discountCodeId) =>
+ queueDiscountCodeDeletion(discountCodeId),
+ ),
+ );
+ }
+
+ return logAndRespond(
+ `Updated ${discountCodesToUpdate.length} discount codes and removed ${discountCodesToRemove.length} discount codes.`,
+ );
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/partners/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/partners/route.ts
index b638dbdd23..13b7f5db56 100644
--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/partners/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/partners/route.ts
@@ -68,6 +68,15 @@ export const POST = withWorkspace(
},
}),
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discounts/remap-discount-codes`,
+ body: {
+ programId,
+ partnerIds,
+ groupId: group.id,
+ },
+ }),
+
triggerDraftBountySubmissionCreation({
programId,
partnerIds,
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
index 955a3f6f6f..f2f5f66b13 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,
- shouldKeepDiscountCodes,
+ isDiscountEquivalent,
} 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,10 +255,10 @@ export const DELETE = withWorkspace(
});
}
- const keepDiscountCodes = shouldKeepDiscountCodes({
- groupDiscount: group.discount,
- defaultGroupDiscount: defaultGroup.discount,
- });
+ const keepDiscountCodes = isDiscountEquivalent(
+ group.discount,
+ defaultGroup.discount,
+ );
// Cache discount codes to delete them later
let discountCodesToDelete: DiscountCode[] = [];
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 84f862cabd..e805557f3d 100644
--- a/apps/web/lib/api/discounts/queue-discount-code-deletion.ts
+++ b/apps/web/lib/api/discounts/queue-discount-code-deletion.ts
@@ -31,27 +31,24 @@ export async function queueDiscountCodeDeletion(
});
}
-export function shouldKeepDiscountCodes({
- groupDiscount,
- defaultGroupDiscount,
-}: {
- groupDiscount: Discount | null | undefined;
- defaultGroupDiscount: Discount | null | undefined;
-}): boolean {
- if (!defaultGroupDiscount || !groupDiscount) {
+export function isDiscountEquivalent(
+ firstDiscount: Discount | null | undefined,
+ secondDiscount: Discount | null | undefined,
+): boolean {
+ if (!firstDiscount || !secondDiscount) {
return false;
}
// If both groups use the same Stripe coupon
- if (groupDiscount.couponId === defaultGroupDiscount.couponId) {
+ if (firstDiscount.couponId === secondDiscount.couponId) {
return true;
}
// If both discounts are effectively equivalent
if (
- groupDiscount.amount === defaultGroupDiscount.amount &&
- groupDiscount.type === defaultGroupDiscount.type &&
- groupDiscount.maxDuration === defaultGroupDiscount.maxDuration
+ firstDiscount.amount === secondDiscount.amount &&
+ firstDiscount.type === secondDiscount.type &&
+ firstDiscount.maxDuration === secondDiscount.maxDuration
) {
return true;
}