Skip to content

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

feat(ee): add templates

Marc Seitz71c78d4a9af7
diff --git a/ee/features/templates/api/datarooms/apply-template.ts b/ee/features/templates/api/datarooms/apply-template.ts
new file mode 100644
index 00000000..a7995e4d
--- /dev/null
+++ b/ee/features/templates/api/datarooms/apply-template.ts
@@ -0,0 +1,105 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import {
+ DATAROOM_TEMPLATES,
+ FolderTemplate,
+} from "@/ee/features/templates/constants/dataroom-templates";
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import slugify from "@sindresorhus/slugify";
+import { getServerSession } from "next-auth/next";
+
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "POST") {
+ // POST /api/teams/:teamId/datarooms/:id/apply-template
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ res.status(401).end("Unauthorized");
+ return;
+ }
+
+ const userId = (session.user as CustomUser).id;
+ const { teamId, id: dataroomId } = req.query as {
+ teamId: string;
+ id: string;
+ };
+ const { type } = req.body as { type: string };
+
+ // Validate the type
+ if (!type || !DATAROOM_TEMPLATES[type]) {
+ return res.status(400).json({
+ message: "Invalid template type.",
+ });
+ }
+
+ try {
+ // Check if the user is part of the team and has access to the dataroom
+ const dataroom = await prisma.dataroom.findUnique({
+ where: {
+ id: dataroomId,
+ teamId: teamId,
+ team: {
+ users: {
+ some: {
+ userId: userId,
+ },
+ },
+ },
+ },
+ });
+
+ if (!dataroom) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const template = DATAROOM_TEMPLATES[type];
+
+ // Helper function to create folders recursively
+ const createFolders = async (
+ folders: FolderTemplate[],
+ parentPath: string = "",
+ parentId: string | null = null,
+ ): Promise<void> => {
+ for (const folder of folders) {
+ const folderPath = parentPath + "/" + slugify(folder.name);
+
+ // Create the folder
+ const createdFolder = await prisma.dataroomFolder.create({
+ data: {
+ name: folder.name,
+ path: folderPath,
+ parentId: parentId,
+ dataroomId: dataroom.id,
+ },
+ });
+
+ // If the folder has subfolders, create them recursively
+ if (folder.subfolders && folder.subfolders.length > 0) {
+ await createFolders(
+ folder.subfolders,
+ folderPath,
+ createdFolder.id,
+ );
+ }
+ }
+ };
+
+ await createFolders(template.folders);
+
+ res.status(200).json({
+ message: "Template applied successfully",
+ });
+ } catch (error) {
+ console.error("Error applying template:", error);
+ return res.status(500).json({ error: "Error applying template" });
+ }
+ } else {
+ res.setHeader("Allow", ["POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}
diff --git a/ee/features/templates/api/datarooms/generate.ts b/ee/features/templates/api/datarooms/generate.ts
new file mode 100644
index 00000000..54d24211
--- /dev/null
+++ b/ee/features/templates/api/datarooms/generate.ts
@@ -0,0 +1,150 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import {
+ DATAROOM_TEMPLATES,
+ FolderTemplate,
+} from "@/ee/features/templates/constants/dataroom-templates";
+import { getLimits } from "@/ee/limits/server";
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import slugify from "@sindresorhus/slugify";
+import { getServerSession } from "next-auth/next";
+
+import { newId } from "@/lib/id-helper";
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "POST") {
+ // POST /api/teams/:teamId/datarooms/generate
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ res.status(401).end("Unauthorized");
+ return;
+ }
+
+ const userId = (session.user as CustomUser).id;
+
+ const { teamId } = req.query as { teamId: string };
+ const { name, type } = req.body as { name: string; type: string };
+
+ // Validate the type
+ if (!type || !DATAROOM_TEMPLATES[type]) {
+ return res.status(400).json({
+ message: "Invalid dataroom type. Please select a valid type.",
+ });
+ }
+
+ try {
+ // Check if the user is part of the team
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ plan: {
+ // exclude all teams not on `business`, `datarooms`, `datarooms-plus`, `business+old`, `datarooms+old`, `datarooms-plus+old` plan
+ in: [
+ "business",
+ "datarooms",
+ "datarooms-plus",
+ "business+old",
+ "datarooms+old",
+ "datarooms-plus+old",
+ "datarooms+drtrial",
+ "business+drtrial",
+ "datarooms-plus+drtrial",
+ ],
+ },
+ users: {
+ some: {
+ userId: userId,
+ },
+ },
+ },
+ });
+
+ if (!team) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ // Limits: Check if the user has reached the limit of datarooms in the team
+ const dataroomCount = await prisma.dataroom.count({
+ where: {
+ teamId: teamId,
+ },
+ });
+
+ const limits = await getLimits({ teamId, userId });
+
+ if (limits && dataroomCount >= limits.datarooms) {
+ return res
+ .status(403)
+ .json({ message: "You have reached the limit of datarooms" });
+ }
+
+ const pId = newId("dataroom");
+
+ // Create the dataroom
+ const dataroom = await prisma.dataroom.create({
+ data: {
+ name: name,
+ teamId: teamId,
+ pId: pId,
+ },
+ });
+
+ // Create folders based on the selected template
+ const template = DATAROOM_TEMPLATES[type];
+
+ // Helper function to create folders recursively
+ const createFolders = async (
+ folders: FolderTemplate[],
+ parentPath: string = "",
+ parentId: string | null = null,
+ ): Promise<void> => {
+ for (const folder of folders) {
+ const folderPath = parentPath + "/" + slugify(folder.name);
+
+ // Create the folder
+ const createdFolder = await prisma.dataroomFolder.create({
+ data: {
+ name: folder.name,
+ path: folderPath,
+ parentId: parentId,
+ dataroomId: dataroom.id,
+ },
+ });
+
+ // If the folder has subfolders, create them recursively
+ if (folder.subfolders && folder.subfolders.length > 0) {
+ await createFolders(
+ folder.subfolders,
+ folderPath,
+ createdFolder.id,
+ );
+ }
+ }
+ };
+
+ await createFolders(template.folders);
+
+ const dataroomWithCount = {
+ ...dataroom,
+ _count: { documents: 0 },
+ };
+
+ res.status(201).json({
+ dataroom: dataroomWithCount,
+ message: "Dataroom generated successfully",
+ });
+ } catch (error) {
+ console.error("Error generating dataroom:", error);
+ return res.status(500).json({ error: "Error generating dataroom" });
+ }
+ } else {
+ // We only allow POST requests
+ res.setHeader("Allow", ["POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}
diff --git a/lib/constants/dataroom-templates.ts b/ee/features/templates/constants/dataroom-templates.ts
similarity index 99%
rename from lib/constants/dataroom-templates.ts
rename to ee/features/templates/constants/dataroom-templates.ts
index 1fc19f80..16048096 100644
--- a/lib/constants/dataroom-templates.ts
+++ b/ee/features/templates/constants/dataroom-templates.ts
@@ -1,5 +1,3 @@
-// Shared dataroom templates used for both generation and application
-
export interface FolderTemplate {
name: string;
subfolders?: FolderTemplate[];
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/apply-template.ts b/pages/api/teams/[teamId]/datarooms/[id]/apply-template.ts
index 71d40ac5..ba98f7e5 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/apply-template.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/apply-template.ts
@@ -1,104 +1,2 @@
-import type { NextApiRequest, NextApiResponse } from "next";
-
-import { authOptions } from "@/pages/api/auth/[...nextauth]";
-import slugify from "@sindresorhus/slugify";
-import { getServerSession } from "next-auth/next";
-
-import {
- DATAROOM_TEMPLATES,
- FolderTemplate,
-} from "@/lib/constants/dataroom-templates";
-import prisma from "@/lib/prisma";
-import { CustomUser } from "@/lib/types";
-
-export default async function handle(
- req: NextApiRequest,
- res: NextApiResponse,
-) {
- if (req.method === "POST") {
- const session = await getServerSession(req, res, authOptions);
- if (!session) {
- res.status(401).end("Unauthorized");
- return;
- }
-
- const userId = (session.user as CustomUser).id;
- const { teamId, id: dataroomId } = req.query as {
- teamId: string;
- id: string;
- };
- const { type } = req.body as { type: string };
-
- // Validate the type
- if (!type || !DATAROOM_TEMPLATES[type]) {
- return res.status(400).json({
- message: "Invalid template type.",
- });
- }
-
- try {
- // Check if the user is part of the team and has access to the dataroom
- const dataroom = await prisma.dataroom.findUnique({
- where: {
- id: dataroomId,
- teamId: teamId,
- team: {
- users: {
- some: {
- userId: userId,
- },
- },
- },
- },
- });
-
- if (!dataroom) {
- return res.status(401).end("Unauthorized");
- }
-
- const template = DATAROOM_TEMPLATES[type];
-
- // Helper function to create folders recursively
- const createFolders = async (
- folders: FolderTemplate[],
- parentPath: string = "",
- parentId: string | null = null,
- ): Promise<void> => {
- for (const folder of folders) {
- const folderPath = parentPath + "/" + slugify(folder.name);
-
- // Create the folder
- const createdFolder = await prisma.dataroomFolder.create({
- data: {
- name: folder.name,
- path: folderPath,
- parentId: parentId,
- dataroomId: dataroom.id,
- },
- });
-
- // If the folder has subfolders, create them recursively
- if (folder.subfolders && folder.subfolders.length > 0) {
- await createFolders(
- folder.subfolders,
- folderPath,
- createdFolder.id,
- );
- }
- }
- };
-
- await createFolders(template.folders);
-
- res.status(200).json({
- message: "Template applied successfully",
- });
- } catch (error) {
- console.error("Error applying template:", error);
- return res.status(500).json({ error: "Error applying template" });
- }
- } else {
- res.setHeader("Allow", ["POST"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
- }
-}
+// Re-export from ee folder
+export { default } from "@/ee/features/templates/api/datarooms/apply-template";
diff --git a/pages/api/teams/[teamId]/datarooms/generate.ts b/pages/api/teams/[teamId]/datarooms/generate.ts
index e099ccea..ca3b6b05 100644
--- a/pages/api/teams/[teamId]/datarooms/generate.ts
+++ b/pages/api/teams/[teamId]/datarooms/generate.ts
@@ -1,150 +1,2 @@
-import type { NextApiRequest, NextApiResponse } from "next";
-
-import { getLimits } from "@/ee/limits/server";
-import { authOptions } from "@/pages/api/auth/[...nextauth]";
-import slugify from "@sindresorhus/slugify";
-import { getServerSession } from "next-auth/next";
-
-import {
- DATAROOM_TEMPLATES,
- FolderTemplate,
-} from "@/lib/constants/dataroom-templates";
-import { newId } from "@/lib/id-helper";
-import prisma from "@/lib/prisma";
-import { CustomUser } from "@/lib/types";
-
-export default async function handle(
- req: NextApiRequest,
- res: NextApiResponse,
-) {
- if (req.method === "POST") {
- // POST /api/teams/:teamId/datarooms/generate
- const session = await getServerSession(req, res, authOptions);
- if (!session) {
- res.status(401).end("Unauthorized");
- return;
- }
-
- const userId = (session.user as CustomUser).id;
-
- const { teamId } = req.query as { teamId: string };
- const { name, type } = req.body as { name: string; type: string };
-
- // Validate the type
- if (!type || !DATAROOM_TEMPLATES[type]) {
- return res.status(400).json({
- message: "Invalid dataroom type. Please select a valid type.",
- });
- }
-
- try {
- // Check if the user is part of the team
- const team = await prisma.team.findUnique({
- where: {
- id: teamId,
- plan: {
- // exclude all teams not on `business`, `datarooms`, `datarooms-plus`, `business+old`, `datarooms+old`, `datarooms-plus+old` plan
- in: [
- "business",
- "datarooms",
- "datarooms-plus",
- "business+old",
- "datarooms+old",
- "datarooms-plus+old",
- "datarooms+drtrial",
- "business+drtrial",
- "datarooms-plus+drtrial",
- ],
- },
- users: {
- some: {
- userId: userId,
- },
- },
- },
- });
-
- if (!team) {
- return res.status(401).end("Unauthorized");
- }
-
- // Limits: Check if the user has reached the limit of datarooms in the team
- const dataroomCount = await prisma.dataroom.count({
- where: {
- teamId: teamId,
- },
- });
-
- const limits = await getLimits({ teamId, userId });
-
- if (limits && dataroomCount >= limits.datarooms) {
- return res
- .status(403)
- .json({ message: "You have reached the limit of datarooms" });
- }
-
- const pId = newId("dataroom");
-
- // Create the dataroom
- const dataroom = await prisma.dataroom.create({
- data: {
- name: name,
- teamId: teamId,
- pId: pId,
- },
- });
-
- // Create folders based on the selected template
- const template = DATAROOM_TEMPLATES[type];
-
- // Helper function to create folders recursively
- const createFolders = async (
- folders: FolderTemplate[],
- parentPath: string = "",
- parentId: string | null = null,
- ): Promise<void> => {
- for (const folder of folders) {
- const folderPath = parentPath + "/" + slugify(folder.name);
-
- // Create the folder
- const createdFolder = await prisma.dataroomFolder.create({
- data: {
- name: folder.name,
- path: folderPath,
- parentId: parentId,
- dataroomId: dataroom.id,
- },
- });
-
- // If the folder has subfolders, create them recursively
- if (folder.subfolders && folder.subfolders.length > 0) {
- await createFolders(
- folder.subfolders,
- folderPath,
- createdFolder.id,
- );
- }
- }
- };
-
- await createFolders(template.folders);
-
- const dataroomWithCount = {
- ...dataroom,
- _count: { documents: 0 },
- };
-
- res.status(201).json({
- dataroom: dataroomWithCount,
- message: "Dataroom generated successfully",
- });
- } catch (error) {
- console.error("Error generating dataroom:", error);
- return res.status(500).json({ error: "Error generating dataroom" });
- }
- } else {
- // We only allow POST requests
- res.setHeader("Allow", ["POST"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
- }
-}
+// Re-export from ee folder
+export { default } from "@/ee/features/templates/api/datarooms/generate";