Skip to content

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

fix: subfolder descendent matching

Marc Seitz3bf03a1582af
diff --git a/lib/dataroom/build-folder-hierarchy.ts b/lib/dataroom/build-folder-hierarchy.ts
index 57411da0..e7046f68 100644
--- a/lib/dataroom/build-folder-hierarchy.ts
+++ b/lib/dataroom/build-folder-hierarchy.ts
@@ -54,6 +54,45 @@ export function buildFolderPathsFromHierarchy(
return pathCache;
}
+/**
+ * Collects all descendant folder IDs for a given root folder using
+ * the parentId hierarchy (BFS traversal).
+ *
+ * Use this instead of `path: { startsWith }` queries, which rely on
+ * the materialized `path` field that can become stale after renames/moves.
+ *
+ * @param rootId - The ID of the root folder
+ * @param folders - All folders to search through (must include at least the subtree)
+ * @returns Set of descendant folder IDs (does NOT include rootId itself)
+ */
+export function collectDescendantIds(
+ rootId: string,
+ folders: { id: string; parentId: string | null }[],
+): Set<string> {
+ // Build a children lookup map
+ const childrenMap = new Map<string, string[]>();
+ for (const folder of folders) {
+ if (folder.parentId) {
+ const siblings = childrenMap.get(folder.parentId) ?? [];
+ siblings.push(folder.id);
+ childrenMap.set(folder.parentId, siblings);
+ }
+ }
+
+ // BFS from rootId
+ const descendants = new Set<string>();
+ const queue = childrenMap.get(rootId) ?? [];
+ while (queue.length > 0) {
+ const current = queue.shift()!;
+ if (descendants.has(current)) continue; // guard against cycles
+ descendants.add(current);
+ const children = childrenMap.get(current) ?? [];
+ queue.push(...children);
+ }
+
+ return descendants;
+}
+
/**
* Builds a folder name map from computed paths.
* Maps computedPath -> { name, id } for looking up display names.
diff --git a/pages/api/links/download/dataroom-folder.ts b/pages/api/links/download/dataroom-folder.ts
index feaf5315..025927f1 100644
--- a/pages/api/links/download/dataroom-folder.ts
+++ b/pages/api/links/download/dataroom-folder.ts
@@ -7,6 +7,7 @@ import slugify from "@sindresorhus/slugify";
import { verifyDataroomSessionInPagesRouter } from "@/lib/auth/dataroom-auth";
import {
buildFolderPathsFromHierarchy,
+ collectDescendantIds,
} from "@/lib/dataroom/build-folder-hierarchy";
import { notifyDocumentDownload } from "@/lib/integrations/slack/events";
import prisma from "@/lib/prisma";
@@ -138,15 +139,18 @@ export default async function handler(
return res.status(404).json({ error: "Folder not found" });
}
- // Find all subfolders using path prefix (to get the full subtree)
- const subfolders = await prisma.dataroomFolder.findMany({
- where: {
- dataroomId,
- path: { startsWith: rootFolder.path + "/" },
- },
+ // Fetch all folders in this dataroom so we can traverse the parentId
+ // hierarchy. This avoids relying on the materialized `path` field which
+ // can become stale after renames/moves.
+ const allDataroomFolders = await prisma.dataroomFolder.findMany({
+ where: { dataroomId },
select: { id: true, name: true, path: true, parentId: true },
});
+ // Collect descendants via parentId chain (source of truth)
+ const descendantIds = collectDescendantIds(rootFolder.id, allDataroomFolders);
+ const subfolders = allDataroomFolders.filter((f) => descendantIds.has(f.id));
+
let allFolders = [rootFolder, ...subfolders];
let allDocuments = await prisma.dataroomDocument.findMany({
where: {