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.

Refactor bulk link deletion folder permissions check

Kiran K8e21df11c4c5
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 7aaf1f4659..6be89e5709 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -14,7 +14,6 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import {
checkFolderPermissions,
- hasFolderPermission,
verifyFolderAccess,
} from "@/lib/folder/permissions";
import { storage } from "@/lib/storage";
@@ -465,36 +464,32 @@ export const DELETE = withWorkspace(
},
});
- const uniqueFolderIds = [
- ...new Set(
- links
- .map((link) => link.folderId)
- .filter((folderId): folderId is string => folderId !== null),
- ),
- ];
-
- let folderAccess: Record<string, boolean> = {};
-
- if (uniqueFolderIds.length > 0) {
- folderAccess = Object.fromEntries(
- await Promise.all(
- uniqueFolderIds.map(async (folderId) => [
- folderId,
- await hasFolderPermission({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId,
- requiredPermission: "folders.links.write",
- }),
- ]),
+ if (checkIfLinksHaveFolders(links)) {
+ const folderIds = [
+ ...new Set(
+ links.map((link) => link.folderId).filter(Boolean) as string[],
),
- );
- }
+ ];
- // filter links that the user has access to
- links = links.filter((link) => {
- return link.folderId ? folderAccess[link.folderId] : true;
- });
+ const folderPermissions = await checkFolderPermissions({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderIds,
+ requiredPermission: "folders.links.write",
+ });
+
+ links = links.filter((link) => {
+ if (!link.folderId) {
+ return true;
+ }
+
+ const validFolder = folderPermissions.find(
+ (folder) => folder.folderId === link.folderId,
+ );
+
+ return validFolder?.hasPermission ?? false;
+ });
+ }
const { count: deletedCount } = await prisma.link.deleteMany({
where: {
diff --git a/apps/web/lib/api/links/utils/check-if-links-have-folders.ts b/apps/web/lib/api/links/utils/check-if-links-have-folders.ts
index e56c319071..f628f0f035 100644
--- a/apps/web/lib/api/links/utils/check-if-links-have-folders.ts
+++ b/apps/web/lib/api/links/utils/check-if-links-have-folders.ts
@@ -1,4 +1,3 @@
-import { ProcessedLinkProps } from "../../../types";
-
-export const checkIfLinksHaveFolders = (links: ProcessedLinkProps[]) =>
- links.some((link) => link.folderId);
+export const checkIfLinksHaveFolders = (
+ links: { folderId?: string | null }[],
+) => links.some((link) => link.folderId);