mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
Compare commits
1 Commits
717fa8f870
...
fix/duplic
| Author | SHA1 | Date | |
|---|---|---|---|
| f51f5426ef |
@ -1,4 +1,6 @@
|
|||||||
import { DocumentSource, type Prisma, WebhookTriggerEvents } from '@prisma/client';
|
import type { Prisma, Recipient } from '@prisma/client';
|
||||||
|
import { DocumentSource, WebhookTriggerEvents } from '@prisma/client';
|
||||||
|
import { omit } from 'remeda';
|
||||||
|
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
@ -7,7 +9,7 @@ import {
|
|||||||
ZWebhookDocumentSchema,
|
ZWebhookDocumentSchema,
|
||||||
mapDocumentToWebhookDocumentPayload,
|
mapDocumentToWebhookDocumentPayload,
|
||||||
} from '../../types/webhook-payload';
|
} from '../../types/webhook-payload';
|
||||||
import { prefixedId } from '../../universal/id';
|
import { nanoid, prefixedId } from '../../universal/id';
|
||||||
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
||||||
import { getDocumentWhereInput } from './get-document-by-id';
|
import { getDocumentWhereInput } from './get-document-by-id';
|
||||||
|
|
||||||
@ -40,14 +42,17 @@ export const duplicateDocument = async ({
|
|||||||
type: true,
|
type: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
documentMeta: {
|
authOptions: true,
|
||||||
|
visibility: true,
|
||||||
|
documentMeta: true,
|
||||||
|
recipients: {
|
||||||
select: {
|
select: {
|
||||||
message: true,
|
id: true,
|
||||||
subject: true,
|
email: true,
|
||||||
dateFormat: true,
|
name: true,
|
||||||
password: true,
|
role: true,
|
||||||
timezone: true,
|
signingOrder: true,
|
||||||
redirectUrl: true,
|
fields: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -59,56 +64,83 @@ export const duplicateDocument = async ({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const createDocumentArguments: Prisma.DocumentCreateArgs = {
|
const documentData = await prisma.documentData.create({
|
||||||
data: {
|
data: {
|
||||||
title: document.title,
|
type: document.documentData.type,
|
||||||
qrToken: prefixedId('qr'),
|
data: document.documentData.initialData,
|
||||||
user: {
|
initialData: document.documentData.initialData,
|
||||||
connect: {
|
|
||||||
id: document.userId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
team: {
|
|
||||||
connect: {
|
|
||||||
id: teamId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
documentData: {
|
|
||||||
create: {
|
|
||||||
...document.documentData,
|
|
||||||
data: document.documentData.initialData,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
documentMeta: {
|
|
||||||
create: {
|
|
||||||
...document.documentMeta,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
source: DocumentSource.DOCUMENT,
|
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
if (teamId !== undefined) {
|
let documentMeta: Prisma.DocumentCreateArgs['data']['documentMeta'] | undefined = undefined;
|
||||||
createDocumentArguments.data.team = {
|
|
||||||
connect: {
|
if (document.documentMeta) {
|
||||||
id: teamId,
|
documentMeta = {
|
||||||
|
create: {
|
||||||
|
...omit(document.documentMeta, ['id', 'documentId']),
|
||||||
|
emailSettings: document.documentMeta.emailSettings || undefined,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const createdDocument = await prisma.document.create({
|
const createdDocument = await prisma.document.create({
|
||||||
...createDocumentArguments,
|
data: {
|
||||||
|
userId: document.userId,
|
||||||
|
teamId: teamId,
|
||||||
|
title: document.title,
|
||||||
|
documentDataId: documentData.id,
|
||||||
|
authOptions: document.authOptions || undefined,
|
||||||
|
visibility: document.visibility,
|
||||||
|
qrToken: prefixedId('qr'),
|
||||||
|
documentMeta,
|
||||||
|
source: DocumentSource.DOCUMENT,
|
||||||
|
},
|
||||||
include: {
|
include: {
|
||||||
recipients: true,
|
recipients: true,
|
||||||
documentMeta: true,
|
documentMeta: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const recipientsToCreate = document.recipients.map((recipient) => ({
|
||||||
|
documentId: createdDocument.id,
|
||||||
|
email: recipient.email,
|
||||||
|
name: recipient.name,
|
||||||
|
role: recipient.role,
|
||||||
|
signingOrder: recipient.signingOrder,
|
||||||
|
token: nanoid(),
|
||||||
|
fields: {
|
||||||
|
createMany: {
|
||||||
|
data: recipient.fields.map((field) => ({
|
||||||
|
documentId: createdDocument.id,
|
||||||
|
type: field.type,
|
||||||
|
page: field.page,
|
||||||
|
positionX: field.positionX,
|
||||||
|
positionY: field.positionY,
|
||||||
|
width: field.width,
|
||||||
|
height: field.height,
|
||||||
|
customText: '',
|
||||||
|
inserted: false,
|
||||||
|
fieldMeta: field.fieldMeta as PrismaJson.FieldMeta,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
const recipients: Recipient[] = [];
|
||||||
|
|
||||||
|
for (const recipientData of recipientsToCreate) {
|
||||||
|
const newRecipient = await prisma.recipient.create({
|
||||||
|
data: recipientData,
|
||||||
|
});
|
||||||
|
|
||||||
|
recipients.push(newRecipient);
|
||||||
|
}
|
||||||
|
|
||||||
await triggerWebhook({
|
await triggerWebhook({
|
||||||
event: WebhookTriggerEvents.DOCUMENT_CREATED,
|
event: WebhookTriggerEvents.DOCUMENT_CREATED,
|
||||||
data: ZWebhookDocumentSchema.parse({
|
data: ZWebhookDocumentSchema.parse({
|
||||||
...mapDocumentToWebhookDocumentPayload(createdDocument),
|
...mapDocumentToWebhookDocumentPayload(createdDocument),
|
||||||
recipients: createdDocument.recipients,
|
recipients,
|
||||||
documentMeta: createdDocument.documentMeta,
|
documentMeta: createdDocument.documentMeta,
|
||||||
}),
|
}),
|
||||||
userId: userId,
|
userId: userId,
|
||||||
|
|||||||
@ -23,8 +23,16 @@ export const duplicateTemplate = async ({
|
|||||||
team: buildTeamWhereQuery({ teamId, userId }),
|
team: buildTeamWhereQuery({ teamId, userId }),
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
recipients: true,
|
recipients: {
|
||||||
fields: true,
|
select: {
|
||||||
|
id: true,
|
||||||
|
email: true,
|
||||||
|
name: true,
|
||||||
|
role: true,
|
||||||
|
signingOrder: true,
|
||||||
|
fields: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
templateDocumentData: true,
|
templateDocumentData: true,
|
||||||
templateMeta: true,
|
templateMeta: true,
|
||||||
},
|
},
|
||||||
@ -59,13 +67,8 @@ export const duplicateTemplate = async ({
|
|||||||
teamId,
|
teamId,
|
||||||
title: template.title + ' (copy)',
|
title: template.title + ' (copy)',
|
||||||
templateDocumentDataId: documentData.id,
|
templateDocumentDataId: documentData.id,
|
||||||
recipients: {
|
authOptions: template.authOptions || undefined,
|
||||||
create: template.recipients.map((recipient) => ({
|
visibility: template.visibility,
|
||||||
email: recipient.email,
|
|
||||||
name: recipient.name,
|
|
||||||
token: nanoid(),
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
templateMeta,
|
templateMeta,
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
@ -73,32 +76,36 @@ export const duplicateTemplate = async ({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await prisma.field.createMany({
|
const recipientsToCreate = template.recipients.map((recipient) => ({
|
||||||
data: template.fields.map((field) => {
|
templateId: duplicatedTemplate.id,
|
||||||
const recipient = template.recipients.find((recipient) => recipient.id === field.recipientId);
|
email: recipient.email,
|
||||||
|
name: recipient.name,
|
||||||
|
role: recipient.role,
|
||||||
|
signingOrder: recipient.signingOrder,
|
||||||
|
token: nanoid(),
|
||||||
|
fields: {
|
||||||
|
createMany: {
|
||||||
|
data: recipient.fields.map((field) => ({
|
||||||
|
templateId: duplicatedTemplate.id,
|
||||||
|
type: field.type,
|
||||||
|
page: field.page,
|
||||||
|
positionX: field.positionX,
|
||||||
|
positionY: field.positionY,
|
||||||
|
width: field.width,
|
||||||
|
height: field.height,
|
||||||
|
customText: '',
|
||||||
|
inserted: false,
|
||||||
|
fieldMeta: field.fieldMeta as PrismaJson.FieldMeta,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
const duplicatedTemplateRecipient = duplicatedTemplate.recipients.find(
|
for (const recipientData of recipientsToCreate) {
|
||||||
(doc) => doc.email === recipient?.email,
|
await prisma.recipient.create({
|
||||||
);
|
data: recipientData,
|
||||||
|
});
|
||||||
if (!duplicatedTemplateRecipient) {
|
}
|
||||||
throw new Error('Recipient not found.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
type: field.type,
|
|
||||||
page: field.page,
|
|
||||||
positionX: field.positionX,
|
|
||||||
positionY: field.positionY,
|
|
||||||
width: field.width,
|
|
||||||
height: field.height,
|
|
||||||
customText: field.customText,
|
|
||||||
inserted: field.inserted,
|
|
||||||
templateId: duplicatedTemplate.id,
|
|
||||||
recipientId: duplicatedTemplateRecipient.id,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
return duplicatedTemplate;
|
return duplicatedTemplate;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user