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.

bulk delete links

Kiran Ka915da5ce965
diff --git a/apps/web/app/api/cron/cleanup/route.ts b/apps/web/app/api/cron/cleanup/route.ts
index 965dc20752..6af4e452e6 100644
--- a/apps/web/app/api/cron/cleanup/route.ts
+++ b/apps/web/app/api/cron/cleanup/route.ts
@@ -63,6 +63,7 @@ export async function GET(req: Request) {
// Post delete cleanup
await bulkDeleteLinks({
links,
+ workspaceId: E2E_WORKSPACE_ID,
});
}
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 4cbd33a53b..fb606552a5 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -414,6 +414,7 @@ export const DELETE = withWorkspace(
waitUntil(
bulkDeleteLinks({
links,
+ workspaceId: workspace.id,
}),
);
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.ts
index cb794fc616..61240c5a02 100644
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ b/apps/web/lib/api/links/bulk-delete-links.ts
@@ -1,3 +1,4 @@
+import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
@@ -6,8 +7,10 @@ import { Link, Tag } from "@prisma/client";
export async function bulkDeleteLinks({
links,
+ workspaceId,
}: {
links: (Link & { tags: Pick<Tag, "id">[] })[];
+ workspaceId: string;
}) {
if (links.length === 0) {
return;
@@ -16,7 +19,7 @@ export async function bulkDeleteLinks({
const pipeline = redis.pipeline();
links.forEach((link) => {
- pipeline.hdel(link.domain.toLowerCase(), link.key.toLowerCase());
+ pipeline.del(`${link.domain}:${link.key}`.toLowerCase());
});
return await Promise.all([
@@ -38,9 +41,20 @@ export async function bulkDeleteLinks({
),
// For links that have an image, delete the image from R2
- // TODO: How do we optimize this?
links
.filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))
.map((link) => storage.delete(link.image!.replace(`${R2_URL}/`, ""))),
+
+ // Decrement the links count for the workspace
+ prisma.project.update({
+ where: {
+ id: workspaceId,
+ },
+ data: {
+ linksUsage: {
+ decrement: links.length,
+ },
+ },
+ }),
]);
}