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.

Page discount codes by default link and delete empty link rewards after default links.

Kiran K9b0ca1eca137
diff --git a/apps/web/app/(ee)/api/cron/cleanup/orphaned/route.ts b/apps/web/app/(ee)/api/cron/cleanup/orphaned/route.ts
index 8f866a7ee0..d24c83c074 100644
--- a/apps/web/app/(ee)/api/cron/cleanup/orphaned/route.ts
+++ b/apps/web/app/(ee)/api/cron/cleanup/orphaned/route.ts
@@ -28,20 +28,20 @@ export const POST = withCron(async () => {
const [
deletedRewardsCount,
deletedDiscountsCount,
- deletedLinkRewardsCount,
deletedDefaultLinksCount,
+ deletedLinkRewardsCount,
] = await Promise.all([
deleteOrphanedRewards(cutoff),
deleteOrphanedDiscounts(cutoff),
- deleteEmptyLinkRewards(),
deleteOrphanedDefaultLinks(cutoff),
+ deleteEmptyLinkRewards(),
]);
console.log({
deletedRewardsCount,
deletedDiscountsCount,
- deletedLinkRewardsCount,
deletedDefaultLinksCount,
+ deletedLinkRewardsCount,
});
return logAndRespond("OK");
diff --git a/apps/web/lib/jobs/handlers/publish-discount-codes-creation-job.ts b/apps/web/lib/jobs/handlers/publish-discount-codes-creation-job.ts
index 12ba91a71f..2b176f4d04 100644
--- a/apps/web/lib/jobs/handlers/publish-discount-codes-creation-job.ts
+++ b/apps/web/lib/jobs/handlers/publish-discount-codes-creation-job.ts
@@ -13,7 +13,7 @@ const inputSchema = z.object({
startingAfter: z.string().optional(),
});
-// Page enrollments for a discount and enqueue per-link create-discount-code jobs
+// Page default links for a discount and enqueue per-link create-discount-code jobs
export const publishDiscountCodesCreationJob = defineJob({
name: "publish-discount-codes-creation-job",
schema: inputSchema,
@@ -77,29 +77,34 @@ export const publishDiscountCodesCreationJob = defineJob({
throw error;
}
- const programEnrollments = await prisma.programEnrollment.findMany({
+ // Get default links for the program
+ const partnerLinks = await prisma.link.findMany({
where: {
programId: program.id,
- discountId: discount.id,
- status: {
- in: ACTIVE_ENROLLMENT_STATUSES,
+ discountCode: null,
+ partnerGroupDefaultLinkId: {
+ not: null,
},
- },
- select: {
- id: true,
- partnerId: true,
- discountId: true,
- links: {
- select: {
- id: true,
+ programEnrollment: {
+ status: {
+ in: ACTIVE_ENROLLMENT_STATUSES,
},
- where: {
- discountCode: null,
- partnerGroupDefaultLinkId: {
- not: null,
+ },
+ OR: [
+ {
+ linkReward: {
+ discountId,
},
},
- },
+ {
+ programEnrollment: {
+ discountId,
+ },
+ },
+ ],
+ },
+ select: {
+ id: true,
},
...(startingAfter && {
skip: 1,
@@ -113,31 +118,24 @@ export const publishDiscountCodesCreationJob = defineJob({
take: CRON_BATCH_SIZE,
});
- if (programEnrollments.length === 0) {
- console.info(
- `No more program enrollments found for discount ${discountId}.`,
- );
+ if (partnerLinks.length === 0) {
+ console.info(`No more links found for discount ${discountId}.`);
return;
}
- const links = programEnrollments.flatMap(({ links }) => links);
-
- if (links.length > 0) {
- await enqueueBatchJobs(
- links.map((link) => ({
- queueName: "create-discount-code",
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discount-codes/create`,
- deduplicationId: `${discountId}-${link.id}`,
- body: {
- linkId: link.id,
- },
- })),
- );
- }
+ await enqueueBatchJobs(
+ partnerLinks.map((link) => ({
+ queueName: "create-discount-code",
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/discount-codes/create`,
+ deduplicationId: `${discountId}-${link.id}`,
+ body: {
+ linkId: link.id,
+ },
+ })),
+ );
- if (programEnrollments.length === CRON_BATCH_SIZE) {
- const startingAfter =
- programEnrollments[programEnrollments.length - 1].id;
+ if (partnerLinks.length === CRON_BATCH_SIZE) {
+ const startingAfter = partnerLinks[partnerLinks.length - 1].id;
await publishDiscountCodesCreationJob.dispatch(
{ discountId, startingAfter },
diff --git a/apps/web/lib/rewards/delete-empty-link-rewards.ts b/apps/web/lib/rewards/delete-empty-link-rewards.ts
index b91d3d82f5..6ef50451de 100644
--- a/apps/web/lib/rewards/delete-empty-link-rewards.ts
+++ b/apps/web/lib/rewards/delete-empty-link-rewards.ts
@@ -1,28 +1,82 @@
import { PRISMA_UPDATEMANY_LIMIT } from "@/lib/cron";
import { prisma } from "../prisma";
-// Hard-delete LinkReward rows whose override IDs were all cleared.
-// update-partner-link nulls those IDs instead of deleting the row, and
-// discount deletes SetNull LinkReward.discountId the same way.
+// Hard-delete LinkReward rows with no remaining reward/discount relations.
+// Loads relations (not just FK null checks) so dangling IDs after a hard-delete
+// are treated as empty too — update-partner-link nulls overrides, and discount
+// deletes SetNull / may leave a stale discountId until this cron runs.
export async function deleteEmptyLinkRewards() {
let deletedCount = 0;
+ let startAfterId: string | undefined;
while (true) {
- const { count } = await prisma.linkReward.deleteMany({
- where: {
- clickRewardId: null,
- leadRewardId: null,
- saleRewardId: null,
- discountId: null,
+ const batch = await prisma.linkReward.findMany({
+ select: {
+ id: true,
+ clickReward: {
+ select: {
+ id: true,
+ },
+ },
+ leadReward: {
+ select: {
+ id: true,
+ },
+ },
+ saleReward: {
+ select: {
+ id: true,
+ },
+ },
+ discount: {
+ select: {
+ id: true,
+ },
+ },
},
- limit: PRISMA_UPDATEMANY_LIMIT,
+ orderBy: {
+ id: "asc",
+ },
+ take: PRISMA_UPDATEMANY_LIMIT,
+ ...(startAfterId && {
+ skip: 1,
+ cursor: {
+ id: startAfterId,
+ },
+ }),
});
- if (count === 0) {
+ if (batch.length === 0) {
break;
}
- deletedCount += count;
+ startAfterId = batch[batch.length - 1].id;
+
+ const emptyIds = batch
+ .filter(
+ (linkReward) =>
+ !linkReward.clickReward &&
+ !linkReward.leadReward &&
+ !linkReward.saleReward &&
+ !linkReward.discount,
+ )
+ .map((linkReward) => linkReward.id);
+
+ if (emptyIds.length > 0) {
+ const { count } = await prisma.linkReward.deleteMany({
+ where: {
+ id: {
+ in: emptyIds,
+ },
+ },
+ });
+
+ deletedCount += count;
+ }
+
+ if (batch.length < PRISMA_UPDATEMANY_LIMIT) {
+ break;
+ }
}
return deletedCount;