Skip to content

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

refector code

Ashish Viradiya0b4fa6e32853
diff --git a/components/datarooms/dataroom-items-list.tsx b/components/datarooms/dataroom-items-list.tsx
index 70a0eddc..0bc79d5d 100644
--- a/components/datarooms/dataroom-items-list.tsx
+++ b/components/datarooms/dataroom-items-list.tsx
@@ -84,8 +84,7 @@ export function DataroomItemsList({
const {
applyDefaultPermissions,
inheritParentPermissions,
- applyDefaultPermissionGroupPermissions,
- inheritParentPermissionGroupPermissions,
+ applyPermissionGroupPermissions,
} = useDataroomPermissions();
const [uploads, setUploads] = useState<UploadState[]>([]);
@@ -595,37 +594,15 @@ export function DataroomItemsList({
// Handle PermissionGroup permissions
if (hasPermissionGroups) {
- const linkPermissionStrategy =
- dataroom?.defaultLinkPermission || "inherit_from_parent";
-
- if (linkPermissionStrategy === "inherit_from_parent") {
- promises.push(
- inheritParentPermissionGroupPermissions(
- dataroomId,
- documentIds,
- folderPathName?.join("/"),
- ),
- );
- } else if (
- linkPermissionStrategy === "use_default_permissions" ||
- linkPermissionStrategy === "use_simple_permissions"
- ) {
- const isRootLevel = !folderPathName || folderPathName.length === 0;
-
- if (isRootLevel) {
- promises.push(
- applyDefaultPermissionGroupPermissions(dataroomId, documentIds),
- );
- } else {
- promises.push(
- inheritParentPermissionGroupPermissions(
- dataroomId,
- documentIds,
- folderPathName?.join("/"),
- ),
- );
- }
- }
+ promises.push(
+ applyPermissionGroupPermissions(
+ dataroomId,
+ documentIds,
+ dataroom?.defaultLinkPermission || "inherit_from_parent",
+ folderPathName?.join("/"),
+ (message) => toast.error(message),
+ ),
+ );
}
Promise.allSettled(promises)
@@ -809,4 +786,4 @@ export function DataroomItemsList({
<DeleteFolderModal />
</>
);
-}
+}
\ No newline at end of file
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index eea7f2ce..20bc48c8 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -88,8 +88,7 @@ export function AddDocumentModal({
const {
applyDefaultPermissions,
inheritParentPermissions,
- applyDefaultPermissionGroupPermissions,
- inheritParentPermissionGroupPermissions,
+ applyPermissionGroupPermissions,
} = useDataroomPermissions();
useEffect(() => {
@@ -151,6 +150,92 @@ export function AddDocumentModal({
"Failed to apply default permissions. Update the group permissions in the group settings.",
);
+ const applyViewerGroupPermissions = async (
+ document: any,
+ dataroomDocument: DataroomDocument & {
+ dataroom: {
+ _count: { viewerGroups: number; permissionGroups: number };
+ };
+ },
+ currentFolderPath?: string[],
+ ): Promise<void> => {
+ const hasViewerGroups = dataroomDocument.dataroom._count.viewerGroups > 0;
+ const hasPermissionGroups =
+ dataroomDocument.dataroom._count.permissionGroups > 0;
+
+ if (hasViewerGroups) {
+ const defaultPermission =
+ dataroom?.defaultGroupPermission || "ask_every_time";
+
+ if (defaultPermission === "ask_every_time") {
+ setShowGroupPermissions(true);
+ setUploadedFiles([
+ {
+ documentId: document.id,
+ dataroomDocumentId: dataroomDocument.id,
+ fileName: document.name,
+ },
+ ]);
+ } else if (defaultPermission === "inherit_from_parent") {
+ const isRootLevel =
+ !currentFolderPath || currentFolderPath.length === 0;
+ if (isRootLevel) {
+ setShowGroupPermissions(true);
+ setUploadedFiles([
+ {
+ documentId: document.id,
+ dataroomDocumentId: dataroomDocument.id,
+ fileName: document.name,
+ },
+ ]);
+ } else {
+ try {
+ const result = await inheritParentPermissions(
+ dataroomId!,
+ [document.id],
+ currentFolderPath?.join("/"),
+ );
+
+ if (!result.success) {
+ console.error(
+ "Failed to inherit parent permissions:",
+ result.error,
+ );
+ toastErrorMessage();
+ }
+ } catch (error) {
+ console.error("Failed to inherit parent permissions:", error);
+ toastErrorMessage();
+ }
+ }
+ } else if (defaultPermission === "use_default_permissions") {
+ try {
+ const result = await applyDefaultPermissions(dataroomId!, [
+ document.id,
+ ]);
+ if (!result.success) {
+ console.error("Failed to apply default permissions:", result.error);
+ toastErrorMessage();
+ }
+ } catch (error) {
+ console.error("Failed to apply default permissions:", error);
+ toastErrorMessage();
+ }
+ }
+ }
+
+ // Handle PermissionGroup permissions
+ if (hasPermissionGroups) {
+ await applyPermissionGroupPermissions(
+ dataroomId!,
+ [document.id],
+ dataroom?.defaultLinkPermission || "inherit_from_parent",
+ currentFolderPath?.join("/"),
+ toastErrorMessage,
+ );
+ }
+ };
+
const handleFileUpload = async (
event: FormEvent<HTMLFormElement>,
): Promise<void> => {
@@ -245,158 +330,11 @@ export function AddDocumentModal({
};
};
- const hasViewerGroups =
- dataroomDocument.dataroom._count.viewerGroups > 0;
- const hasPermissionGroups =
- dataroomDocument.dataroom._count.permissionGroups > 0;
- if (hasViewerGroups) {
- const defaultPermission =
- dataroom?.defaultGroupPermission || "ask_every_time";
-
- if (defaultPermission === "ask_every_time") {
- setShowGroupPermissions(true);
- setUploadedFiles([
- {
- documentId: document.id,
- dataroomDocumentId: dataroomDocument.id,
- fileName: document.name,
- },
- ]);
- } else if (defaultPermission === "inherit_from_parent") {
- const isRootLevel =
- !currentFolderPath || currentFolderPath.length === 0;
- if (isRootLevel) {
- setShowGroupPermissions(true);
- setUploadedFiles([
- {
- documentId: document.id,
- dataroomDocumentId: dataroomDocument.id,
- fileName: document.name,
- },
- ]);
- } else {
- try {
- if (hasViewerGroups) {
- const result = await inheritParentPermissions(
- dataroomId,
- [document.id],
- currentFolderPath?.join("/"),
- );
-
- if (!result.success) {
- console.error(
- "Failed to inherit parent permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- }
- } catch (error) {
- console.error(
- "Failed to inherit parent permissions:",
- error,
- );
- toastErrorMessage();
- }
- }
- } else if (defaultPermission === "use_default_permissions") {
- try {
- if (hasViewerGroups) {
- const result = await applyDefaultPermissions(dataroomId, [
- document.id,
- ]);
- if (!result.success) {
- console.error(
- "Failed to apply default permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- }
- } catch (error) {
- console.error("Failed to apply default permissions:", error);
- toastErrorMessage();
- }
- }
- }
-
- if (hasPermissionGroups) {
- const linkPermissionStrategy =
- dataroom?.defaultLinkPermission || "inherit_from_parent";
-
- if (linkPermissionStrategy === "inherit_from_parent") {
- try {
- const result = await inheritParentPermissionGroupPermissions(
- dataroomId,
- [document.id],
- currentFolderPath?.join("/"),
- );
- if (!result.success) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- } catch (error) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- error,
- );
- toastErrorMessage();
- }
- } else if (
- linkPermissionStrategy === "use_default_permissions" ||
- linkPermissionStrategy === "use_simple_permissions"
- ) {
- const isRootLevel =
- !currentFolderPath || currentFolderPath.length === 0;
-
- if (isRootLevel) {
- try {
- const result = await applyDefaultPermissionGroupPermissions(
- dataroomId,
- [document.id],
- );
- if (!result.success) {
- console.error(
- "Failed to apply default PermissionGroup permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- } catch (error) {
- console.error(
- "Failed to apply default PermissionGroup permissions:",
- error,
- );
- toastErrorMessage();
- }
- } else {
- try {
- const result =
- await inheritParentPermissionGroupPermissions(
- dataroomId,
- [document.id],
- currentFolderPath?.join("/"),
- );
- if (!result.success) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- } catch (error) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- error,
- );
- toastErrorMessage();
- }
- }
- }
- }
+ await applyViewerGroupPermissions(
+ document,
+ dataroomDocument,
+ currentFolderPath,
+ );
}
analytics.capture("Document Added", {
@@ -544,159 +482,11 @@ export function AddDocumentModal({
};
};
- const hasViewerGroups =
- dataroomDocument.dataroom._count.viewerGroups > 0;
- const hasPermissionGroups =
- dataroomDocument.dataroom._count.permissionGroups > 0;
-
- // Handle ViewerGroup permissions
- if (hasViewerGroups) {
- const defaultPermission =
- dataroom?.defaultGroupPermission || "ask_every_time";
-
- if (defaultPermission === "ask_every_time") {
- setShowGroupPermissions(true);
- setUploadedFiles([
- {
- documentId: document.id,
- dataroomDocumentId: dataroomDocument.id,
- fileName: document.name,
- },
- ]);
- } else if (defaultPermission === "inherit_from_parent") {
- const isRootLevel =
- !currentFolderPath || currentFolderPath.length === 0;
- if (isRootLevel) {
- setShowGroupPermissions(true);
- setUploadedFiles([
- {
- documentId: document.id,
- dataroomDocumentId: dataroomDocument.id,
- fileName: document.name,
- },
- ]);
- } else {
- // For subfolder uploads, inherit permissions from parent folder automatically
- try {
- if (hasViewerGroups) {
- const result = await inheritParentPermissions(
- dataroomId,
- [document.id],
- currentFolderPath?.join("/"),
- );
-
- if (!result.success) {
- console.error(
- "Failed to inherit parent permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- }
- } catch (error) {
- console.error("Failed to inherit parent permissions:", error);
- toastErrorMessage();
- }
- }
- } else if (defaultPermission === "use_default_permissions") {
- try {
- if (hasViewerGroups) {
- const result = await applyDefaultPermissions(dataroomId, [
- document.id,
- ]);
- if (!result.success) {
- console.error(
- "Failed to apply default permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- }
- } catch (error) {
- console.error("Failed to apply default permissions:", error);
- toastErrorMessage();
- }
- }
-
- // Handle PermissionGroup permissions based on defaultLinkPermission
- if (hasPermissionGroups) {
- const linkPermissionStrategy =
- dataroom?.defaultLinkPermission || "inherit_from_parent";
-
- if (linkPermissionStrategy === "inherit_from_parent") {
- try {
- const result = await inheritParentPermissionGroupPermissions(
- dataroomId,
- [document.id],
- currentFolderPath?.join("/"),
- );
- if (!result.success) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- } catch (error) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- error,
- );
- toastErrorMessage();
- }
- } else if (
- linkPermissionStrategy === "use_default_permissions" ||
- linkPermissionStrategy === "use_simple_permissions"
- ) {
- const isRootLevel =
- !currentFolderPath || currentFolderPath.length === 0;
-
- if (isRootLevel) {
- try {
- const result = await applyDefaultPermissionGroupPermissions(
- dataroomId,
- [document.id],
- );
- if (!result.success) {
- console.error(
- "Failed to apply default PermissionGroup permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- } catch (error) {
- console.error(
- "Failed to apply default PermissionGroup permissions:",
- error,
- );
- toastErrorMessage();
- }
- } else {
- try {
- const result =
- await inheritParentPermissionGroupPermissions(
- dataroomId,
- [document.id],
- currentFolderPath?.join("/"),
- );
- if (!result.success) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- result.error,
- );
- toastErrorMessage();
- }
- } catch (error) {
- console.error(
- "Failed to inherit parent PermissionGroup permissions:",
- error,
- );
- toastErrorMessage();
- }
- }
- }
- }
- }
+ await applyViewerGroupPermissions(
+ document,
+ dataroomDocument,
+ currentFolderPath,
+ );
}
analytics.capture("Document Added", {
@@ -946,4 +736,4 @@ export function AddDocumentModal({
)}
</>
);
-}
+}
\ No newline at end of file
diff --git a/lib/hooks/use-dataroom-permissions.ts b/lib/hooks/use-dataroom-permissions.ts
index bdf5d424..076b98e6 100644
--- a/lib/hooks/use-dataroom-permissions.ts
+++ b/lib/hooks/use-dataroom-permissions.ts
@@ -184,10 +184,97 @@ export const useDataroomPermissions = () => {
}
};
+ const applyPermissionGroupPermissions = async (
+ dataroomId: string,
+ documentIds: string[],
+ defaultLinkPermission: string,
+ folderPath?: string,
+ onError?: (message: string) => void,
+ ): Promise<{ success: boolean; error?: string }> => {
+ if (defaultLinkPermission === "inherit_from_parent") {
+ try {
+ const result = await inheritParentPermissionGroupPermissions(
+ dataroomId,
+ documentIds,
+ folderPath,
+ );
+ if (!result.success) {
+ console.error(
+ "Failed to inherit parent PermissionGroup permissions:",
+ result.error,
+ );
+ onError?.("Failed to inherit parent PermissionGroup permissions");
+ }
+ return result;
+ } catch (error) {
+ console.error(
+ "Failed to inherit parent PermissionGroup permissions:",
+ error,
+ );
+ onError?.("Failed to inherit parent PermissionGroup permissions");
+ return { success: false, error: error instanceof Error ? error.message : "Unknown error" };
+ }
+ } else if (
+ defaultLinkPermission === "use_default_permissions" ||
+ defaultLinkPermission === "use_simple_permissions"
+ ) {
+ const isRootLevel = !folderPath || folderPath.length === 0;
+
+ if (isRootLevel) {
+ try {
+ const result = await applyDefaultPermissionGroupPermissions(
+ dataroomId,
+ documentIds,
+ );
+ if (!result.success) {
+ console.error(
+ "Failed to apply default PermissionGroup permissions:",
+ result.error,
+ );
+ onError?.("Failed to apply default PermissionGroup permissions");
+ }
+ return result;
+ } catch (error) {
+ console.error(
+ "Failed to apply default PermissionGroup permissions:",
+ error,
+ );
+ onError?.("Failed to apply default PermissionGroup permissions");
+ return { success: false, error: error instanceof Error ? error.message : "Unknown error" };
+ }
+ } else {
+ try {
+ const result = await inheritParentPermissionGroupPermissions(
+ dataroomId,
+ documentIds,
+ folderPath,
+ );
+ if (!result.success) {
+ console.error(
+ "Failed to inherit parent PermissionGroup permissions:",
+ result.error,
+ );
+ onError?.("Failed to inherit parent PermissionGroup permissions");
+ }
+ return result;
+ } catch (error) {
+ console.error(
+ "Failed to inherit parent PermissionGroup permissions:",
+ error,
+ );
+ onError?.("Failed to inherit parent PermissionGroup permissions");
+ return { success: false, error: error instanceof Error ? error.message : "Unknown error" };
+ }
+ }
+ }
+ return { success: true };
+ };
+
return {
applyDefaultPermissions,
inheritParentPermissions,
applyDefaultPermissionGroupPermissions,
- inheritParentPermissionGroupPermissions
+ inheritParentPermissionGroupPermissions,
+ applyPermissionGroupPermissions
};
};
\ No newline at end of file
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
index 53afe5c5..972ffd06 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
@@ -12,11 +12,12 @@ async function applyFolderPermissions(
dataroomId: string,
folderId: string,
folderPath: string,
-) {
+): Promise<void> {
try {
await applyDefaultFolderPermissions(dataroomId, folderId);
} catch (error) {
console.error("Error applying folder permissions:", error);
+ throw error;
}
}
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/inherit-parent-permissions.ts b/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/inherit-parent-permissions.ts
index e30d0b9b..ef6a490c 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/inherit-parent-permissions.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/permission-groups/inherit-parent-permissions.ts
@@ -140,9 +140,7 @@ export default async function handler(
});
});
- // Apply false permissions in a transaction
await prisma.$transaction(async (tx) => {
- // First, remove any existing permissions for these documents and groups
await tx.permissionGroupAccessControls.deleteMany({
where: {
itemId: {
@@ -155,7 +153,6 @@ export default async function handler(
},
});
- // Then create the false permissions
await tx.permissionGroupAccessControls.createMany({
data: permissionsToCreate,
skipDuplicates: true,