Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
import { NextApiRequest, NextApiResponse } from "next";
import { isTeamPausedById } from "@/ee/features/billing/cancellation/lib/is-team-paused";
import { getLimits } from "@/ee/limits/server";
import {
Dataroom,
DataroomBrand,
DataroomDocument,
DataroomFolder,
} from "@prisma/client";
import { withTeamApi } from "@/lib/api/auth/with-session-team";
import { newId } from "@/lib/id-helper";
import prisma from "@/lib/prisma";
interface DataroomWithContents extends Dataroom {
documents: DataroomDocument[];
folders: DataroomFolderWithContents[];
brand: Partial<DataroomBrand> | null;
}
interface DataroomFolderWithContents extends DataroomFolder {
documents: DataroomDocument[];
childFolders: DataroomFolderWithContents[];
}
// Function to fetch the existing data room structure
async function fetchDataroomContents(
dataroomId: string,
): Promise<DataroomWithContents> {
const dataroom = await prisma.dataroom.findUnique({
where: { id: dataroomId },
include: {
documents: true,
folders: {
where: { parentId: null }, // Only get root folders initially
include: { documents: true },
},
brand: true,
},
});
if (!dataroom) {
throw new Error(`Dataroom with id ${dataroomId} not found`);
}
// Recursive function to fetch folder contents
async function getFolderContents(
folderId: string,
): Promise<DataroomFolderWithContents> {
const folder = await prisma.dataroomFolder.findUnique({
where: { id: folderId },
include: {
documents: true,
childFolders: {
include: { documents: true },
},
},
});
if (!folder) {
throw new Error(`Folder with id ${folderId} not found`);
}
const childFolders = await Promise.all(
folder.childFolders.map(async (childFolder) => {
const nestedContents = await getFolderContents(childFolder.id);
return nestedContents;
}),
);
return {
...folder,
documents: folder.documents,
childFolders: childFolders,
};
}
// Transform root folders by fetching their complete contents
const foldersWithContents = await Promise.all(
dataroom.folders.map((folder) => getFolderContents(folder.id)),
);
return {
...dataroom,
documents: dataroom.documents.filter((doc) => !doc.folderId),
folders: foldersWithContents,
brand: dataroom.brand,
};
}
// Recursive function to duplicate folders and documents
async function duplicateFolders(
dataroomId: string,
folder: DataroomFolderWithContents,
parentFolderId?: string,
) {
const newFolder = await prisma.dataroomFolder.create({
data: {
name: folder.name,
path: folder.path,
parentId: parentFolderId,
dataroomId: dataroomId,
},
select: { id: true },
});
// Duplicate documents for the current folder
await Promise.allSettled(
folder.documents.map((doc) =>
prisma.dataroomDocument.create({
data: {
documentId: doc.documentId,
dataroomId: dataroomId,
folderId: newFolder.id,
},
}),
),
);
// Duplicate child folders recursively
await Promise.allSettled(
folder.childFolders.map((childFolder) =>
duplicateFolders(dataroomId, childFolder, newFolder.id),
),
);
}
// POST /api/teams/:teamId/datarooms/:id/duplicate
const postHandler = withTeamApi(
async ({ req, res, teamId, userId, team }) => {
const { id: dataroomId } = req.query as { id: string };
try {
if (team.plan.includes("drtrial")) {
return res.status(403).json({
message:
"You've reached the limit of datarooms. Consider upgrading your plan.",
});
}
// Check if team is paused
const teamIsPaused = await isTeamPausedById(teamId);
if (teamIsPaused) {
return res.status(403).json({
error:
"Team is currently paused. Duplicating dataroom is not available.",
});
}
const dataroom = await prisma.dataroom.findUnique({
where: {
id: dataroomId,
teamId: teamId,
},
select: { id: true },
});
if (!dataroom) {
return res.status(404).json({ message: "Dataroom not found" });
}
// Check if the team has reached the limit of datarooms
const [dataroomCount, limits] = await Promise.all([
prisma.dataroom.count({ where: { teamId } }),
getLimits({ teamId, userId }),
]);
if (
limits &&
limits.datarooms !== null &&
dataroomCount >= limits.datarooms
) {
console.log("Dataroom limit reached", limits.datarooms, dataroomCount);
return res.status(400).json({
message:
"You've reached the limit of datarooms. Consider upgrading your plan.",
});
}
// Fetch the existing data room structure
const dataroomContents = await fetchDataroomContents(dataroomId);
// Create a new data room
const pId = newId("dataroom");
const newDataroom = await prisma.dataroom.create({
data: {
pId: pId,
name: dataroomContents.name + " (Copy)",
teamId: dataroomContents.teamId,
brandId: dataroomContents.brandId,
documents: {
create: dataroomContents.documents.map((doc) => ({
documentId: doc.documentId,
})),
},
folders: {
create: [],
},
brand: dataroomContents.brand
? {
create: {
logo: dataroomContents.brand.logo,
hideLogo: dataroomContents.brand.hideLogo,
banner: dataroomContents.brand.banner,
brandColor: dataroomContents.brand.brandColor,
accentColor: dataroomContents.brand.accentColor,
accentButtonColor: dataroomContents.brand.accentButtonColor,
applyAccentColorToDataroomView:
dataroomContents.brand.applyAccentColorToDataroomView ??
false,
welcomeMessage: dataroomContents.brand.welcomeMessage,
defaultLanguage: dataroomContents.brand.defaultLanguage,
cardLayout: dataroomContents.brand.cardLayout ?? undefined,
showFolderTree:
dataroomContents.brand.showFolderTree ?? undefined,
viewerLayoutPreset:
dataroomContents.brand.viewerLayoutPreset ?? undefined,
viewerHeaderStyle:
dataroomContents.brand.viewerHeaderStyle ?? undefined,
hideFolderIconsInMain:
dataroomContents.brand.hideFolderIconsInMain ?? undefined,
ctaLabel: dataroomContents.brand.ctaLabel,
ctaUrl: dataroomContents.brand.ctaUrl,
customLinkPreviewEnabled:
dataroomContents.brand.customLinkPreviewEnabled ?? false,
linkPreviewTitle: dataroomContents.brand.linkPreviewTitle,
linkPreviewDescription:
dataroomContents.brand.linkPreviewDescription,
linkPreviewImage: dataroomContents.brand.linkPreviewImage,
linkPreviewFavicon: dataroomContents.brand.linkPreviewFavicon,
},
}
: undefined,
},
});
// Changed this section to properly await all folder duplications
await Promise.all(
dataroomContents.folders
.filter((folder) => !folder.parentId)
.map((folder) => duplicateFolders(newDataroom.id, folder)),
);
res.status(201).json(newDataroom);
} catch (error) {
console.error("Request error", error);
res.status(500).json({ message: "Error duplicating dataroom" });
}
},
{
// Duplicating a dataroom creates a new team-level dataroom; scoped members
// are excluded (they hold datarooms.write only for rooms they already
// manage, and the copy would never be assigned to them).
requiredPermissions: ["datarooms.write"],
requiredRoles: ["ADMIN", "MANAGER", "MEMBER"],
},
);
export default async function handle(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method === "POST") {
return postHandler(req, res);
} else {
// We only allow POST requests
res.setHeader("Allow", ["POST"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}