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.

(WIP) Bulk delete via API

Kiran K7fde50436050
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 3babcb9882..100f87e7ab 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -1,5 +1,10 @@
import { DubApiError, exceededLimitError } from "@/lib/api/errors";
-import { bulkCreateLinks, combineTagIds, processLink } from "@/lib/api/links";
+import {
+ bulkCreateLinks,
+ combineTagIds,
+ deleteLink,
+ processLink,
+} from "@/lib/api/links";
import { bulkUpdateLinks } from "@/lib/api/links/bulk-update-links";
import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
import { parseRequestBody } from "@/lib/api/utils";
@@ -269,3 +274,46 @@ export const PATCH = withWorkspace(async ({ req, workspace, headers }) => {
return NextResponse.json([...response, ...errorLinks], { headers });
});
+
+// DELETE /api/links/bulk – bulk delete up to 100 links
+export const DELETE = withWorkspace(
+ async ({ workspace, headers, searchParams }) => {
+ const linkIds = searchParams["linkIds"]?.split(",") || [];
+
+ if (linkIds.length === 0) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "Please provide linkIds as a comma-separated query parameter 'linkIds'.",
+ });
+ }
+
+ if (linkIds.length > 100) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "You can only delete up to 100 links at a time.",
+ });
+ }
+
+ const links = await prisma.link.findMany({
+ where: {
+ id: { in: linkIds },
+ projectId: workspace.id,
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ const deletedLinks = await Promise.all(
+ links.map(({ id }) => deleteLink(id)),
+ );
+
+ return NextResponse.json(
+ {
+ deletedCount: deletedLinks.length,
+ },
+ { headers },
+ );
+ },
+);
diff --git a/apps/web/lib/openapi/links/bulk-delete-links.ts b/apps/web/lib/openapi/links/bulk-delete-links.ts
new file mode 100644
index 0000000000..fcd8113216
--- /dev/null
+++ b/apps/web/lib/openapi/links/bulk-delete-links.ts
@@ -0,0 +1,25 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const bulkDeleteLinks: ZodOpenApiOperationObject = {
+ operationId: "bulkDeleteLinks",
+ "x-speakeasy-name-override": "deleteMany",
+ summary: "Bulk delete links",
+ description: "Bulk delete up to 100 links for the authenticated workspace.",
+ responses: {
+ "200": {
+ description: "The deleted links count.",
+ content: {
+ "application/json": {
+ schema: z.object({
+ deletedCount: z.number(),
+ }),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Links"],
+ security: [{ token: [] }],
+};
diff --git a/apps/web/lib/openapi/links/index.ts b/apps/web/lib/openapi/links/index.ts
index 7d52c2bd6d..296ca97756 100644
--- a/apps/web/lib/openapi/links/index.ts
+++ b/apps/web/lib/openapi/links/index.ts
@@ -1,6 +1,7 @@
import { ZodOpenApiPathsObject } from "zod-openapi";
import { bulkCreateLinks } from "./bulk-create-links";
+import { bulkDeleteLinks } from "./bulk-delete-links";
import { bulkUpdateLinks } from "./bulk-update-links";
import { createLink } from "./create-link";
import { deleteLink } from "./delete-link";
@@ -28,6 +29,7 @@ export const linksPaths: ZodOpenApiPathsObject = {
"/links/bulk": {
post: bulkCreateLinks,
patch: bulkUpdateLinks,
+ delete: bulkDeleteLinks,
},
"/links/upsert": {
put: upsertLink,