## Description Currently users can sign and complete draft documents, which will result in a completed document in an invalid state. ## Changes Made - Prevent recipients from inserting or uninserting fields for draft documents - Prevent recipients from completing draft documents - Remove ability to copy signing tokens unless document is pending <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced document status visibility and control across various components in the application. Users can now see and interact with document statuses more dynamically in views like `DocumentPageView`, `DocumentEditPageView`, and `DocumentsDataTable`. - Improved document signing process with updated status checks, ensuring actions like signing, completing, and removing fields are only available under appropriate document statuses. - **Bug Fixes** - Adjusted document status validation logic in server-side operations to prevent actions on incorrectly stated documents, enhancing the overall security and functionality of document processing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
'use server';
|
|
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
|
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
|
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
|
|
import { prisma } from '@documenso/prisma';
|
|
import { DocumentStatus, SigningStatus } from '@documenso/prisma/client';
|
|
|
|
export type RemovedSignedFieldWithTokenOptions = {
|
|
token: string;
|
|
fieldId: number;
|
|
requestMetadata?: RequestMetadata;
|
|
};
|
|
|
|
export const removeSignedFieldWithToken = async ({
|
|
token,
|
|
fieldId,
|
|
requestMetadata,
|
|
}: RemovedSignedFieldWithTokenOptions) => {
|
|
const field = await prisma.field.findFirstOrThrow({
|
|
where: {
|
|
id: fieldId,
|
|
Recipient: {
|
|
token,
|
|
},
|
|
},
|
|
include: {
|
|
Document: true,
|
|
Recipient: true,
|
|
},
|
|
});
|
|
|
|
const { Document: document, Recipient: recipient } = field;
|
|
|
|
if (!document) {
|
|
throw new Error(`Document not found for field ${field.id}`);
|
|
}
|
|
|
|
if (document.status !== DocumentStatus.PENDING) {
|
|
throw new Error(`Document ${document.id} must be pending`);
|
|
}
|
|
|
|
if (recipient?.signingStatus === SigningStatus.SIGNED) {
|
|
throw new Error(`Recipient ${recipient.id} has already signed`);
|
|
}
|
|
|
|
// Unreachable code based on the above query but we need to satisfy TypeScript
|
|
if (field.recipientId === null) {
|
|
throw new Error(`Field ${fieldId} has no recipientId`);
|
|
}
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
await tx.field.update({
|
|
where: {
|
|
id: field.id,
|
|
},
|
|
data: {
|
|
customText: '',
|
|
inserted: false,
|
|
},
|
|
});
|
|
|
|
await tx.signature.deleteMany({
|
|
where: {
|
|
fieldId: field.id,
|
|
},
|
|
});
|
|
|
|
await tx.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_FIELD_UNINSERTED,
|
|
documentId: document.id,
|
|
user: {
|
|
name: recipient?.name,
|
|
email: recipient?.email,
|
|
},
|
|
requestMetadata,
|
|
data: {
|
|
field: field.type,
|
|
fieldId: field.secondaryId,
|
|
},
|
|
}),
|
|
});
|
|
});
|
|
};
|