Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: update build hierarchy before filtering

Marc Seitz60c5412bdf2f
diff --git a/pages/api/links/download/bulk.ts b/pages/api/links/download/bulk.ts
index ea0411e6..54fab7a4 100644
--- a/pages/api/links/download/bulk.ts
+++ b/pages/api/links/download/bulk.ts
@@ -178,7 +178,15 @@ export default async function handle(
return res.status(403).json({ error: "Error downloading" });
}
- let downloadFolders = view.dataroom.folders;
+ // Build folder paths from the FULL folder list (source of truth) BEFORE
+ // permission filtering. This ensures parent folders that only have canView
+ // (not canDownload) are still included in the hierarchy so child paths are
+ // computed correctly (e.g. "/legal/contracts" instead of just "/contracts").
+ const allFolders = view.dataroom.folders;
+ const computedPathMap = buildFolderPathsFromHierarchy(allFolders);
+ const folderMap = buildFolderNameMap(allFolders, computedPathMap);
+
+ let downloadFolders = allFolders;
let downloadDocuments = view.dataroom.documents;
// Check permissions based on groupId (ViewerGroup) or permissionGroupId (PermissionGroup)
@@ -267,12 +275,6 @@ export default async function handle(
skipDuplicates: true,
});
- // Build folder paths from the parentId hierarchy (source of truth)
- // instead of using the materialized `path` field which can become stale
- // after folder renames/moves
- const computedPathMap = buildFolderPathsFromHierarchy(downloadFolders);
- const folderMap = buildFolderNameMap(downloadFolders, computedPathMap);
-
// Construct folderStructure and fileKeys
const folderStructure: {
[key: string]: {
diff --git a/pages/api/links/download/dataroom-folder.ts b/pages/api/links/download/dataroom-folder.ts
index 9923daf5..801adf25 100644
--- a/pages/api/links/download/dataroom-folder.ts
+++ b/pages/api/links/download/dataroom-folder.ts
@@ -151,7 +151,11 @@ export default async function handler(
const descendantIds = collectDescendantIds(rootFolder.id, allDataroomFolders);
const subfolders = allDataroomFolders.filter((f) => descendantIds.has(f.id));
- let allFolders = [rootFolder, ...subfolders];
+ // Keep the full (unfiltered) folder list for path computation below.
+ // Permission filtering may remove parent folders that only have canView
+ // (not canDownload), which would break child path computation.
+ const fullFolders = [rootFolder, ...subfolders];
+ let allFolders = [...fullFolders];
let allDocuments = await prisma.dataroomDocument.findMany({
where: {
dataroomId,
@@ -224,9 +228,11 @@ export default async function handler(
);
}
- // Build folder paths from the parentId hierarchy (source of truth)
- // This ensures the download structure matches what the UI shows
- const computedPathMap = buildFolderPathsFromHierarchy(allFolders);
+ // Build folder paths from the FULL (unfiltered) folder hierarchy so that
+ // parent folders removed by permission filtering are still present in the
+ // path map, producing correct child paths (e.g. "/legal/contracts" instead
+ // of just "/contracts").
+ const computedPathMap = buildFolderPathsFromHierarchy(fullFolders);
// Compute the root folder's path from the hierarchy
const computedRootPath = computedPathMap.get(rootFolder.id) ?? rootFolder.path;