mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
fix: refactor prisma relations (#1581)
This commit is contained in:
@ -32,7 +32,10 @@ import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
||||
import type { TRecipientActionAuthTypes } from '../../types/document-auth';
|
||||
import { DocumentAccessAuth, ZRecipientAuthOptionsSchema } from '../../types/document-auth';
|
||||
import { ZFieldMetaSchema } from '../../types/field-meta';
|
||||
import { ZWebhookDocumentSchema } from '../../types/webhook-payload';
|
||||
import {
|
||||
ZWebhookDocumentSchema,
|
||||
mapDocumentToWebhookDocumentPayload,
|
||||
} from '../../types/webhook-payload';
|
||||
import type { ApiRequestMetadata } from '../../universal/extract-request-metadata';
|
||||
import type { CreateDocumentAuditLogDataResponse } from '../../utils/document-audit-logs';
|
||||
import { createDocumentAuditLogData } from '../../utils/document-audit-logs';
|
||||
@ -64,7 +67,7 @@ export type CreateDocumentFromDirectTemplateOptions = {
|
||||
};
|
||||
|
||||
type CreatedDirectRecipientField = {
|
||||
field: Field & { Signature?: Signature | null };
|
||||
field: Field & { signature?: Signature | null };
|
||||
derivedRecipientActionAuth: TRecipientActionAuthTypes | null;
|
||||
};
|
||||
|
||||
@ -95,15 +98,15 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
},
|
||||
},
|
||||
include: {
|
||||
Recipient: {
|
||||
recipients: {
|
||||
include: {
|
||||
Field: true,
|
||||
fields: true,
|
||||
},
|
||||
},
|
||||
directLink: true,
|
||||
templateDocumentData: true,
|
||||
templateMeta: true,
|
||||
User: true,
|
||||
user: true,
|
||||
team: {
|
||||
include: {
|
||||
teamGlobalSettings: true,
|
||||
@ -116,7 +119,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
throw new AppError(AppErrorCode.INVALID_REQUEST, { message: 'Invalid or missing template' });
|
||||
}
|
||||
|
||||
const { Recipient: recipients, directLink, User: templateOwner } = template;
|
||||
const { recipients, directLink, user: templateOwner } = template;
|
||||
|
||||
const directTemplateRecipient = recipients.find(
|
||||
(recipient) => recipient.id === directLink.directTemplateRecipientId,
|
||||
@ -159,7 +162,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
directTemplateRecipient.authOptions,
|
||||
);
|
||||
|
||||
const nonDirectTemplateRecipients = template.Recipient.filter(
|
||||
const nonDirectTemplateRecipients = template.recipients.filter(
|
||||
(recipient) => recipient.id !== directTemplateRecipient.id,
|
||||
);
|
||||
|
||||
@ -173,7 +176,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
|
||||
// Associate, validate and map to a query every direct template recipient field with the provided fields.
|
||||
const createDirectRecipientFieldArgs = await Promise.all(
|
||||
directTemplateRecipient.Field.map(async (templateField) => {
|
||||
directTemplateRecipient.fields.map(async (templateField) => {
|
||||
const signedFieldValue = signedFieldValues.find(
|
||||
(value) => value.fieldId === templateField.id,
|
||||
);
|
||||
@ -268,7 +271,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
globalAccessAuth: templateAuthOptions.globalAccessAuth,
|
||||
globalActionAuth: templateAuthOptions.globalActionAuth,
|
||||
}),
|
||||
Recipient: {
|
||||
recipients: {
|
||||
createMany: {
|
||||
data: nonDirectTemplateRecipients.map((recipient) => {
|
||||
const authOptions = ZRecipientAuthOptionsSchema.parse(recipient?.authOptions);
|
||||
@ -306,7 +309,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
},
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
recipients: true,
|
||||
team: {
|
||||
select: {
|
||||
url: true,
|
||||
@ -318,7 +321,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
let nonDirectRecipientFieldsToCreate: Omit<Field, 'id' | 'secondaryId' | 'templateId'>[] = [];
|
||||
|
||||
Object.values(nonDirectTemplateRecipients).forEach((templateRecipient) => {
|
||||
const recipient = document.Recipient.find(
|
||||
const recipient = document.recipients.find(
|
||||
(recipient) => recipient.email === templateRecipient.email,
|
||||
);
|
||||
|
||||
@ -327,7 +330,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
}
|
||||
|
||||
nonDirectRecipientFieldsToCreate = nonDirectRecipientFieldsToCreate.concat(
|
||||
templateRecipient.Field.map((field) => ({
|
||||
templateRecipient.fields.map((field) => ({
|
||||
documentId: document.id,
|
||||
recipientId: recipient.id,
|
||||
type: field.type,
|
||||
@ -366,7 +369,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
sendStatus: SendStatus.SENT,
|
||||
signedAt: initialRequestTime,
|
||||
signingOrder: directTemplateRecipient.signingOrder,
|
||||
Field: {
|
||||
fields: {
|
||||
createMany: {
|
||||
data: directTemplateNonSignatureFields.map(({ templateField, customText }) => ({
|
||||
documentId: document.id,
|
||||
@ -384,7 +387,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
},
|
||||
},
|
||||
include: {
|
||||
Field: true,
|
||||
fields: true,
|
||||
},
|
||||
});
|
||||
|
||||
@ -410,7 +413,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
customText: '',
|
||||
inserted: true,
|
||||
fieldMeta: templateField.fieldMeta || Prisma.JsonNull,
|
||||
Signature: {
|
||||
signature: {
|
||||
create: {
|
||||
recipientId: createdDirectRecipient.id,
|
||||
signatureImageAsBase64: signature.signatureImageAsBase64,
|
||||
@ -419,7 +422,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
},
|
||||
},
|
||||
include: {
|
||||
Signature: true,
|
||||
signature: true,
|
||||
},
|
||||
});
|
||||
|
||||
@ -432,7 +435,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
);
|
||||
|
||||
const createdDirectRecipientFields: CreatedDirectRecipientField[] = [
|
||||
...createdDirectRecipient.Field.map((field) => ({
|
||||
...createdDirectRecipient.fields.map((field) => ({
|
||||
field,
|
||||
derivedRecipientActionAuth: null,
|
||||
})),
|
||||
@ -501,7 +504,7 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
.with(FieldType.SIGNATURE, FieldType.FREE_SIGNATURE, (type) => ({
|
||||
type,
|
||||
data:
|
||||
field.Signature?.signatureImageAsBase64 || field.Signature?.typedSignature || '',
|
||||
field.signature?.signatureImageAsBase64 || field.signature?.typedSignature || '',
|
||||
}))
|
||||
.with(
|
||||
FieldType.DATE,
|
||||
@ -610,13 +613,13 @@ export const createDocumentFromDirectTemplate = async ({
|
||||
include: {
|
||||
documentData: true,
|
||||
documentMeta: true,
|
||||
Recipient: true,
|
||||
recipients: true,
|
||||
},
|
||||
});
|
||||
|
||||
await triggerWebhook({
|
||||
event: WebhookTriggerEvents.DOCUMENT_SIGNED,
|
||||
data: ZWebhookDocumentSchema.parse(createdDocument),
|
||||
data: ZWebhookDocumentSchema.parse(mapDocumentToWebhookDocumentPayload(createdDocument)),
|
||||
userId: template.userId,
|
||||
teamId: template.teamId ?? undefined,
|
||||
});
|
||||
|
||||
@ -43,8 +43,8 @@ export const createDocumentFromTemplateLegacy = async ({
|
||||
}),
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
Field: true,
|
||||
recipients: true,
|
||||
fields: true,
|
||||
templateDocumentData: true,
|
||||
templateMeta: true,
|
||||
team: {
|
||||
@ -76,8 +76,8 @@ export const createDocumentFromTemplateLegacy = async ({
|
||||
title: template.title,
|
||||
visibility: template.team?.teamGlobalSettings?.documentVisibility,
|
||||
documentDataId: documentData.id,
|
||||
Recipient: {
|
||||
create: template.Recipient.map((recipient) => ({
|
||||
recipients: {
|
||||
create: template.recipients.map((recipient) => ({
|
||||
email: recipient.email,
|
||||
name: recipient.name,
|
||||
role: recipient.role,
|
||||
@ -100,7 +100,7 @@ export const createDocumentFromTemplateLegacy = async ({
|
||||
},
|
||||
|
||||
include: {
|
||||
Recipient: {
|
||||
recipients: {
|
||||
orderBy: {
|
||||
id: 'asc',
|
||||
},
|
||||
@ -110,10 +110,10 @@ export const createDocumentFromTemplateLegacy = async ({
|
||||
});
|
||||
|
||||
await prisma.field.createMany({
|
||||
data: template.Field.map((field) => {
|
||||
const recipient = template.Recipient.find((recipient) => recipient.id === field.recipientId);
|
||||
data: template.fields.map((field) => {
|
||||
const recipient = template.recipients.find((recipient) => recipient.id === field.recipientId);
|
||||
|
||||
const documentRecipient = document.Recipient.find((doc) => doc.email === recipient?.email);
|
||||
const documentRecipient = document.recipients.find((doc) => doc.email === recipient?.email);
|
||||
|
||||
if (!documentRecipient) {
|
||||
throw new Error('Recipient not found.');
|
||||
@ -135,9 +135,9 @@ export const createDocumentFromTemplateLegacy = async ({
|
||||
});
|
||||
|
||||
if (recipients && recipients.length > 0) {
|
||||
document.Recipient = await Promise.all(
|
||||
document.recipients = await Promise.all(
|
||||
recipients.map(async (recipient, index) => {
|
||||
const existingRecipient = document.Recipient.at(index);
|
||||
const existingRecipient = document.recipients.at(index);
|
||||
|
||||
return await prisma.recipient.upsert({
|
||||
where: {
|
||||
|
||||
@ -25,7 +25,10 @@ import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
|
||||
import { ZRecipientAuthOptionsSchema } from '../../types/document-auth';
|
||||
import type { TDocumentEmailSettings } from '../../types/document-email';
|
||||
import { ZFieldMetaSchema } from '../../types/field-meta';
|
||||
import { ZWebhookDocumentSchema } from '../../types/webhook-payload';
|
||||
import {
|
||||
ZWebhookDocumentSchema,
|
||||
mapDocumentToWebhookDocumentPayload,
|
||||
} from '../../types/webhook-payload';
|
||||
import type { ApiRequestMetadata } from '../../universal/extract-request-metadata';
|
||||
import { createDocumentAuditLogData } from '../../utils/document-audit-logs';
|
||||
import {
|
||||
@ -78,7 +81,7 @@ export type CreateDocumentFromTemplateOptions = {
|
||||
|
||||
export const ZCreateDocumentFromTemplateResponseSchema = DocumentSchema.extend({
|
||||
documentData: DocumentDataSchema,
|
||||
Recipient: RecipientSchema.array(),
|
||||
recipients: RecipientSchema.array(),
|
||||
});
|
||||
|
||||
export type TCreateDocumentFromTemplateResponse = z.infer<
|
||||
@ -115,9 +118,9 @@ export const createDocumentFromTemplate = async ({
|
||||
}),
|
||||
},
|
||||
include: {
|
||||
Recipient: {
|
||||
recipients: {
|
||||
include: {
|
||||
Field: true,
|
||||
fields: true,
|
||||
},
|
||||
},
|
||||
templateDocumentData: true,
|
||||
@ -138,7 +141,7 @@ export const createDocumentFromTemplate = async ({
|
||||
|
||||
// Check that all the passed in recipient IDs can be associated with a template recipient.
|
||||
recipients.forEach((recipient) => {
|
||||
const foundRecipient = template.Recipient.find(
|
||||
const foundRecipient = template.recipients.find(
|
||||
(templateRecipient) => templateRecipient.id === recipient.id,
|
||||
);
|
||||
|
||||
@ -153,12 +156,12 @@ export const createDocumentFromTemplate = async ({
|
||||
documentAuth: template.authOptions,
|
||||
});
|
||||
|
||||
const finalRecipients: FinalRecipient[] = template.Recipient.map((templateRecipient) => {
|
||||
const finalRecipients: FinalRecipient[] = template.recipients.map((templateRecipient) => {
|
||||
const foundRecipient = recipients.find((recipient) => recipient.id === templateRecipient.id);
|
||||
|
||||
return {
|
||||
templateRecipientId: templateRecipient.id,
|
||||
fields: templateRecipient.Field,
|
||||
fields: templateRecipient.fields,
|
||||
name: foundRecipient ? (foundRecipient.name ?? '') : templateRecipient.name,
|
||||
email: foundRecipient ? foundRecipient.email : templateRecipient.email,
|
||||
role: templateRecipient.role,
|
||||
@ -233,7 +236,7 @@ export const createDocumentFromTemplate = async ({
|
||||
override?.typedSignatureEnabled ?? template.templateMeta?.typedSignatureEnabled,
|
||||
},
|
||||
},
|
||||
Recipient: {
|
||||
recipients: {
|
||||
createMany: {
|
||||
data: finalRecipients.map((recipient) => {
|
||||
const authOptions = ZRecipientAuthOptionsSchema.parse(recipient?.authOptions);
|
||||
@ -260,7 +263,7 @@ export const createDocumentFromTemplate = async ({
|
||||
},
|
||||
},
|
||||
include: {
|
||||
Recipient: {
|
||||
recipients: {
|
||||
orderBy: {
|
||||
id: 'asc',
|
||||
},
|
||||
@ -272,7 +275,7 @@ export const createDocumentFromTemplate = async ({
|
||||
let fieldsToCreate: Omit<Field, 'id' | 'secondaryId' | 'templateId'>[] = [];
|
||||
|
||||
Object.values(finalRecipients).forEach(({ email, fields }) => {
|
||||
const recipient = document.Recipient.find((recipient) => recipient.email === email);
|
||||
const recipient = document.recipients.find((recipient) => recipient.email === email);
|
||||
|
||||
if (!recipient) {
|
||||
throw new Error('Recipient not found.');
|
||||
@ -323,7 +326,7 @@ export const createDocumentFromTemplate = async ({
|
||||
},
|
||||
include: {
|
||||
documentMeta: true,
|
||||
Recipient: true,
|
||||
recipients: true,
|
||||
},
|
||||
});
|
||||
|
||||
@ -333,7 +336,7 @@ export const createDocumentFromTemplate = async ({
|
||||
|
||||
await triggerWebhook({
|
||||
event: WebhookTriggerEvents.DOCUMENT_CREATED,
|
||||
data: ZWebhookDocumentSchema.parse(createdDocument),
|
||||
data: ZWebhookDocumentSchema.parse(mapDocumentToWebhookDocumentPayload(createdDocument)),
|
||||
userId,
|
||||
teamId,
|
||||
});
|
||||
|
||||
@ -52,7 +52,7 @@ export const createTemplateDirectLink = async ({
|
||||
}),
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
recipients: true,
|
||||
directLink: true,
|
||||
},
|
||||
});
|
||||
@ -67,14 +67,14 @@ export const createTemplateDirectLink = async ({
|
||||
|
||||
if (
|
||||
directRecipientId &&
|
||||
!template.Recipient.find((recipient) => recipient.id === directRecipientId)
|
||||
!template.recipients.find((recipient) => recipient.id === directRecipientId)
|
||||
) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, { message: 'Recipient not found' });
|
||||
}
|
||||
|
||||
if (
|
||||
!directRecipientId &&
|
||||
template.Recipient.find(
|
||||
template.recipients.find(
|
||||
(recipient) => recipient.email.toLowerCase() === DIRECT_TEMPLATE_RECIPIENT_EMAIL,
|
||||
)
|
||||
) {
|
||||
|
||||
@ -37,7 +37,7 @@ export const deleteTemplateDirectLink = async ({
|
||||
},
|
||||
include: {
|
||||
directLink: true,
|
||||
Recipient: true,
|
||||
recipients: true,
|
||||
},
|
||||
});
|
||||
|
||||
@ -60,7 +60,7 @@ export const deleteTemplateDirectLink = async ({
|
||||
id: directLink.directTemplateRecipientId,
|
||||
},
|
||||
data: {
|
||||
...generateAvaliableRecipientPlaceholder(template.Recipient),
|
||||
...generateAvaliableRecipientPlaceholder(template.recipients),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -41,8 +41,8 @@ export const duplicateTemplate = async ({
|
||||
}),
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
Field: true,
|
||||
recipients: true,
|
||||
fields: true,
|
||||
templateDocumentData: true,
|
||||
templateMeta: true,
|
||||
},
|
||||
@ -77,8 +77,8 @@ export const duplicateTemplate = async ({
|
||||
teamId,
|
||||
title: template.title + ' (copy)',
|
||||
templateDocumentDataId: documentData.id,
|
||||
Recipient: {
|
||||
create: template.Recipient.map((recipient) => ({
|
||||
recipients: {
|
||||
create: template.recipients.map((recipient) => ({
|
||||
email: recipient.email,
|
||||
name: recipient.name,
|
||||
token: nanoid(),
|
||||
@ -87,15 +87,15 @@ export const duplicateTemplate = async ({
|
||||
templateMeta,
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
recipients: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.field.createMany({
|
||||
data: template.Field.map((field) => {
|
||||
const recipient = template.Recipient.find((recipient) => recipient.id === field.recipientId);
|
||||
data: template.fields.map((field) => {
|
||||
const recipient = template.recipients.find((recipient) => recipient.id === field.recipientId);
|
||||
|
||||
const duplicatedTemplateRecipient = duplicatedTemplate.Recipient.find(
|
||||
const duplicatedTemplateRecipient = duplicatedTemplate.recipients.find(
|
||||
(doc) => doc.email === recipient?.email,
|
||||
);
|
||||
|
||||
|
||||
@ -36,8 +36,8 @@ export const ZFindTemplatesResponseSchema = ZFindResultResponse.extend({
|
||||
id: true,
|
||||
url: true,
|
||||
}).nullable(),
|
||||
Field: FieldSchema.array(),
|
||||
Recipient: RecipientSchema.array(),
|
||||
fields: FieldSchema.array(),
|
||||
recipients: RecipientSchema.array(),
|
||||
templateMeta: TemplateMetaSchema.pick({
|
||||
signingOrder: true,
|
||||
distributionMethod: true,
|
||||
@ -119,8 +119,8 @@ export const findTemplates = async ({
|
||||
url: true,
|
||||
},
|
||||
},
|
||||
Field: true,
|
||||
Recipient: true,
|
||||
fields: true,
|
||||
recipients: true,
|
||||
templateMeta: true,
|
||||
directLink: {
|
||||
select: {
|
||||
|
||||
@ -16,9 +16,9 @@ export const getTemplateByDirectLinkToken = async ({
|
||||
},
|
||||
include: {
|
||||
directLink: true,
|
||||
Recipient: {
|
||||
recipients: {
|
||||
include: {
|
||||
Field: true,
|
||||
fields: true,
|
||||
},
|
||||
},
|
||||
templateDocumentData: true,
|
||||
@ -28,6 +28,6 @@ export const getTemplateByDirectLinkToken = async ({
|
||||
|
||||
return {
|
||||
...template,
|
||||
Field: template.Recipient.map((recipient) => recipient.Field).flat(),
|
||||
fields: template.recipients.map((recipient) => recipient.fields).flat(),
|
||||
};
|
||||
};
|
||||
|
||||
@ -23,9 +23,9 @@ export const ZGetTemplateByIdResponseSchema = TemplateSchema.extend({
|
||||
directLink: TemplateDirectLinkSchema.nullable(),
|
||||
templateDocumentData: DocumentDataSchema,
|
||||
templateMeta: TemplateMetaSchema.nullable(),
|
||||
Recipient: RecipientSchema.array(),
|
||||
Field: FieldSchema.array(),
|
||||
User: UserSchema.pick({
|
||||
recipients: RecipientSchema.array(),
|
||||
fields: FieldSchema.array(),
|
||||
user: UserSchema.pick({
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
@ -62,9 +62,9 @@ export const getTemplateById = async ({
|
||||
directLink: true,
|
||||
templateDocumentData: true,
|
||||
templateMeta: true,
|
||||
Recipient: true,
|
||||
Field: true,
|
||||
User: {
|
||||
recipients: true,
|
||||
fields: true,
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
|
||||
@ -46,7 +46,7 @@ export const toggleTemplateDirectLink = async ({
|
||||
}),
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
recipients: true,
|
||||
directLink: true,
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user