mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
fix: add document visibility to template (#1566)
Adds the visibility property to templates
This commit is contained in:
@ -213,7 +213,7 @@ export const createDocumentFromTemplate = async ({
|
||||
globalAccessAuth: templateAuthOptions.globalAccessAuth,
|
||||
globalActionAuth: templateAuthOptions.globalActionAuth,
|
||||
}),
|
||||
visibility: template.team?.teamGlobalSettings?.documentVisibility,
|
||||
visibility: template.visibility || template.team?.teamGlobalSettings?.documentVisibility,
|
||||
documentMeta: {
|
||||
create: {
|
||||
subject: override?.subject || template.templateMeta?.subject,
|
||||
|
||||
@ -1,7 +1,13 @@
|
||||
import { match } from 'ts-pattern';
|
||||
import type { z } from 'zod';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import type { Prisma, Template } from '@documenso/prisma/client';
|
||||
import {
|
||||
DocumentVisibility,
|
||||
type Prisma,
|
||||
TeamMemberRole,
|
||||
type Template,
|
||||
} from '@documenso/prisma/client';
|
||||
import {
|
||||
DocumentDataSchema,
|
||||
FieldSchema,
|
||||
@ -12,6 +18,7 @@ import {
|
||||
TemplateSchema,
|
||||
} from '@documenso/prisma/generated/zod';
|
||||
|
||||
import { AppError, AppErrorCode } from '../../errors/app-error';
|
||||
import { type FindResultResponse, ZFindResultResponse } from '../../types/search-params';
|
||||
|
||||
export type FindTemplatesOptions = {
|
||||
@ -52,28 +59,58 @@ export const findTemplates = async ({
|
||||
page = 1,
|
||||
perPage = 10,
|
||||
}: FindTemplatesOptions): Promise<TFindTemplatesResponse> => {
|
||||
let whereFilter: Prisma.TemplateWhereInput = {
|
||||
userId,
|
||||
teamId: null,
|
||||
type,
|
||||
};
|
||||
const whereFilter: Prisma.TemplateWhereInput[] = [];
|
||||
|
||||
if (teamId === undefined) {
|
||||
whereFilter.push({ userId, teamId: null });
|
||||
}
|
||||
|
||||
if (teamId !== undefined) {
|
||||
whereFilter = {
|
||||
team: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
const teamMember = await prisma.teamMember.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
if (!teamMember) {
|
||||
throw new AppError(AppErrorCode.UNAUTHORIZED, {
|
||||
message: 'You are not a member of this team.',
|
||||
});
|
||||
}
|
||||
|
||||
whereFilter.push(
|
||||
{ teamId },
|
||||
{
|
||||
OR: [
|
||||
match(teamMember.role)
|
||||
.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 })),
|
||||
{ userId, teamId },
|
||||
],
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const [data, count] = await Promise.all([
|
||||
prisma.template.findMany({
|
||||
where: whereFilter,
|
||||
where: {
|
||||
type,
|
||||
AND: whereFilter,
|
||||
},
|
||||
include: {
|
||||
templateDocumentData: true,
|
||||
team: {
|
||||
@ -103,7 +140,9 @@ export const findTemplates = async ({
|
||||
},
|
||||
}),
|
||||
prisma.template.count({
|
||||
where: whereFilter,
|
||||
where: {
|
||||
AND: whereFilter,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import type { z } from 'zod';
|
||||
import { isUserEnterprise } from '@documenso/ee/server-only/util/is-document-enterprise';
|
||||
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import type { Template, TemplateMeta } from '@documenso/prisma/client';
|
||||
import type { DocumentVisibility, Template, TemplateMeta } from '@documenso/prisma/client';
|
||||
import { TemplateSchema } from '@documenso/prisma/generated/zod';
|
||||
|
||||
import { AppError, AppErrorCode } from '../../errors/app-error';
|
||||
@ -19,6 +19,7 @@ export type UpdateTemplateSettingsOptions = {
|
||||
data: {
|
||||
title?: string;
|
||||
externalId?: string | null;
|
||||
visibility?: DocumentVisibility;
|
||||
globalAccessAuth?: TDocumentAccessAuthTypes | null;
|
||||
globalActionAuth?: TDocumentActionAuthTypes | null;
|
||||
publicTitle?: string;
|
||||
@ -110,6 +111,7 @@ export const updateTemplateSettings = async ({
|
||||
title: data.title,
|
||||
externalId: data.externalId,
|
||||
type: data.type,
|
||||
visibility: data.visibility,
|
||||
publicDescription: data.publicDescription,
|
||||
publicTitle: data.publicTitle,
|
||||
authOptions,
|
||||
|
||||
Reference in New Issue
Block a user