mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
This PR is handles the changes required to support envelopes. The new envelope editor/signing page will be hidden during release. The core changes here is to migrate the documents and templates model to a centralized envelopes model. Even though Documents and Templates are removed, from the user perspective they will still exist as we remap envelopes to documents and templates.
153 lines
4.3 KiB
TypeScript
153 lines
4.3 KiB
TypeScript
import { EnvelopeType, type FieldType } from '@prisma/client';
|
|
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
|
import type { TFieldMetaSchema } from '@documenso/lib/types/field-meta';
|
|
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
|
import {
|
|
createDocumentAuditLogData,
|
|
diffFieldChanges,
|
|
} from '@documenso/lib/utils/document-audit-logs';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
import { mapFieldToLegacyField } from '../../utils/fields';
|
|
import { canRecipientFieldsBeModified } from '../../utils/recipients';
|
|
import { getEnvelopeWhereInput } from '../envelope/get-envelope-by-id';
|
|
|
|
export interface UpdateDocumentFieldsOptions {
|
|
userId: number;
|
|
teamId: number;
|
|
documentId: number;
|
|
fields: {
|
|
id: number;
|
|
type?: FieldType;
|
|
pageNumber?: number;
|
|
pageX?: number;
|
|
pageY?: number;
|
|
width?: number;
|
|
height?: number;
|
|
fieldMeta?: TFieldMetaSchema;
|
|
}[];
|
|
requestMetadata: ApiRequestMetadata;
|
|
}
|
|
|
|
export const updateDocumentFields = async ({
|
|
userId,
|
|
teamId,
|
|
documentId,
|
|
fields,
|
|
requestMetadata,
|
|
}: UpdateDocumentFieldsOptions) => {
|
|
const { envelopeWhereInput } = await getEnvelopeWhereInput({
|
|
id: {
|
|
type: 'documentId',
|
|
id: documentId,
|
|
},
|
|
type: EnvelopeType.DOCUMENT,
|
|
userId,
|
|
teamId,
|
|
});
|
|
|
|
const envelope = await prisma.envelope.findFirst({
|
|
where: envelopeWhereInput,
|
|
include: {
|
|
recipients: true,
|
|
fields: true,
|
|
},
|
|
});
|
|
|
|
if (!envelope) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Document not found',
|
|
});
|
|
}
|
|
|
|
if (envelope.completedAt) {
|
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
|
message: 'Document already complete',
|
|
});
|
|
}
|
|
|
|
const fieldsToUpdate = fields.map((field) => {
|
|
const originalField = envelope.fields.find((existingField) => existingField.id === field.id);
|
|
|
|
if (!originalField) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: `Field with id ${field.id} not found`,
|
|
});
|
|
}
|
|
|
|
const recipient = envelope.recipients.find(
|
|
(recipient) => recipient.id === originalField.recipientId,
|
|
);
|
|
|
|
// Each field MUST have a recipient associated with it.
|
|
if (!recipient) {
|
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
|
message: `Recipient attached to field ${field.id} not found`,
|
|
});
|
|
}
|
|
|
|
// Check whether the recipient associated with the field can be modified.
|
|
if (!canRecipientFieldsBeModified(recipient, envelope.fields)) {
|
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
|
message:
|
|
'Cannot modify a field where the recipient has already interacted with the document',
|
|
});
|
|
}
|
|
|
|
return {
|
|
originalField,
|
|
updateData: field,
|
|
recipientEmail: recipient.email,
|
|
};
|
|
});
|
|
|
|
const updatedFields = await prisma.$transaction(async (tx) => {
|
|
return await Promise.all(
|
|
fieldsToUpdate.map(async ({ originalField, updateData, recipientEmail }) => {
|
|
const updatedField = await tx.field.update({
|
|
where: {
|
|
id: updateData.id,
|
|
},
|
|
data: {
|
|
type: updateData.type,
|
|
page: updateData.pageNumber,
|
|
positionX: updateData.pageX,
|
|
positionY: updateData.pageY,
|
|
width: updateData.width,
|
|
height: updateData.height,
|
|
fieldMeta: updateData.fieldMeta,
|
|
},
|
|
});
|
|
|
|
const changes = diffFieldChanges(originalField, updatedField);
|
|
|
|
// Handle field updated audit log.
|
|
if (changes.length > 0) {
|
|
await tx.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.FIELD_UPDATED,
|
|
envelopeId: envelope.id,
|
|
metadata: requestMetadata,
|
|
data: {
|
|
fieldId: updatedField.secondaryId,
|
|
fieldRecipientEmail: recipientEmail,
|
|
fieldRecipientId: updatedField.recipientId,
|
|
fieldType: updatedField.type,
|
|
changes,
|
|
},
|
|
}),
|
|
});
|
|
}
|
|
|
|
return updatedField;
|
|
}),
|
|
);
|
|
});
|
|
|
|
return {
|
|
fields: updatedFields.map((field) => mapFieldToLegacyField(field, envelope)),
|
|
};
|
|
};
|