The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix partner group default link deletion when partner links have discount codes (#4195)
Kiran Kb3b6d1632776
diff --git a/apps/web/app/(ee)/api/cron/groups/delete-default-links/route.ts b/apps/web/app/(ee)/api/cron/groups/delete-default-links/route.tsindex 0517d4300b..40c92439ae 100644--- a/apps/web/app/(ee)/api/cron/groups/delete-default-links/route.ts+++ b/apps/web/app/(ee)/api/cron/groups/delete-default-links/route.ts@@ -1,122 +1,36 @@-import { handleAndReturnErrorResponse } from "@/lib/api/errors";-import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";-import { includeTags } from "@/lib/api/links/include-tags";-import { qstash } from "@/lib/cron";-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";-import { prisma } from "@/lib/prisma";-import { APP_DOMAIN_WITH_NGROK, log, prettyPrint } from "@dub/utils";+import { withCron } from "@/lib/cron/with-cron";+import { defaultLinkDeletedJob } from "@/lib/jobs/handlers/default-link-deleted-job";import * as z from "zod/v4";import { logAndRespond } from "../../utils";-export const dynamic = "force-dynamic";-const MAX_LINKS_PER_BATCH = 100;+export const dynamic = "force-dynamic";const schema = z.object({partnerGroupDefaultLinkId: z.string(),});-// POST /api/cron/groups/delete-default-links – delete a default link for a group-export async function POST(req: Request) {- try {- const rawBody = await req.text();- await verifyQstashSignature({ req, rawBody });-- const { partnerGroupDefaultLinkId } = schema.parse(JSON.parse(rawBody));-- const linksToDelete = await prisma.link.findMany({- where: {- partnerGroupDefaultLinkId,- clicks: 0, // only delete links that have no clicks / activity- },- take: MAX_LINKS_PER_BATCH,- include: {- ...includeTags,- ...includeProgramEnrollment,- },- });-- if (linksToDelete.length > 0) {- const deletedLinks = await prisma.link.deleteMany({- where: {- id: {- in: linksToDelete.map(({ id }) => id),- },- },- });-- console.log(- `Deleted ${deletedLinks.count} links for partner group default link ${partnerGroupDefaultLinkId}.`,- );-- await bulkDeleteLinks(linksToDelete);-- // if linksToDelete is equals to MAX_LINKS_PER_BATCH, there might be more links to delete, so schedule another job to delete the remaining links- if (linksToDelete.length === MAX_LINKS_PER_BATCH) {- const deleteDefaultLinksJob = await qstash.publishJSON({- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/groups/delete-default-links`,- body: {- partnerGroupDefaultLinkId,- },- });-- return logAndRespond(- `Scheduled delete-default-links job for partner group default link ${partnerGroupDefaultLinkId}: ${prettyPrint(deleteDefaultLinksJob)}`,- );- }- }-- // no more links with activity left, remove the partner group default link id from any remaining links (that have no activity)- let batch = 0;- const BATCH_LIMIT = 20;- while (batch < BATCH_LIMIT) {- const updatedLinks = await prisma.link.updateMany({- where: {- partnerGroupDefaultLinkId,- },- data: {- partnerGroupDefaultLinkId: null,- },- limit: MAX_LINKS_PER_BATCH,- });-- if (updatedLinks.count === 0) {- break;- }-- batch++;- }-- if (batch === BATCH_LIMIT) {- // if batch limit reached, there might be more links to update, so schedule another job to delete the remaining links- const deleteDefaultLinksJob = await qstash.publishJSON({- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/groups/delete-default-links`,- body: {- partnerGroupDefaultLinkId,- },- });-- return logAndRespond(- `Scheduled delete-default-links job for partner group default link ${partnerGroupDefaultLinkId}: ${prettyPrint(deleteDefaultLinksJob)}`,- );- }-- // finally, delete the partner group default link- await prisma.partnerGroupDefaultLink.delete({- where: {- id: partnerGroupDefaultLinkId,- },- });-- return logAndRespond(- `Finished deleting all default links for partner group default link ${partnerGroupDefaultLinkId}.`,- );- } catch (error) {- await log({- message: `Error deleting default links: ${error.message}.`,- type: "errors",- });-- return handleAndReturnErrorResponse(error);- }-}+// Legacy QStash endpoint kept for in-flight messages published before the+// jobs migration.++// TODO: Remove this route after old QStash traffic has drained.++// POST /api/cron/groups/delete-default-links+export const POST = withCron(async ({ rawBody }) => {+ const { partnerGroupDefaultLinkId: defaultLinkId } = schema.parse(+ JSON.parse(rawBody),+ );++ await defaultLinkDeletedJob.dispatch(+ {+ defaultLinkId,+ },+ {+ delay: 1,+ label: defaultLinkId,+ },+ );++ return logAndRespond(+ `Forwarded default link deletion to defaultLinkDeletedJob.`,+ );+});diff --git a/apps/web/app/(ee)/api/cron/tags/delete-link-tag/route.ts b/apps/web/app/(ee)/api/cron/tags/delete-link-tag/route.tsdeleted file mode 100644index da6ec252e5..0000000000--- a/apps/web/app/(ee)/api/cron/tags/delete-link-tag/route.ts+++ /dev/null@@ -1,28 +0,0 @@-import { withCron } from "@/lib/cron/with-cron";-import { linkTagDeletedJob } from "@/lib/jobs/handlers/link-tag-deleted-job";-import * as z from "zod/v4";-import { logAndRespond } from "../../utils";--export const dynamic = "force-dynamic";--const schema = z.object({- id: z.string(),-});--// Legacy QStash endpoint kept for in-flight messages published before the-// jobs migration. Forwards to linkTagDeletedJob;--// TODO: Remove this route after old QStash traffic has drained.--// POST /api/cron/tags/delete-link-tag-export const POST = withCron(async ({ rawBody }) => {- const { id: tagId } = schema.parse(JSON.parse(rawBody));-- await linkTagDeletedJob.execute({- tagId,- });-- return logAndRespond(- `Forwarded link tag ${tagId} deletion to linkTagDeletedJob.`,- );-});diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.tsindex db28f589e9..f994becc67 100644--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.ts+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/default-links/[defaultLinkId]/route.ts@@ -5,16 +5,13 @@ import { parseRequestBody } from "@/lib/api/utils";import { extractUtmParams } from "@/lib/api/utm/extract-utm-params";import { withWorkspace } from "@/lib/auth";import { qstash } from "@/lib/cron";+import { defaultLinkDeletedJob } from "@/lib/jobs/handlers/default-link-deleted-job";import { prisma } from "@/lib/prisma";import {createOrUpdateDefaultLinkSchema,PartnerGroupDefaultLinkSchema,} from "@/lib/zod/schemas/groups";-import {- APP_DOMAIN_WITH_NGROK,- constructURLFromUTMParams,- prettyPrint,-} from "@dub/utils";+import { APP_DOMAIN_WITH_NGROK, constructURLFromUTMParams } from "@dub/utils";import { waitUntil } from "@vercel/functions";import { NextResponse } from "next/server";@@ -191,17 +188,6 @@ export const DELETE = withWorkspace(const defaultLinkId = group.partnerGroupDefaultLinks[0].id;- const deleteDefaultLinksJob = await qstash.publishJSON({- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/groups/delete-default-links`,- body: {- partnerGroupDefaultLinkId: defaultLinkId,- },- });-- console.log(- `Scheduled delete-default-links job for partner group default link ${defaultLinkId}: ${prettyPrint(deleteDefaultLinksJob)}`,- );-// soft delete the default link by setting the groupId to nullawait prisma.partnerGroupDefaultLink.update({where: {@@ -211,7 +197,15 @@ export const DELETE = withWorkspace(groupId: null,},});- console.log(`Soft deleted default link ${defaultLinkId}`);++ await defaultLinkDeletedJob.dispatch(+ {+ defaultLinkId,+ },+ {+ label: defaultLinkId,+ },+ );return NextResponse.json({id: defaultLinkId,diff --git a/apps/web/lib/jobs/handlers/default-link-deleted-job.ts b/apps/web/lib/jobs/handlers/default-link-deleted-job.tsnew file mode 100644index 0000000000..c7d2ee5965--- /dev/null+++ b/apps/web/lib/jobs/handlers/default-link-deleted-job.ts@@ -0,0 +1,150 @@+import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";+import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";+import { includeTags } from "@/lib/api/links/include-tags";+import { CRON_BATCH_SIZE } from "@/lib/cron";+import { deleteDiscountCodes } from "@/lib/discounts/delete-discount-code";+import { processInBatches } from "@dub/utils";+import * as z from "zod/v4";+import { prisma } from "../../prisma";+import { defineJob } from "../index";++const inputSchema = z.object({+ defaultLinkId: z.string(),+});++const MAX_BATCHES = 20;++// Job to delete a PartnerGroupDefaultLink and all its link associations+export const defaultLinkDeletedJob = defineJob({+ name: "default-link-deleted-job",+ schema: inputSchema,+ async handle(input) {+ const { defaultLinkId } = input;++ const defaultLink = await prisma.partnerGroupDefaultLink.findUnique({+ where: {+ id: defaultLinkId,+ },+ select: {+ groupId: true,+ },+ });++ if (!defaultLink) {+ console.error(+ `[defaultLinkDeletedJob] Default link ${defaultLinkId} not found. Skipping...`,+ );+ return;+ }++ if (defaultLink.groupId !== null) {+ console.error(+ `[defaultLinkDeletedJob] Default link ${defaultLinkId} not marked for deletion. Skipping...`,+ );+ return;+ }++ // Find the links to delete+ // Only delete links that have no clicks / activity+ const linksToDelete = await prisma.link.findMany({+ where: {+ partnerGroupDefaultLinkId: defaultLinkId,+ clicks: 0,+ },+ take: CRON_BATCH_SIZE,+ include: {+ ...includeTags,+ ...includeProgramEnrollment,+ },+ });++ // If there are links to delete, delete the discount codes and the links+ if (linksToDelete.length > 0) {+ const linkIds = linksToDelete.map(({ id }) => id);++ const discountCodesToDelete = await prisma.discountCode.findMany({+ where: {+ linkId: {+ in: linkIds,+ },+ },+ include: {+ discount: {+ select: {+ provider: true,+ },+ },+ },+ });++ await deleteDiscountCodes(discountCodesToDelete);++ const deletedLinks = await prisma.link.deleteMany({+ where: {+ id: {+ in: linkIds,+ },+ },+ });++ console.log(+ `Deleted ${deletedLinks.count} links for default link ${defaultLinkId}.`,+ );++ if (deletedLinks.count > 0) {+ await bulkDeleteLinks(linksToDelete);+ }+ }++ // More associations remain — queue the next batch.+ if (linksToDelete.length === CRON_BATCH_SIZE) {+ await defaultLinkDeletedJob.dispatch(+ {+ defaultLinkId,+ },+ {+ delay: 1,+ label: defaultLinkId,+ },+ );+ return;+ }++ // No more links with activity left, remove the partner group default link id from any remaining links (that have no activity)+ const { hasMore } = await processInBatches(MAX_BATCHES, () =>+ prisma.link.updateMany({+ where: {+ partnerGroupDefaultLinkId: defaultLinkId,+ },+ data: {+ partnerGroupDefaultLinkId: null,+ },+ limit: CRON_BATCH_SIZE,+ }),+ );++ if (hasMore) {+ await defaultLinkDeletedJob.dispatch(+ {+ defaultLinkId,+ },+ {+ delay: 1,+ label: defaultLinkId,+ },+ );+ return;+ }++ // Finally, delete the partner group default link+ await prisma.partnerGroupDefaultLink.delete({+ where: {+ id: defaultLinkId,+ },+ });++ console.log(+ `[defaultLinkDeletedJob] Deleted partner group default link ${defaultLinkId}.`,+ );+ },+});diff --git a/apps/web/lib/jobs/registry.ts b/apps/web/lib/jobs/registry.tsindex 4d74c6e1cc..f916e4073c 100644--- a/apps/web/lib/jobs/registry.ts+++ b/apps/web/lib/jobs/registry.ts@@ -16,6 +16,11 @@ const jobLoaders = {"link-tag-deleted-job": () =>import("./handlers/link-tag-deleted-job").then((m) => m.linkTagDeletedJob),++ "default-link-deleted-job": () =>+ import("./handlers/default-link-deleted-job").then(+ (m) => m.defaultLinkDeletedJob,+ ),} as const satisfies Record<string, () => Promise<JobDefinition>>;const jobCache = new Map<string, JobDefinition>();diff --git a/apps/web/tests/misc/process-in-batches.test.ts b/apps/web/tests/misc/process-in-batches.test.tsnew file mode 100644index 0000000000..f4c3846e68--- /dev/null+++ b/apps/web/tests/misc/process-in-batches.test.ts@@ -0,0 +1,53 @@+import { processInBatches } from "@dub/utils";+import { describe, expect, it, vi } from "vitest";++describe("processInBatches", () => {+ it("returns hasMore: false when the first batch processes nothing", async () => {+ const processBatch = vi.fn().mockResolvedValue({ count: 0 });++ const result = await processInBatches(5, processBatch);++ expect(result).toEqual({ hasMore: false });+ expect(processBatch).toHaveBeenCalledTimes(1);+ });++ it("stops early and returns hasMore: false when a later batch is empty", async () => {+ const processBatch = vi+ .fn()+ .mockResolvedValueOnce({ count: 10 })+ .mockResolvedValueOnce({ count: 10 })+ .mockResolvedValueOnce({ count: 0 });++ const result = await processInBatches(5, processBatch);++ expect(result).toEqual({ hasMore: false });+ expect(processBatch).toHaveBeenCalledTimes(3);+ });++ it("returns hasMore: true after exhausting maxBatches with remaining work", async () => {+ const processBatch = vi.fn().mockResolvedValue({ count: 10 });++ const result = await processInBatches(3, processBatch);++ expect(result).toEqual({ hasMore: true });+ expect(processBatch).toHaveBeenCalledTimes(3);+ });++ it("throws when maxBatches is 0", async () => {+ const processBatch = vi.fn().mockResolvedValue({ count: 10 });++ await expect(processInBatches(0, processBatch)).rejects.toThrow(+ "maxBatches must be greater than 0.",+ );+ expect(processBatch).not.toHaveBeenCalled();+ });++ it("propagates errors from processBatch", async () => {+ const processBatch = vi.fn().mockRejectedValue(new Error("batch failed"));++ await expect(processInBatches(5, processBatch)).rejects.toThrow(+ "batch failed",+ );+ expect(processBatch).toHaveBeenCalledTimes(1);+ });+});diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.tsindex 580ba56f47..a0f7a6526c 100644--- a/packages/utils/src/functions/index.ts+++ b/packages/utils/src/functions/index.ts@@ -29,6 +29,7 @@ export * from "./parse-filter-value";export * from "./pick";export * from "./pluralize";export * from "./pretty-print";+export * from "./process-in-batches";export * from "./promises";export * from "./punycode";export * from "./random-value";diff --git a/packages/utils/src/functions/process-in-batches.ts b/packages/utils/src/functions/process-in-batches.tsnew file mode 100644index 0000000000..3316012429--- /dev/null+++ b/packages/utils/src/functions/process-in-batches.ts@@ -0,0 +1,26 @@+// Runs a batch operation up to `maxBatches` times in a single invocation.+// Use in cron jobs/background workers when you need to process a large set of rows without exceeding the function timeout.+// Each call to `processBatch` should itself be limited (e.g. Prisma `updateMany`/`deleteMany` with `limit`)+export async function processInBatches(+ maxBatches: number,+ processBatch: () => Promise<{ count: number }>,+): Promise<{ hasMore: boolean }> {+ if (maxBatches <= 0) {+ throw new Error("maxBatches must be greater than 0.");+ }++ for (let batch = 0; batch < maxBatches; batch++) {+ const { count } = await processBatch();++ if (count === 0) {+ return {+ hasMore: false,+ };+ }+ }++ // Exhausted allowed batches. There may still be work left.+ return {+ hasMore: true,+ };+}