Skip to content

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

feat: seed dataroom folders from excel ecf0

Marc Seitzfbbfa6969e4e
diff --git a/components/documents/add-document-dropdown.tsx b/components/documents/add-document-dropdown.tsx
index 2f74e40d..ce58f387 100644
--- a/components/documents/add-document-dropdown.tsx
+++ b/components/documents/add-document-dropdown.tsx
@@ -8,6 +8,7 @@ import {
FolderUpIcon,
LinkIcon,
PlusIcon,
+ TablePropertiesIcon,
} from "lucide-react";
import { useHotkeys } from "react-hotkeys-hook";
import { toast } from "sonner";
@@ -20,6 +21,7 @@ import { cn } from "@/lib/utils";
import { UpgradePlanModal } from "@/components/billing/upgrade-plan-modal";
import { AddDocumentModal } from "@/components/documents/add-document-modal";
import { AddFolderModal } from "@/components/folders/add-folder-modal";
+import { ImportFoldersModal } from "@/components/folders/import-folders-modal";
import NotionIcon from "@/components/shared/icons/files/notion";
import { Button } from "@/components/ui/button";
import {
@@ -61,6 +63,7 @@ export function AddDocumentDropdown({
const [menuOpen, setMenuOpen] = useState(false);
const [folderOpen, setFolderOpen] = useState(false);
+ const [importFoldersOpen, setImportFoldersOpen] = useState(false);
const [docModal, setDocModal] = useState<DocModalState>({
open: false,
defaultTab: "document",
@@ -101,6 +104,18 @@ export function AddDocumentDropdown({
setFolderOpen(true);
}, [requiresUpgradeForFolders]);
+ const handleImportFolders = useCallback(() => {
+ if (requiresUpgradeForFolders) {
+ setUpgradeModal({
+ open: true,
+ trigger: "import_folders_button",
+ highlight: ["folder", "folder-sharing", "datarooms"],
+ });
+ return;
+ }
+ setImportFoldersOpen(true);
+ }, [requiresUpgradeForFolders]);
+
const handleUploadFiles = useCallback(() => {
if (!enforceDocumentLimit()) return;
if (uploadTriggers) {
@@ -293,6 +308,22 @@ export function AddDocumentDropdown({
</DropdownMenuShortcut>
</DropdownMenuItem>
+ {isDataroom && dataroomId ? (
+ <DropdownMenuItem
+ onSelect={(e) => {
+ e.preventDefault();
+ setMenuOpen(false);
+ handleImportFolders();
+ }}
+ >
+ <TablePropertiesIcon
+ className="h-4 w-4 -scale-x-100"
+ aria-hidden="true"
+ />
+ <span>Import folders from Excel</span>
+ </DropdownMenuItem>
+ ) : null}
+
<DropdownMenuSeparator />
<DropdownMenuLabel className="text-xs font-medium uppercase tracking-wider text-muted-foreground">
Upload from computer
@@ -358,6 +389,14 @@ export function AddDocumentDropdown({
dataroomId={dataroomId}
/>
+ {isDataroom && dataroomId ? (
+ <ImportFoldersModal
+ open={importFoldersOpen}
+ setOpen={setImportFoldersOpen}
+ dataroomId={dataroomId}
+ />
+ ) : null}
+
<AddDocumentModal
key={docModalKey}
openModal={docModal.open}
diff --git a/components/folders/import-folders-modal.tsx b/components/folders/import-folders-modal.tsx
new file mode 100644
index 00000000..13bd31d1
--- /dev/null
+++ b/components/folders/import-folders-modal.tsx
@@ -0,0 +1,449 @@
+import { useRouter } from "next/router";
+
+import { useCallback, useMemo, useRef, useState } from "react";
+
+import { useTeam } from "@/context/team-context";
+import {
+ AlertCircleIcon,
+ DownloadIcon,
+ FileSpreadsheetIcon,
+ FolderIcon,
+ Loader2Icon,
+ UploadCloudIcon,
+} from "lucide-react";
+import { toast } from "sonner";
+import { mutate } from "swr";
+
+import { useAnalytics } from "@/lib/analytics";
+import { bulkCreateFolders } from "@/lib/folders/create-folder";
+import {
+ type ParsedFolderRow,
+ type SkippedFolderRow,
+ parseFoldersFromFile,
+ toBulkFolderItems,
+} from "@/lib/folders/parse-folder-import";
+import { downloadFileFromApi } from "@/lib/utils/download-from-api";
+
+import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+
+const MAX_FILE_SIZE_BYTES = 5 * 1024 * 1024; // 5 MB
+
+function isSpreadsheet(file: File): boolean {
+ const name = file.name.toLowerCase();
+ return (
+ name.endsWith(".xlsx") ||
+ name.endsWith(".xls") ||
+ name.endsWith(".csv") ||
+ file.type ===
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
+ file.type === "application/vnd.ms-excel" ||
+ file.type === "text/csv"
+ );
+}
+
+/** Group skipped rows by reason, e.g. `Duplicate hierarchy code "3" (row 15)`. */
+function summarizeSkippedRows(skipped: SkippedFolderRow[]): string[] {
+ const rowsByReason = new Map<string, number[]>();
+ for (const { reason, row } of skipped) {
+ const bucket = rowsByReason.get(reason) ?? [];
+ bucket.push(row);
+ rowsByReason.set(reason, bucket);
+ }
+ return Array.from(
+ rowsByReason,
+ ([reason, rows]) =>
+ `${reason} (row${rows.length === 1 ? "" : "s"} ${rows.join(", ")})`,
+ );
+}
+
+export function ImportFoldersModal({
+ open,
+ setOpen,
+ dataroomId,
+ onImported,
+}: {
+ open: boolean;
+ setOpen: (open: boolean) => void;
+ dataroomId: string;
+ onImported?: () => void;
+}) {
+ const router = useRouter();
+ const teamInfo = useTeam();
+ const analytics = useAnalytics();
+ const teamId = teamInfo?.currentTeam?.id;
+
+ const fileInputRef = useRef<HTMLInputElement>(null);
+ const [isDownloadingTemplate, setIsDownloadingTemplate] = useState(false);
+ const [fileName, setFileName] = useState<string | null>(null);
+ const [rows, setRows] = useState<ParsedFolderRow[]>([]);
+ const [skipped, setSkipped] = useState<SkippedFolderRow[]>([]);
+ const [parseError, setParseError] = useState<string | null>(null);
+ const [isParsing, setIsParsing] = useState(false);
+ const [isSubmitting, setIsSubmitting] = useState(false);
+ const [isDragging, setIsDragging] = useState(false);
+
+ const skippedReasons = useMemo(
+ () => summarizeSkippedRows(skipped),
+ [skipped],
+ );
+
+ // Import under the folder the user is currently viewing, mirroring how the
+ // "Create folder" modal reads the current dataroom path from the route.
+ const currentFolderPath = router.query.name as string[] | undefined;
+ const rootPath =
+ currentFolderPath && currentFolderPath.length > 0
+ ? "/" + currentFolderPath.join("/")
+ : "/";
+
+ const reset = useCallback(() => {
+ setFileName(null);
+ setRows([]);
+ setSkipped([]);
+ setParseError(null);
+ setIsParsing(false);
+ if (fileInputRef.current) fileInputRef.current.value = "";
+ }, []);
+
+ const handleClose = useCallback(
+ (next: boolean) => {
+ if (!next && !isSubmitting) reset();
+ if (!isSubmitting) setOpen(next);
+ },
+ [isSubmitting, reset, setOpen],
+ );
+
+ const handleFile = useCallback(async (file: File | undefined | null) => {
+ if (!file) return;
+ setParseError(null);
+ setRows([]);
+ setSkipped([]);
+
+ if (!isSpreadsheet(file)) {
+ setParseError("Please choose an .xlsx, .xls, or .csv file.");
+ return;
+ }
+ if (file.size > MAX_FILE_SIZE_BYTES) {
+ setParseError(
+ "File is larger than 5 MB. Please split it into smaller files.",
+ );
+ return;
+ }
+
+ setIsParsing(true);
+ try {
+ const result = await parseFoldersFromFile(file);
+ if (result.rows.length === 0) {
+ setParseError(
+ "No folders found. Each row needs at least a folder name.",
+ );
+ setFileName(file.name);
+ setSkipped(result.skipped);
+ return;
+ }
+ setRows(result.rows);
+ setSkipped(result.skipped);
+ setFileName(file.name);
+ } catch (error) {
+ setParseError(
+ error instanceof Error
+ ? error.message
+ : "Failed to read the file. Please check the format.",
+ );
+ } finally {
+ setIsParsing(false);
+ }
+ }, []);
+
+ const handleDownloadTemplate = useCallback(async () => {
+ if (!teamId) return;
+ setIsDownloadingTemplate(true);
+ try {
+ await downloadFileFromApi(
+ `/api/teams/${teamId}/datarooms/${dataroomId}/folders/import-template`,
+ "folders_import_template.xlsx",
+ );
+ } catch (error) {
+ toast.error((error as Error).message || "Failed to download template");
+ } finally {
+ setIsDownloadingTemplate(false);
+ }
+ }, [dataroomId, teamId]);
+
+ const handleDragOver = useCallback((event: React.DragEvent<HTMLElement>) => {
+ event.preventDefault();
+ event.stopPropagation();
+ if (event.dataTransfer) event.dataTransfer.dropEffect = "copy";
+ setIsDragging(true);
+ }, []);
+
+ const handleDragLeave = useCallback((event: React.DragEvent<HTMLElement>) => {
+ event.preventDefault();
+ event.stopPropagation();
+ setIsDragging(false);
+ }, []);
+
+ const handleDrop = useCallback(
+ (event: React.DragEvent<HTMLElement>) => {
+ event.preventDefault();
+ event.stopPropagation();
+ setIsDragging(false);
+ void handleFile(event.dataTransfer?.files?.[0]);
+ },
+ [handleFile],
+ );
+
+ const revalidateDataroomFolders = useCallback(() => {
+ if (!teamId) return;
+ // Broad one-shot revalidation of every cached folder/document key for this
+ // dataroom, matching the bulk folder-upload flow in upload-zone.tsx.
+ mutate(
+ (key: unknown) =>
+ typeof key === "string" &&
+ (key.startsWith(
+ `/api/teams/${teamId}/datarooms/${dataroomId}/folders`,
+ ) ||
+ key.startsWith(
+ `/api/teams/${teamId}/datarooms/${dataroomId}/folder-documents`,
+ ) ||
+ key.startsWith(
+ `/api/teams/${teamId}/datarooms/${dataroomId}/documents`,
+ )),
+ );
+ }, [dataroomId, teamId]);
+
+ const handleSubmit = useCallback(async () => {
+ if (rows.length === 0 || !teamId) return;
+ setIsSubmitting(true);
+ try {
+ // Parsing caps the sheet at MAX_IMPORT_FOLDERS, which is the server's
+ // per-request limit, so the whole import is always one transaction.
+ const created = await bulkCreateFolders({
+ url: `/api/teams/${teamId}/datarooms/${dataroomId}/folders/bulk`,
+ rootPath,
+ folders: toBulkFolderItems(rows),
+ });
+
+ analytics.capture("Folder Added (bulk import)", {
+ count: created.length,
+ dataroomId,
+ });
+
+ revalidateDataroomFolders();
+ toast.success(
+ created.length === 1
+ ? "Imported 1 folder"
+ : `Imported ${created.length} folders`,
+ );
+ reset();
+ setOpen(false);
+ onImported?.();
+ } catch (error) {
+ toast.error((error as Error).message || "Failed to import folders");
+ } finally {
+ setIsSubmitting(false);
+ }
+ }, [
+ analytics,
+ dataroomId,
+ onImported,
+ reset,
+ revalidateDataroomFolders,
+ rootPath,
+ rows,
+ setOpen,
+ teamId,
+ ]);
+
+ return (
+ <Dialog open={open} onOpenChange={handleClose}>
+ <DialogContent className="sm:max-w-2xl">
+ <DialogHeader>
+ <DialogTitle>Import folders from Excel</DialogTitle>
+ <DialogDescription>
+ Upload an Excel (.xlsx) or CSV file to create many folders at once.
+ Use one column for the hierarchy (e.g. 1, 1.1, 1.2) and one for the
+ folder name.
+ </DialogDescription>
+ </DialogHeader>
+
+ <div className="flex flex-col gap-4">
+ <div className="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-muted/40 px-3 py-2 text-sm">
+ <div className="flex items-center gap-2">
+ <FileSpreadsheetIcon className="h-4 w-4 text-muted-foreground" />
+ <span className="text-muted-foreground">
+ Need a starting point?
+ </span>
+ </div>
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ onClick={() => void handleDownloadTemplate()}
+ loading={isDownloadingTemplate}
+ >
+ <DownloadIcon className="mr-2 h-4 w-4" />
+ Download template
+ </Button>
+ </div>
+
+ <input
+ ref={fileInputRef}
+ type="file"
+ accept=".xlsx,.xls,.csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,text/csv"
+ className="hidden"
+ onChange={(event) => {
+ void handleFile(event.target.files?.[0]);
+ event.target.value = "";
+ }}
+ />
+
+ <button
+ type="button"
+ onClick={() => fileInputRef.current?.click()}
+ onDragEnter={handleDragOver}
+ onDragOver={handleDragOver}
+ onDragLeave={handleDragLeave}
+ onDrop={handleDrop}
+ className={`flex flex-col items-center justify-center gap-2 rounded-md border border-dashed px-4 py-8 text-center transition-colors hover:border-foreground/40 hover:bg-muted/40 ${
+ isDragging ? "border-foreground/60 bg-muted/60" : "border-input"
+ }`}
+ >
+ {isParsing ? (
+ <Loader2Icon className="h-6 w-6 animate-spin text-muted-foreground" />
+ ) : (
+ <UploadCloudIcon className="h-6 w-6 text-muted-foreground" />
+ )}
+ <div className="text-sm">
+ {fileName ? (
+ <>
+ <span className="font-medium">{fileName}</span>
+ {rows.length > 0 ? (
+ <span className="ml-1 text-muted-foreground">
+ ({rows.length} folder{rows.length === 1 ? "" : "s"})
+ </span>
+ ) : null}
+ </>
+ ) : (
+ <>
+ <span className="font-medium text-foreground">
+ Click to choose a file
+ </span>
+ <span className="ml-1 text-muted-foreground">
+ or drag &amp; drop (.xlsx, .xls, .csv — max 5 MB)
+ </span>
+ </>
+ )}
+ </div>
+ </button>
+
+ {rootPath !== "/" ? (
+ <p className="text-xs text-muted-foreground">
+ Folders will be created inside{" "}
+ <span className="font-medium text-foreground">{rootPath}</span>.
+ </p>
+ ) : null}
+
+ {parseError ? (
+ <div className="flex items-start gap-2 rounded-md border border-destructive/40 bg-destructive/10 px-3 py-2 text-sm text-destructive">
+ <AlertCircleIcon className="mt-0.5 h-4 w-4 shrink-0" />
+ <span>{parseError}</span>
+ </div>
+ ) : null}
+
+ {skipped.length > 0 ? (
+ <div className="flex items-start gap-2 rounded-md border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-sm text-amber-700 dark:text-amber-400">
+ <AlertCircleIcon className="mt-0.5 h-4 w-4 shrink-0" />
+ <div>
+ <p>
+ Skipping {skipped.length} row
+ {skipped.length === 1 ? "" : "s"}:
+ </p>
+ <ul className="ml-4 list-disc">
+ {skippedReasons.map((reason) => (
+ <li key={reason}>{reason}</li>
+ ))}
+ </ul>
+ </div>
+ </div>
+ ) : null}
+
+ {rows.length > 0 ? <PreviewTree rows={rows} /> : null}
+ </div>
+
+ <DialogFooter className="mt-2">
+ <Button
+ type="button"
+ variant="outline"
+ onClick={() => handleClose(false)}
+ disabled={isSubmitting}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="button"
+ onClick={handleSubmit}
+ disabled={isSubmitting || rows.length === 0 || isParsing}
+ >
+ {isSubmitting ? (
+ <>
+ <Loader2Icon className="mr-2 h-4 w-4 animate-spin" />
+ Importing…
+ </>
+ ) : rows.length === 0 ? (
+ "Import folders"
+ ) : (
+ `Import ${rows.length} folder${rows.length === 1 ? "" : "s"}`
+ )}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
+
+function PreviewTree({ rows }: { rows: ParsedFolderRow[] }) {
+ const preview = useMemo(() => rows.slice(0, 100), [rows]);
+ return (
+ <div className="rounded-md border">
+ <div className="flex items-center justify-between border-b bg-muted/40 px-3 py-2 text-xs uppercase tracking-wide text-muted-foreground">
+ <span>Preview</span>
+ <span>
+ {rows.length} folder{rows.length === 1 ? "" : "s"}
+ </span>
+ </div>
+ <div className="max-h-60 overflow-y-auto">
+ <ul className="divide-y text-sm">
+ {preview.map((row) => (
+ <li key={row.tempId} className="flex items-center gap-2 px-3 py-2">
+ <span
+ className="flex min-w-0 items-center gap-2"
+ style={{ paddingLeft: `${Math.min(row.depth, 8) * 16}px` }}
+ >
+ <FolderIcon className="h-4 w-4 shrink-0 text-muted-foreground" />
+ <span className="truncate font-medium">{row.name}</span>
+ </span>
+ {row.hierarchy ? (
+ <span className="ml-auto shrink-0 text-xs text-muted-foreground">
+ {row.hierarchy}
+ </span>
+ ) : null}
+ </li>
+ ))}
+ </ul>
+ {rows.length > preview.length ? (
+ <div className="border-t px-3 py-2 text-center text-xs text-muted-foreground">
+ + {rows.length - preview.length} more
+ </div>
+ ) : null}
+ </div>
+ </div>
+ );
+}
diff --git a/lib/folders/bulk-create.ts b/lib/folders/bulk-create.ts
index 39e2377b..c67bdec9 100644
--- a/lib/folders/bulk-create.ts
+++ b/lib/folders/bulk-create.ts
@@ -91,6 +91,15 @@ export interface BulkFolderInput {
* Ignored if `parentTempId` is set.
*/
parentPath?: string | null;
+ /**
+ * Position among this folder's siblings, counting from 0. Written straight
+ * to DataroomFolder.orderIndex, so callers that import into a parent which
+ * already has ordered children must offset first (see
+ * `appendRootChildrenAfterExisting`). Ignored by the all-documents Folder
+ * model, which has no orderIndex column. Null leaves orderIndex null, which
+ * sorts the folder after every numbered sibling.
+ */
+ orderIndex?: number | null;
}
export interface BulkFolderResult {
@@ -273,10 +282,15 @@ async function resolveExternalParentPaths(
}
/**
- * Resolves slug collisions for an entire level in a single SELECT instead of
- * up to MAX_SLUG_SUFFIX sequential findUniques per folder.
+ * Assigns every folder at one tree level its final, collision-free path.
+ *
+ * Asking for all MAX_SLUG_SUFFIX fallbacks up front costs 51 bind parameters
+ * per folder, which puts a 500-folder level over 25k parameters and near
+ * Postgres' 65535 limit. Almost no import actually collides, so this asks for
+ * the plain slugs first and only pays for fallbacks on the folders that can
+ * collide with something.
*
- * Returns the resolved final path per tempId.
+ * Returns the resolved name and path per tempId.
*/
async function resolveLevelPaths(args: {
level: BulkFolderInput[];
@@ -291,30 +305,69 @@ async function resolveLevelPaths(args: {
const candidatesPerFolder: {
tempId: string;
name: string;
+ basePathPrefix: string;
basePath: string;
candidates: string[];
}[] = [];
- const allCandidates = new Set<string>();
+ const indicesByBasePath = new Map<string, number[]>();
- for (const f of level) {
+ for (const [index, f] of level.entries()) {
const parentPath = parentPathOf(f);
- const basePath = parentPath === "/" ? "/" : parentPath + "/";
- const slug = safeSlugify(f.name);
- const candidates: string[] = [basePath + slug];
- for (let i = 1; i <= MAX_SLUG_SUFFIX; i++) {
- candidates.push(basePath + safeSlugify(`${f.name} (${i})`));
- }
+ const basePathPrefix = parentPath === "/" ? "/" : parentPath + "/";
+ const basePath = basePathPrefix + safeSlugify(f.name);
candidatesPerFolder.push({
tempId: f.tempId,
name: f.name,
+ basePathPrefix,
basePath,
- candidates,
+ candidates: [basePath],
});
- for (const c of candidates) allCandidates.add(c);
+ const siblings = indicesByBasePath.get(basePath);
+ if (siblings) siblings.push(index);
+ else indicesByBasePath.set(basePath, [index]);
}
- const existingRows = await findExisting(Array.from(allCandidates));
- const taken = new Set(existingRows.map((r) => r.path));
+ const taken = new Set(
+ (await findExisting(Array.from(indicesByBasePath.keys()))).map(
+ (r) => r.path,
+ ),
+ );
+
+ const needsSuffixes = new Set<number>();
+ const pending: number[] = [];
+ const markContested = (index: number) => {
+ if (needsSuffixes.has(index)) return;
+ needsSuffixes.add(index);
+ pending.push(index);
+ };
+
+ for (const [basePath, indices] of indicesByBasePath) {
+ // Only the first of N same-named siblings can keep the base path.
+ if (taken.has(basePath) || indices.length > 1) {
+ indices.forEach(markContested);
+ }
+ }
+
+ // A folder literally named "Foo (1)" is uncontested on its own, but becomes
+ // contested once some "Foo" in the same batch may claim that exact path.
+ const suffixCandidates = new Set<string>();
+ while (pending.length > 0) {
+ const entry = candidatesPerFolder[pending.pop()!];
+ for (let i = 1; i <= MAX_SLUG_SUFFIX; i++) {
+ const candidate =
+ entry.basePathPrefix + safeSlugify(`${entry.name} (${i})`);
+ entry.candidates.push(candidate);
+ if (suffixCandidates.has(candidate)) continue;
+ suffixCandidates.add(candidate);
+ indicesByBasePath.get(candidate)?.forEach(markContested);
+ }
+ }
+
+ if (suffixCandidates.size > 0) {
+ for (const row of await findExisting(Array.from(suffixCandidates))) {
+ taken.add(row.path);
+ }
+ }
const resolved = new Map<string, { name: string; path: string }>();
for (const entry of candidatesPerFolder) {
@@ -331,11 +384,10 @@ async function resolveLevelPaths(args: {
if (!pickedPath || !pickedName) {
throw new BulkValidationError(
"SLUG_EXHAUSTED",
- `Could not find a free slug for "${entry.name}" under ${entry.basePath}`,
+ `Could not find a free slug for "${entry.name}" under ${entry.basePathPrefix}`,
);
}
- // Reserve the path so subsequent folders at the same level don't collide
- // with it (siblings dropped with identical names within the same batch).
+ // The batch's own rows aren't in the DB yet, so reserve as we go.
taken.add(pickedPath);
resolved.set(entry.tempId, { name: pickedName, path: pickedPath });
}
@@ -452,6 +504,59 @@ export async function bulkCreateMainDocsFolders(args: {
// Dataroom (DataroomFolder) bulk creator
// ---------------------------------------------------------------------------
+/**
+ * Shifts request-relative sibling positions past whatever already sits under
+ * the request root, so an import appends instead of interleaving with items
+ * the user arranged by hand. Only folders landing directly under `rootPath`
+ * can have pre-existing siblings; everything deeper gets a parent this same
+ * request creates.
+ */
+async function appendRootChildrenAfterExisting(args: {
+ tx: Prisma.TransactionClient;
+ dataroomId: string;
+ rootParentId: string | null;
+ folders: BulkFolderInput[];
+}): Promise<BulkFolderInput[]> {
+ const { tx, dataroomId, rootParentId, folders } = args;
+ const isRootChild = (f: BulkFolderInput) => !f.parentTempId && !f.parentPath;
+
+ if (!folders.some((f) => isRootChild(f) && f.orderIndex != null)) {
+ return folders;
+ }
+
+ // Serialize sibling-order reads for this parent so concurrent imports cannot
+ // compute the same offset. Held until the surrounding transaction commits,
+ // covering the inserts that consume the offset below.
+ await tx.$executeRaw`
+ SELECT pg_advisory_xact_lock(
+ hashtext(${`bulk-folder-order:${dataroomId}:${rootParentId ?? ""}`})
+ )
+ `;
+
+ const [folderMax, documentMax] = await Promise.all([
+ tx.dataroomFolder.aggregate({
+ where: { dataroomId, parentId: rootParentId },
+ _max: { orderIndex: true },
+ }),
+ tx.dataroomDocument.aggregate({
+ where: { dataroomId, folderId: rootParentId },
+ _max: { orderIndex: true },
+ }),
+ ]);
+ const offset =
+ Math.max(
+ folderMax._max.orderIndex ?? -1,
+ documentMax._max.orderIndex ?? -1,
+ ) + 1;
+ if (offset === 0) return folders;
+
+ return folders.map((f) =>
+ isRootChild(f) && f.orderIndex != null
+ ? { ...f, orderIndex: f.orderIndex + offset }
+ : f,
+ );
+}
+
export async function bulkCreateDataroomFolders(args: {
tx: Prisma.TransactionClient;
dataroomId: string;
@@ -459,7 +564,13 @@ export async function bulkCreateDataroomFolders(args: {
rootParentId: string | null;
folders: BulkFolderInput[];
}): Promise<BulkFolderResult[]> {
- const { tx, dataroomId, rootPath, rootParentId, folders } = args;
+ const { tx, dataroomId, rootPath, rootParentId } = args;
+ const folders = await appendRootChildrenAfterExisting({
+ tx,
+ dataroomId,
+ rootParentId,
+ folders: args.folders,
+ });
const levels = buildLevels(folders);
if (levels.length === 0) return [];
@@ -505,16 +616,14 @@ export async function bulkCreateDataroomFolders(args: {
externalParentPathToId,
rootParentId,
);
- // orderIndex and hierarchicalIndex are intentionally left null.
- // Matches the existing single-folder POST behavior; null sorts last
- // so new folders append to the end of each parent. They can be
- // re-indexed via /api/teams/.../datarooms/[id]/calculate-indexes
- // when needed.
+ // hierarchicalIndex stays null; it is recomputed in bulk via
+ // /api/teams/.../datarooms/[id]/calculate-indexes.
return {
name: r.name,
path: r.path,
parentId,
dataroomId,
+ orderIndex: f.orderIndex ?? null,
};
});
diff --git a/lib/folders/create-folder.ts b/lib/folders/create-folder.ts
index 0f3f9aa1..ae8941f9 100644
--- a/lib/folders/create-folder.ts
+++ b/lib/folders/create-folder.ts
@@ -24,6 +24,13 @@ export interface BulkFolderRequestItem {
* looks this path up in DB to resolve the parent's id.
*/
parentPath?: string | null;
+ /**
+ * Position among the folder's siblings within this request, counting from
+ * 0. The dataroom bulk endpoint shifts these past whatever already sits
+ * under the parent and stores the result as orderIndex. Omitted/null leaves
+ * orderIndex null, which sorts the folder after every numbered sibling.
+ */
+ orderIndex?: number | null;
}
export interface BulkFolderResultItem {
@@ -175,6 +182,7 @@ export async function bulkCreateFoldersChunked(args: {
name: f.name,
parentTempId: null,
parentPath: parent.path,
+ orderIndex: f.orderIndex,
};
}
return f;
diff --git a/lib/folders/import-template.ts b/lib/folders/import-template.ts
new file mode 100644
index 00000000..7925fbe5
--- /dev/null
+++ b/lib/folders/import-template.ts
@@ -0,0 +1,123 @@
+import ExcelJS from "exceljs";
+
+// =============================================================================
+// Dataroom folder import — server-side Excel template generation (exceljs).
+//
+// Mirrors ee/features/request-lists/lib/export-generator.ts and
+// lib/dataroom/index-generator.ts: builds a styled .xlsx and returns
+// { data, filename, mimeType } for a route handler to stream as a download.
+// Parsing uploads stays on SheetJS (lib/folders/parse-folder-import.ts) — the
+// same "exceljs writes, sheetjs reads" split the codebase already uses.
+// =============================================================================
+
+const MIME_XLSX =
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
+
+// Columns the import parser understands (see parse-folder-import.ts aliases).
+const IMPORT_COLUMNS = [
+ { header: "Hierarchy", key: "hierarchy", width: 16 },
+ { header: "Name", key: "name", width: 48 },
+] as const;
+
+// Example tree that demonstrates nesting via the outline codes.
+const EXAMPLE_ROWS: [string, string][] = [
+ ["1", "Legal"],
+ ["1.1", "Corporate Documents"],
+ ["1.2", "Contracts"],
+ ["1.2.1", "Customer Contracts"],
+ ["1.2.2", "Supplier Contracts"],
+ ["2", "Financials"],
+ ["2.1", "Statements"],
+ ["2.1.1", "2023"],
+ ["2.1.2", "2024"],
+ ["3", "Commercial"],
+];
+
+const TITLE_STYLE = {
+ fill: {
+ type: "pattern" as const,
+ pattern: "solid" as const,
+ fgColor: { argb: "FF4F81BD" },
+ },
+ font: { color: { argb: "FFFFFFFF" }, bold: true, size: 14 },
+ alignment: { horizontal: "left" as const },
+};
+
+function safeFilename(suffix: string, generatedAt: Date): string {
+ const date = generatedAt
+ .toLocaleDateString("en-US", {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ })
+ .replace(/[^a-zA-Z0-9]/g, "_");
+ return `folders_${suffix}_${date}.xlsx`;
+}
+
+/**
+ * Build a blank folder-import template: two title rows, a styled header row,
+ * and an example tree the user can overwrite. Returns { data, filename,
+ * mimeType } for the route handler to stream.
+ */
+export async function generateFolderImportTemplate(): Promise<{
+ data: Uint8Array<ArrayBuffer>;
+ filename: string;
+ mimeType: string;
+}> {
+ const generatedAt = new Date();
+ const workbook = new ExcelJS.Workbook();
+ const worksheet = workbook.addWorksheet("Folders");
+
+ worksheet.columns = IMPORT_COLUMNS.map((column) => ({ ...column }));
+
+ // Insert title + subtitle + a spacer above the header row.
+ worksheet.spliceRows(
+ 1,
+ 0,
+ ["Dataroom folder import template"],
+ ['Use dotted outline codes (1, 1.1, 1.2) to nest folders. "Name" is required.'],
+ [],
+ );
+
+ worksheet.getRow(1).eachCell((cell) => {
+ cell.style = TITLE_STYLE;
+ });
+ worksheet.getRow(2).eachCell((cell) => {
+ cell.style = TITLE_STYLE;
+ });
+
+ const lastCol = worksheet.getColumn(IMPORT_COLUMNS.length).letter;
+ worksheet.mergeCells(`A1:${lastCol}1`);
+ worksheet.mergeCells(`A2:${lastCol}2`);
+
+ const headerRow = worksheet.getRow(4);
+ headerRow.font = { size: 11, bold: true };
+ headerRow.fill = {
+ type: "pattern",
+ pattern: "solid",
+ fgColor: { argb: "FFDCE6F1" },
+ };
+ headerRow.alignment = { horizontal: "left" };
+ headerRow.eachCell((cell) => {
+ cell.border = {
+ top: { style: "thin" },
+ bottom: { style: "thin" },
+ left: { style: "thin" },
+ right: { style: "thin" },
+ };
+ });
+
+ for (const [hierarchy, name] of EXAMPLE_ROWS) {
+ const row = worksheet.addRow([hierarchy, name]);
+ row.font = { italic: true, color: { argb: "FF808080" } };
+ }
+
+ const buffer = await workbook.xlsx.writeBuffer();
+ // Copy into a Uint8Array backed by a plain ArrayBuffer so it is a valid
+ // BodyInit for NextResponse / the Node response (see export-generator.ts).
+ return {
+ data: new Uint8Array(buffer as ArrayBuffer),
+ filename: safeFilename("import_template", generatedAt),
+ mimeType: MIME_XLSX,
+ };
+}
diff --git a/lib/folders/parse-folder-import.ts b/lib/folders/parse-folder-import.ts
new file mode 100644
index 00000000..55ec5467
--- /dev/null
+++ b/lib/folders/parse-folder-import.ts
@@ -0,0 +1,304 @@
+// =============================================================================
+// Dataroom folder import — client-side Excel/CSV parsing (SheetJS).
+//
+// Turns a two-column sheet (a hierarchy/outline column + a folder name column)
+// into a flat list of folders with parent references, ready to POST to the
+// bulk folder endpoint. `xlsx` (SheetJS) is imported dynamically so it stays
+// out of the main bundle until a user actually picks a file — the same split
+// the request-list import (ee/features/request-lists/lib/excel.ts) uses.
+// =============================================================================
+import type { BulkFolderRequestItem } from "./create-folder";
+
+/** A single folder parsed from the sheet, ready to preview and submit. */
+export type ParsedFolderRow = {
+ tempId: string;
+ name: string;
+ parentTempId: string | null;
+ /** Depth in the tree (0 = top level). Used only for preview indentation. */
+ depth: number;
+ /** The original hierarchy/outline code (e.g. "1.2"), for preview display. */
+ hierarchy: string;
+ /**
+ * Position among the folder's siblings, counting from 0 in the order the
+ * rows appear in the sheet. This is the same unit the drag-to-reorder
+ * endpoint writes to DataroomFolder.orderIndex, so imported folders end up
+ * in the order the preview shows.
+ */
+ orderIndex: number;
+};
+
+/** A row we couldn't import (e.g. a hierarchy code with no folder name). */
+export type SkippedFolderRow = {
+ /** 1-based spreadsheet row number (matches what users see in Excel). */
+ row: number;
+ reason: string;
+};
+
+export type FolderParseResult = {
+ rows: ParsedFolderRow[];
+ skipped: SkippedFolderRow[];
+};
+
+/** Mirrors MAX_BULK_FOLDERS_PER_REQUEST in lib/folders/bulk-create.ts. */
+export const MAX_IMPORT_FOLDERS = 500;
+
+const MAX_FOLDER_NAME_LENGTH = 255;
+
+// --- Header matching --------------------------------------------------------
+
+const HEADER_ALIASES = {
+ name: ["name", "folder", "folder name", "foldername", "title", "label"],
+ hierarchy: [
+ "hierarchy",
+ "level",
+ "outline",
+ "index",
+ "no",
+ "no.",
+ "number",
+ "#",
+ "id",
+ "order",
+ "structure",
+ ],
+} as const;
+
+type ColumnIndex = { name: number; hierarchy: number };
+
+function buildColumnIndex(headers: string[]): ColumnIndex {
+ const normalized = headers.map((h) =>
+ String(h ?? "")
+ .trim()
+ .toLowerCase(),
+ );
+ const find = (aliases: readonly string[]) =>
+ normalized.findIndex((h) => aliases.includes(h));
+ return {
+ name: find(HEADER_ALIASES.name),
+ hierarchy: find(HEADER_ALIASES.hierarchy),
+ };
+}
+
+// --- Value coercion ---------------------------------------------------------
+
+function cellToString(value: unknown): string {
+ if (value === null || value === undefined) return "";
+ if (value instanceof Date) return value.toISOString();
+ return String(value).trim();
+}
+
+/**
+ * Normalize a hierarchy/outline code into canonical dotted segments.
+ * Tolerates trailing separators and stray whitespace, and works for numeric
+ * ("1.2.3") as well as alphanumeric ("A.1") outlines. Returns the segment
+ * array; e.g. "1. 2 ." -> ["1", "2"].
+ */
+function hierarchySegments(raw: string): string[] {
+ return raw
+ .split(".")
+ .map((segment) => segment.trim())
+ .filter((segment) => segment.length > 0);
+}
+
+// --- Parse ------------------------------------------------------------------
+
+/**
+ * Parse an uploaded .xlsx/.xls/.csv into a folder tree.
+ *
+ * The sheet is expected to have a folder-name column and (optionally) a
+ * hierarchy/outline column such as:
+ *
+ * Hierarchy | Name
+ * 1 | Legal
+ * 1.1 | Contracts
+ * 1.2 | Corporate
+ * 2 | Financials
+ *
+ * A folder's parent is the row whose hierarchy code is the current code with
+ * its last segment removed ("1.2" -> parent "1"). If an intermediate code is
+ * missing we walk further up the outline, falling back to the top level. When
+ * no hierarchy column is present every folder is created at the top level.
+ */
+export async function parseFoldersFromFile(
+ file: File,
+): Promise<FolderParseResult> {
+ const XLSX = await import("xlsx");
+
+ const buffer = await file.arrayBuffer();
+ const workbook = XLSX.read(new Uint8Array(buffer), {
+ type: "array",
+ cellDates: true,
+ });
+
+ const sheetName = workbook.SheetNames[0];
+ if (!sheetName) {
+ throw new Error("The file does not contain any sheets.");
+ }
+ const worksheet = workbook.Sheets[sheetName];
+
+ // Keep blank rows so skipped-row numbers map back to spreadsheet coordinates.
+ const matrix = XLSX.utils.sheet_to_json<unknown[]>(worksheet, {
+ header: 1,
+ blankrows: true,
+ defval: "",
+ });
+
+ // Tolerate a leading title/subtitle block (like our own template): find the
+ // first row that actually contains a recognized "name" column header.
+ const headerRowIndex = matrix.findIndex((raw) => {
+ const cells = (raw as unknown[]).map((c) => cellToString(c));
+ return buildColumnIndex(cells).name !== -1;
+ });
+
+ if (headerRowIndex === -1) {
+ throw new Error(
+ 'Missing a "Name" column. Download the template for the expected format.',
+ );
+ }
+
+ const headers = (matrix[headerRowIndex] as unknown[]).map((h) =>
+ cellToString(h),
+ );
+ const columns = buildColumnIndex(headers);
+
+ const dataRows = matrix.slice(headerRowIndex + 1);
+ const contentRowCount = dataRows.reduce((count, raw) => {
+ const cells = raw as unknown[];
+ const name = columns.name === -1 ? "" : cellToString(cells[columns.name]);
+ const hierarchy =
+ columns.hierarchy === -1 ? "" : cellToString(cells[columns.hierarchy]);
+ return name || hierarchy ? count + 1 : count;
+ }, 0);
+ if (contentRowCount === 0) {
+ throw new Error(
+ "No data rows found. Add a header row plus at least one folder.",
+ );
+ }
+ if (contentRowCount > MAX_IMPORT_FOLDERS) {
+ throw new Error(
+ `File contains ${contentRowCount} rows. Maximum is ${MAX_IMPORT_FOLDERS}.`,
+ );
+ }
+
+ type WorkingRow = {
+ tempId: string;
+ name: string;
+ segments: string[];
+ code: string;
+ };
+
+ const working: WorkingRow[] = [];
+ const skipped: SkippedFolderRow[] = [];
+ // First tempId seen for a given canonical hierarchy code, so children can
+ // resolve their parent by code. Later duplicates are skipped.
+ const tempIdByCode = new Map<string, string>();
+
+ dataRows.forEach((raw, index) => {
+ const cells = raw as unknown[];
+ const at = (i: number) => (i === -1 ? "" : cellToString(cells[i]));
+
+ const name = at(columns.name);
+ const rawHierarchy = at(columns.hierarchy);
+ // 1-based spreadsheet row: matrix is 0-based, data starts after the header.
+ const spreadsheetRow = headerRowIndex + index + 2;
+
+ if (!name) {
+ // Flag rows that carry a hierarchy code but no folder name; ignore rows
+ // that are entirely empty.
+ if (rawHierarchy) {
+ skipped.push({ row: spreadsheetRow, reason: "Missing folder name" });
+ }
+ return;
+ }
+
+ const segments = hierarchySegments(rawHierarchy);
+ const code = segments.join(".");
+ const tempId = `f${index}`;
+
+ if (code) {
+ if (tempIdByCode.has(code)) {
+ skipped.push({
+ row: spreadsheetRow,
+ reason: `Duplicate hierarchy code "${code}"`,
+ });
+ return;
+ }
+ tempIdByCode.set(code, tempId);
+ }
+
+ working.push({
+ tempId,
+ name: name.slice(0, MAX_FOLDER_NAME_LENGTH),
+ segments,
+ code,
+ });
+ });
+
+ const parentTempIdOf = (segments: string[]): string | null => {
+ // Walk up the outline ("1.2.3" -> "1.2" -> "1") until we find a known
+ // ancestor; fall back to the top level when none exists.
+ for (let cut = segments.length - 1; cut >= 1; cut--) {
+ const candidate = segments.slice(0, cut).join(".");
+ const parent = tempIdByCode.get(candidate);
+ if (parent) return parent;
+ }
+ return null;
+ };
+
+ const rowByTempId = new Map<string, ParsedFolderRow>();
+ for (const row of working) {
+ rowByTempId.set(row.tempId, {
+ tempId: row.tempId,
+ name: row.name,
+ parentTempId: parentTempIdOf(row.segments),
+ depth: 0,
+ hierarchy: row.code,
+ orderIndex: 0,
+ });
+ }
+
+ const depthOf = (tempId: string, seen: Set<string>): number => {
+ const row = rowByTempId.get(tempId);
+ if (!row || row.parentTempId === null) return 0;
+ if (seen.has(tempId)) return 0; // defensive: never loop on malformed input
+ seen.add(tempId);
+ return depthOf(row.parentTempId, seen) + 1;
+ };
+ for (const row of rowByTempId.values()) {
+ row.depth = depthOf(row.tempId, new Set());
+ }
+
+ // Emit in pre-order so the preview reads like a tree while preserving the
+ // original sibling order.
+ const childrenByParent = new Map<string | null, ParsedFolderRow[]>();
+ for (const row of working) {
+ const parsed = rowByTempId.get(row.tempId)!;
+ const bucket = childrenByParent.get(parsed.parentTempId) ?? [];
+ parsed.orderIndex = bucket.length;
+ bucket.push(parsed);
+ childrenByParent.set(parsed.parentTempId, bucket);
+ }
+
+ const ordered: ParsedFolderRow[] = [];
+ const visit = (parentTempId: string | null) => {
+ for (const child of childrenByParent.get(parentTempId) ?? []) {
+ ordered.push(child);
+ visit(child.tempId);
+ }
+ };
+ visit(null);
+
+ return { rows: ordered, skipped };
+}
+
+/** Map preview rows to the payload the bulk folder endpoint expects. */
+export function toBulkFolderItems(
+ rows: ParsedFolderRow[],
+): BulkFolderRequestItem[] {
+ return rows.map((row) => ({
+ tempId: row.tempId,
+ name: row.name,
+ parentTempId: row.parentTempId,
+ orderIndex: row.orderIndex,
+ }));
+}
diff --git a/lib/utils/download-from-api.ts b/lib/utils/download-from-api.ts
new file mode 100644
index 00000000..dc7bff71
--- /dev/null
+++ b/lib/utils/download-from-api.ts
@@ -0,0 +1,36 @@
+import { extractFilenameFromContentDisposition } from "./download-document";
+
+/**
+ * GET a file-download endpoint and save the response, using the filename from
+ * the Content-Disposition header. Mirrors the dataroom index / request-list
+ * template download flows.
+ */
+export async function downloadFileFromApi(
+ url: string,
+ fallbackFileName = "download.xlsx",
+): Promise<void> {
+ const res = await fetch(url);
+ if (!res.ok) {
+ const body = (await res.json().catch(() => ({}))) as { message?: string };
+ throw new Error(body?.message || "Download failed");
+ }
+
+ const filename =
+ extractFilenameFromContentDisposition(
+ res.headers.get("Content-Disposition"),
+ ) ?? fallbackFileName;
+
+ const blob = await res.blob();
+ const objectUrl = window.URL.createObjectURL(blob);
+ const link = window.document.createElement("a");
+ link.href = objectUrl;
+ link.download = filename;
+ link.rel = "noopener noreferrer";
+ window.document.body.appendChild(link);
+ link.click();
+
+ setTimeout(() => {
+ window.URL.revokeObjectURL(objectUrl);
+ if (link.parentNode) window.document.body.removeChild(link);
+ }, 100);
+}
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/bulk.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/bulk.ts
index d2ea6ad9..4c53c967 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/bulk.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/bulk.ts
@@ -1,11 +1,11 @@
import { NextApiRequest, NextApiResponse } from "next";
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { Prisma } from "@prisma/client";
import { getServerSession } from "next-auth/next";
import { z } from "zod";
-import { authOptions } from "@/pages/api/auth/[...nextauth]";
-
+import { enforceDataroomMemberScope } from "@/lib/api/rbac/guard";
import {
BulkFolderInput,
BulkValidationError,
@@ -35,6 +35,19 @@ const BulkSchema = z.object({
* multiple requests. Ignored if `parentTempId` is set.
*/
parentPath: z.string().min(1).max(2000).optional().nullable(),
+ /**
+ * Position among the folder's siblings within this request, counting
+ * from 0. Folders landing directly under `rootPath` get shifted past
+ * the items already there, so an import never reorders what the user
+ * already arranged. Omitted/null leaves orderIndex null.
+ */
+ orderIndex: z
+ .number()
+ .int()
+ .min(0)
+ .max(MAX_BULK_FOLDERS_PER_REQUEST)
+ .optional()
+ .nullable(),
}),
)
.min(1)
@@ -80,6 +93,12 @@ export default async function handle(
const inputFolders: BulkFolderInput[] = parsed.data.folders;
try {
+ // Scoped members may only create folders within their assigned rooms,
+ // mirroring the single-folder POST in folders/index.ts.
+ if (await enforceDataroomMemberScope({ userId, teamId, dataroomId, res })) {
+ return;
+ }
+
const team = await prisma.team.findUnique({
where: {
id: teamId,
@@ -106,30 +125,37 @@ export default async function handle(
});
}
- const created = await prisma.$transaction(async (tx) => {
- let rootParentId: string | null = null;
- if (rootPath !== "/") {
- const parent = await tx.dataroomFolder.findUnique({
- where: { dataroomId_path: { dataroomId, path: rootPath } },
- select: { id: true },
- });
- if (!parent) {
- throw new BulkValidationError(
- "UNKNOWN_ROOT_PATH",
- "Parent folder does not exist",
- );
+ const created = await prisma.$transaction(
+ async (tx) => {
+ let rootParentId: string | null = null;
+ if (rootPath !== "/") {
+ const parent = await tx.dataroomFolder.findUnique({
+ where: { dataroomId_path: { dataroomId, path: rootPath } },
+ select: { id: true },
+ });
+ if (!parent) {
+ throw new BulkValidationError(
+ "UNKNOWN_ROOT_PATH",
+ "Parent folder does not exist",
+ );
+ }
+ rootParentId = parent.id;
}
- rootParentId = parent.id;
- }
-
- return bulkCreateDataroomFolders({
- tx,
- dataroomId,
- rootPath,
- rootParentId,
- folders: inputFolders,
- });
- });
+
+ return bulkCreateDataroomFolders({
+ tx,
+ dataroomId,
+ rootPath,
+ rootParentId,
+ folders: inputFolders,
+ });
+ },
+ // A full 500-folder batch runs one SELECT + one INSERT per tree level
+ // plus the ACL createMany. Prisma's 5s default leaves no headroom for
+ // that many round trips over a pooled connection, and blowing it rolls
+ // back the whole import.
+ { timeout: 20_000, maxWait: 10_000 },
+ );
return res.status(201).json({ folders: created });
} catch (error) {
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/import-template.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/import-template.ts
new file mode 100644
index 00000000..4be755d1
--- /dev/null
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/import-template.ts
@@ -0,0 +1,72 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { Prisma } from "@prisma/client";
+import { getServerSession } from "next-auth/next";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+
+import { enforceDataroomMemberScope } from "@/lib/api/rbac/guard";
+import { generateFolderImportTemplate } from "@/lib/folders/import-template";
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+
+// GET /api/teams/:teamId/datarooms/:id/folders/import-template
+// Streams a blank .xlsx template describing the folder-import format.
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method !== "GET") {
+ res.setHeader("Allow", ["GET"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const userId = (session.user as CustomUser).id;
+ const { teamId, id: dataroomId } = req.query as {
+ teamId: string;
+ id: string;
+ };
+
+ try {
+ if (await enforceDataroomMemberScope({ userId, teamId, dataroomId, res })) {
+ return;
+ }
+
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: { some: { userId } },
+ datarooms: { some: { id: dataroomId } },
+ },
+ select: { id: true },
+ });
+
+ if (!team) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const { data, filename, mimeType } = await generateFolderImportTemplate();
+
+ res.setHeader("Content-Type", mimeType);
+ res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
+ res.setHeader("Content-Length", String(data.byteLength));
+ return res.status(200).send(Buffer.from(data));
+ } catch (error) {
+ if (error instanceof Prisma.PrismaClientKnownRequestError) {
+ console.error("Folder import template failed:", error);
+ return res
+ .status(500)
+ .json({ error: "DB_ERROR", message: "Failed to generate import template" });
+ }
+ console.error("Folder import template failed:", error);
+ return res.status(500).json({
+ error: "INTERNAL_ERROR",
+ message: "Failed to generate import template",
+ });
+ }
+}
diff --git a/pages/api/teams/[teamId]/folders/bulk.ts b/pages/api/teams/[teamId]/folders/bulk.ts
index 5625f296..c124f691 100644
--- a/pages/api/teams/[teamId]/folders/bulk.ts
+++ b/pages/api/teams/[teamId]/folders/bulk.ts
@@ -87,30 +87,35 @@ export default async function handle(
});
}
- const created = await prisma.$transaction(async (tx) => {
- let rootParentId: string | null = null;
- if (rootPath !== "/") {
- const parent = await tx.folder.findUnique({
- where: { teamId_path: { teamId, path: rootPath } },
- select: { id: true },
- });
- if (!parent) {
- throw new BulkValidationError(
- "UNKNOWN_ROOT_PATH",
- "Parent folder does not exist",
- );
+ const created = await prisma.$transaction(
+ async (tx) => {
+ let rootParentId: string | null = null;
+ if (rootPath !== "/") {
+ const parent = await tx.folder.findUnique({
+ where: { teamId_path: { teamId, path: rootPath } },
+ select: { id: true },
+ });
+ if (!parent) {
+ throw new BulkValidationError(
+ "UNKNOWN_ROOT_PATH",
+ "Parent folder does not exist",
+ );
+ }
+ rootParentId = parent.id;
}
- rootParentId = parent.id;
- }
- return bulkCreateMainDocsFolders({
- tx,
- teamId,
- rootPath,
- rootParentId,
- folders: inputFolders,
- });
- });
+ return bulkCreateMainDocsFolders({
+ tx,
+ teamId,
+ rootPath,
+ rootParentId,
+ folders: inputFolders,
+ });
+ },
+ // See the dataroom variant: Prisma's 5s default is too tight for a
+ // full 500-folder batch over a pooled connection.
+ { timeout: 20_000, maxWait: 10_000 },
+ );
return res.status(201).json({ folders: created });
} catch (error) {