Files
sign/packages/lib/server-only/document/duplicate-document-by-id.ts

93 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-12-14 01:23:35 +09:00
import { z } from 'zod';
2023-11-08 09:25:44 +00:00
import { prisma } from '@documenso/prisma';
import { DocumentSource, type Prisma } from '@documenso/prisma/client';
import { getDocumentWhereInput } from './get-document-by-id';
2023-11-08 09:25:44 +00:00
2024-12-14 01:23:35 +09:00
export interface DuplicateDocumentOptions {
2024-12-10 16:11:20 +09:00
documentId: number;
2023-11-08 09:25:44 +00:00
userId: number;
teamId?: number;
2023-11-08 09:25:44 +00:00
}
2024-12-14 01:23:35 +09:00
export const ZDuplicateDocumentResponseSchema = z.object({
documentId: z.number(),
});
export type TDuplicateDocumentResponse = z.infer<typeof ZDuplicateDocumentResponseSchema>;
export const duplicateDocument = async ({
2024-12-10 16:11:20 +09:00
documentId,
userId,
teamId,
2024-12-14 01:23:35 +09:00
}: DuplicateDocumentOptions): Promise<TDuplicateDocumentResponse> => {
const documentWhereInput = await getDocumentWhereInput({
2024-12-10 16:11:20 +09:00
documentId,
userId,
teamId,
});
2023-11-08 09:25:44 +00:00
const document = await prisma.document.findUniqueOrThrow({
where: documentWhereInput,
2023-11-08 09:25:44 +00:00
select: {
title: true,
userId: true,
documentData: {
select: {
data: true,
initialData: true,
type: true,
},
},
documentMeta: {
select: {
message: true,
subject: true,
dateFormat: true,
2024-01-17 17:17:08 +11:00
password: true,
timezone: true,
redirectUrl: true,
2023-11-08 09:25:44 +00:00
},
},
},
});
const createDocumentArguments: Prisma.DocumentCreateArgs = {
2023-11-08 09:25:44 +00:00
data: {
title: document.title,
User: {
connect: {
id: document.userId,
},
},
documentData: {
create: {
...document.documentData,
data: document.documentData.initialData,
},
},
documentMeta: {
create: {
...document.documentMeta,
},
},
source: DocumentSource.DOCUMENT,
2023-11-08 09:25:44 +00:00
},
};
if (teamId !== undefined) {
createDocumentArguments.data.team = {
connect: {
id: teamId,
},
};
}
const createdDocument = await prisma.document.create(createDocumentArguments);
2023-11-08 09:25:44 +00:00
2024-12-14 01:23:35 +09:00
return {
documentId: createdDocument.id,
};
2023-11-08 09:25:44 +00:00
};