Skip to content

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

feat: add prisma transaction

Marc Seitz4a47891a0ba8
diff --git a/ee/features/templates/api/datarooms/apply-template.ts b/ee/features/templates/api/datarooms/apply-template.ts
index ae968f41..0a5f4376 100644
--- a/ee/features/templates/api/datarooms/apply-template.ts
+++ b/ee/features/templates/api/datarooms/apply-template.ts
@@ -64,37 +64,40 @@ export default async function handle(
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 folders in a transaction to prevent hanging results
+ await prisma.$transaction(async (tx) => {
+ // 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,
- },
- });
+ // Create the folder
+ const createdFolder = await tx.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,
- );
+ // 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);
+ await createFolders(template.folders);
+ });
res.status(200).json({
message: "Template applied successfully",
diff --git a/ee/features/templates/api/datarooms/generate.ts b/ee/features/templates/api/datarooms/generate.ts
index 7dfc68a8..70dbfa45 100644
--- a/ee/features/templates/api/datarooms/generate.ts
+++ b/ee/features/templates/api/datarooms/generate.ts
@@ -90,49 +90,54 @@ export default async function handle(
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,
- );
+ // Create the dataroom and folders in a transaction to prevent hanging results
+ const dataroom = await prisma.$transaction(async (tx) => {
+ // Create the dataroom
+ const createdDataroom = await tx.dataroom.create({
+ data: {
+ name: name,
+ teamId: teamId,
+ pId: pId,
+ },
+ });
+
+ // 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 tx.dataroomFolder.create({
+ data: {
+ name: folder.name,
+ path: folderPath,
+ parentId: parentId,
+ dataroomId: createdDataroom.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);
+ await createFolders(template.folders);
+
+ return createdDataroom;
+ });
const dataroomWithCount = {
...dataroom,