mirror of
https://github.com/documenso/documenso.git
synced 2025-11-16 01:32:06 +10:00
fix: refactor
This commit is contained in:
@ -1,6 +1,12 @@
|
||||
import { nanoid } from '@documenso/lib/universal/id';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import type { Recipient } from '@documenso/prisma/client';
|
||||
import type { Field } from '@documenso/prisma/client';
|
||||
import { type Recipient, WebhookTriggerEvents } from '@documenso/prisma/client';
|
||||
|
||||
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
||||
import type { RequestMetadata } from '../../universal/extract-request-metadata';
|
||||
import { createDocumentAuditLogData } from '../../utils/document-audit-logs';
|
||||
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
||||
|
||||
type RecipientWithId = {
|
||||
id: number;
|
||||
@ -8,6 +14,11 @@ type RecipientWithId = {
|
||||
email: string;
|
||||
};
|
||||
|
||||
type FinalRecipient = Pick<Recipient, 'name' | 'email' | 'role'> & {
|
||||
templateRecipientId: number;
|
||||
fields: Field[];
|
||||
};
|
||||
|
||||
export type CreateDocumentFromTemplateOptions = {
|
||||
templateId: number;
|
||||
userId: number;
|
||||
@ -18,6 +29,7 @@ export type CreateDocumentFromTemplateOptions = {
|
||||
name?: string;
|
||||
email: string;
|
||||
}[];
|
||||
requestMetadata?: RequestMetadata;
|
||||
};
|
||||
|
||||
export const createDocumentFromTemplate = async ({
|
||||
@ -25,7 +37,14 @@ export const createDocumentFromTemplate = async ({
|
||||
userId,
|
||||
teamId,
|
||||
recipients,
|
||||
requestMetadata,
|
||||
}: CreateDocumentFromTemplateOptions) => {
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
|
||||
const template = await prisma.template.findUnique({
|
||||
where: {
|
||||
id: templateId,
|
||||
@ -46,10 +65,9 @@ export const createDocumentFromTemplate = async ({
|
||||
}),
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
Field: {
|
||||
Recipient: {
|
||||
include: {
|
||||
Recipient: true,
|
||||
Field: true,
|
||||
},
|
||||
},
|
||||
templateDocumentData: true,
|
||||
@ -64,7 +82,7 @@ export const createDocumentFromTemplate = async ({
|
||||
throw new Error('Invalid number of recipients.');
|
||||
}
|
||||
|
||||
let finalRecipients: Pick<Recipient, 'name' | 'email' | 'role'>[] = [];
|
||||
let finalRecipients: FinalRecipient[] = [];
|
||||
|
||||
if (recipients.length > 0 && Object.prototype.hasOwnProperty.call(recipients[0], 'id')) {
|
||||
finalRecipients = template.Recipient.map((templateRecipient) => {
|
||||
@ -78,6 +96,8 @@ export const createDocumentFromTemplate = async ({
|
||||
}
|
||||
|
||||
return {
|
||||
templateRecipientId: templateRecipient.id,
|
||||
fields: templateRecipient.Field,
|
||||
name: foundRecipient.name ?? '',
|
||||
email: foundRecipient.email,
|
||||
role: templateRecipient.role,
|
||||
@ -87,6 +107,8 @@ export const createDocumentFromTemplate = async ({
|
||||
// Backwards compatible logic for /v1/ API where we use the index to associate
|
||||
// the provided recipient with the template recipient.
|
||||
finalRecipients = recipients.map((recipient, index) => ({
|
||||
templateRecipientId: template.Recipient[index].id,
|
||||
fields: template.Recipient[index].Field,
|
||||
name: recipient.name ?? '',
|
||||
email: recipient.email,
|
||||
role: template.Recipient[index].role,
|
||||
@ -109,12 +131,14 @@ export const createDocumentFromTemplate = async ({
|
||||
title: template.title,
|
||||
documentDataId: documentData.id,
|
||||
Recipient: {
|
||||
create: finalRecipients.map((recipient) => ({
|
||||
email: recipient.email,
|
||||
name: recipient.name,
|
||||
role: recipient.role,
|
||||
token: nanoid(),
|
||||
})),
|
||||
createMany: {
|
||||
data: finalRecipients.map((recipient) => ({
|
||||
email: recipient.email,
|
||||
name: recipient.name,
|
||||
role: recipient.role,
|
||||
token: nanoid(),
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
@ -127,31 +151,54 @@ export const createDocumentFromTemplate = async ({
|
||||
},
|
||||
});
|
||||
|
||||
await tx.field.createMany({
|
||||
data: template.Field.map((field) => {
|
||||
const documentRecipient = document.Recipient.find(
|
||||
(recipient) => recipient.email === field.Recipient.email,
|
||||
);
|
||||
let fieldsToCreate: Omit<Field, 'id' | 'secondaryId' | 'templateId'>[] = [];
|
||||
|
||||
if (!documentRecipient) {
|
||||
throw new Error('Recipient not found.');
|
||||
}
|
||||
Object.values(finalRecipients).forEach(({ email, fields }) => {
|
||||
const recipient = document.Recipient.find((recipient) => recipient.email === email);
|
||||
|
||||
return {
|
||||
if (!recipient) {
|
||||
throw new Error('Recipient not found.');
|
||||
}
|
||||
|
||||
fieldsToCreate = fieldsToCreate.concat(
|
||||
fields.map((field) => ({
|
||||
documentId: document.id,
|
||||
recipientId: recipient.id,
|
||||
type: field.type,
|
||||
page: field.page,
|
||||
positionX: field.positionX,
|
||||
positionY: field.positionY,
|
||||
width: field.width,
|
||||
height: field.height,
|
||||
customText: field.customText,
|
||||
inserted: field.inserted,
|
||||
documentId: document.id,
|
||||
recipientId: documentRecipient.id,
|
||||
};
|
||||
customText: '',
|
||||
inserted: false,
|
||||
})),
|
||||
);
|
||||
});
|
||||
|
||||
await tx.field.createMany({
|
||||
data: fieldsToCreate,
|
||||
});
|
||||
|
||||
await tx.documentAuditLog.create({
|
||||
data: createDocumentAuditLogData({
|
||||
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_CREATED,
|
||||
documentId: document.id,
|
||||
user,
|
||||
requestMetadata,
|
||||
data: {
|
||||
title: document.title,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
await triggerWebhook({
|
||||
event: WebhookTriggerEvents.DOCUMENT_CREATED,
|
||||
data: document,
|
||||
userId,
|
||||
teamId,
|
||||
});
|
||||
|
||||
return document;
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user