Files
sign/packages/lib/server-only/template/create-template.ts

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-12-14 01:23:35 +09:00
import type { z } from 'zod';
2023-10-06 22:54:24 +00:00
import { prisma } from '@documenso/prisma';
2025-01-02 15:33:37 +11:00
import { TemplateSchema } from '@documenso/prisma/generated/zod/modelSchema//TemplateSchema';
2024-02-08 12:33:20 +11:00
import type { TCreateTemplateMutationSchema } from '@documenso/trpc/server/template-router/schema';
2023-10-06 22:54:24 +00:00
import { AppError, AppErrorCode } from '../../errors/app-error';
2023-10-06 22:54:24 +00:00
export type CreateTemplateOptions = TCreateTemplateMutationSchema & {
userId: number;
2024-02-08 12:33:20 +11:00
teamId?: number;
2023-10-06 22:54:24 +00:00
};
2024-12-14 01:23:35 +09:00
export const ZCreateTemplateResponseSchema = TemplateSchema;
export type TCreateTemplateResponse = z.infer<typeof ZCreateTemplateResponseSchema>;
2023-10-06 22:54:24 +00:00
export const createTemplate = async ({
title,
userId,
2024-02-08 12:33:20 +11:00
teamId,
2023-10-06 22:54:24 +00:00
templateDocumentDataId,
}: CreateTemplateOptions) => {
let team = null;
2024-02-08 12:33:20 +11:00
if (teamId) {
team = await prisma.team.findFirst({
2024-02-08 12:33:20 +11:00
where: {
id: teamId,
members: {
some: {
userId,
},
},
},
include: {
teamGlobalSettings: true,
},
2024-02-08 12:33:20 +11:00
});
if (!team) {
throw new AppError(AppErrorCode.NOT_FOUND);
}
2024-02-08 12:33:20 +11:00
}
2023-10-06 22:54:24 +00:00
return await prisma.template.create({
data: {
title,
userId,
templateDocumentDataId,
2024-02-08 12:33:20 +11:00
teamId,
templateMeta: {
create: {
language: team?.teamGlobalSettings?.documentLanguage,
typedSignatureEnabled: team?.teamGlobalSettings?.typedSignatureEnabled ?? true,
uploadSignatureEnabled: team?.teamGlobalSettings?.uploadSignatureEnabled ?? true,
drawSignatureEnabled: team?.teamGlobalSettings?.drawSignatureEnabled ?? true,
},
},
2023-10-06 22:54:24 +00:00
},
});
};