mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { TeamMemberRole } from '@prisma/client';
|
|
import { match } from 'ts-pattern';
|
|
|
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { DocumentVisibility } from '../../types/document-visibility';
|
|
import type { TFolderType } from '../../types/folder-type';
|
|
import { getTeamById } from '../team/get-team';
|
|
|
|
export interface GetFolderByIdOptions {
|
|
userId: number;
|
|
teamId: number;
|
|
folderId?: string;
|
|
type?: TFolderType;
|
|
}
|
|
|
|
export const getFolderById = async ({ userId, teamId, folderId, type }: GetFolderByIdOptions) => {
|
|
const team = await getTeamById({ userId, teamId });
|
|
|
|
const visibilityFilters = match(team.currentTeamRole)
|
|
.with(TeamMemberRole.ADMIN, () => ({
|
|
visibility: {
|
|
in: [
|
|
DocumentVisibility.EVERYONE,
|
|
DocumentVisibility.MANAGER_AND_ABOVE,
|
|
DocumentVisibility.ADMIN,
|
|
],
|
|
},
|
|
}))
|
|
.with(TeamMemberRole.MANAGER, () => ({
|
|
visibility: {
|
|
in: [DocumentVisibility.EVERYONE, DocumentVisibility.MANAGER_AND_ABOVE],
|
|
},
|
|
}))
|
|
.otherwise(() => ({ visibility: DocumentVisibility.EVERYONE }));
|
|
|
|
const whereClause = {
|
|
id: folderId,
|
|
...(type ? { type } : {}),
|
|
OR: [
|
|
{ teamId, ...visibilityFilters },
|
|
{ userId, teamId },
|
|
],
|
|
};
|
|
|
|
const folder = await prisma.folder.findFirst({
|
|
where: whereClause,
|
|
});
|
|
|
|
if (!folder) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Folder not found',
|
|
});
|
|
}
|
|
|
|
return folder;
|
|
};
|