Files
sign/packages/lib/server-only/document/complete-document-with-token.ts

168 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-08-17 19:56:18 +10:00
'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';
2023-08-17 19:56:18 +10:00
import { prisma } from '@documenso/prisma';
import { DocumentStatus, SigningStatus } from '@documenso/prisma/client';
2024-02-16 11:04:11 +02:00
import { WebhookTriggerEvents } from '@documenso/prisma/client';
2023-08-17 19:56:18 +10:00
2024-03-28 13:13:29 +08:00
import type { TRecipientActionAuth } from '../../types/document-auth';
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
2023-08-17 19:56:18 +10:00
import { sealDocument } from './seal-document';
2023-09-17 14:31:44 +00:00
import { sendPendingEmail } from './send-pending-email';
2023-08-17 19:56:18 +10:00
export type CompleteDocumentWithTokenOptions = {
token: string;
documentId: number;
2024-03-28 13:13:29 +08:00
userId?: number;
authOptions?: TRecipientActionAuth;
requestMetadata?: RequestMetadata;
2023-08-17 19:56:18 +10:00
};
2024-02-16 11:04:11 +02:00
const getDocument = async ({ token, documentId }: CompleteDocumentWithTokenOptions) => {
return await prisma.document.findFirstOrThrow({
2023-08-17 19:56:18 +10:00
where: {
id: documentId,
Recipient: {
some: {
token,
},
},
},
include: {
Recipient: {
where: {
token,
},
},
},
});
2024-02-16 11:04:11 +02:00
};
export const completeDocumentWithToken = async ({
token,
documentId,
2024-02-16 12:12:54 +02:00
requestMetadata,
2024-02-16 11:04:11 +02:00
}: CompleteDocumentWithTokenOptions) => {
'use server';
const document = await getDocument({ token, documentId });
2023-08-17 19:56:18 +10:00
if (document.status === DocumentStatus.COMPLETED) {
throw new Error(`Document ${document.id} has already been completed`);
}
if (document.Recipient.length === 0) {
throw new Error(`Document ${document.id} has no recipient with token ${token}`);
}
const [recipient] = document.Recipient;
if (recipient.signingStatus === SigningStatus.SIGNED) {
throw new Error(`Recipient ${recipient.id} has already signed`);
}
const fields = await prisma.field.findMany({
where: {
documentId: document.id,
recipientId: recipient.id,
},
});
if (fields.some((field) => !field.inserted)) {
throw new Error(`Recipient ${recipient.id} has unsigned fields`);
}
2024-03-28 13:13:29 +08:00
// Document reauth for completing documents is currently not required.
// const { derivedRecipientActionAuth } = extractDocumentAuthMethods({
// documentAuth: document.authOptions,
// recipientAuth: recipient.authOptions,
// });
// const isValid = await isRecipientAuthorized({
// type: 'ACTION',
// document: document,
// recipient: recipient,
// userId,
// authOptions,
// });
// if (!isValid) {
// throw new AppError(AppErrorCode.UNAUTHORIZED, 'Invalid authentication values');
// }
await prisma.$transaction(async (tx) => {
await tx.recipient.update({
where: {
id: recipient.id,
},
data: {
2024-03-28 13:13:29 +08:00
signingStatus: SigningStatus.SIGNED,
signedAt: new Date(),
},
2024-03-28 13:13:29 +08:00
});
await tx.documentAuditLog.create({
data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_RECIPIENT_COMPLETED,
documentId: document.id,
user: {
name: recipient.name,
email: recipient.email,
},
requestMetadata,
data: {
recipientEmail: recipient.email,
recipientName: recipient.name,
recipientId: recipient.id,
recipientRole: recipient.role,
// actionAuth: derivedRecipientActionAuth || undefined,
},
}),
});
});
2023-09-22 23:22:48 +10:00
const pendingRecipients = await prisma.recipient.count({
where: {
documentId: document.id,
2023-09-17 14:38:39 +00:00
signingStatus: {
not: SigningStatus.SIGNED,
},
},
});
2023-09-22 23:22:48 +10:00
if (pendingRecipients > 0) {
await sendPendingEmail({ documentId, recipientId: recipient.id });
}
2023-08-17 19:56:18 +10:00
const documents = await prisma.document.updateMany({
where: {
id: document.id,
Recipient: {
every: {
signingStatus: SigningStatus.SIGNED,
},
},
},
data: {
status: DocumentStatus.COMPLETED,
2023-11-03 15:48:19 +11:00
completedAt: new Date(),
2023-08-17 19:56:18 +10:00
},
});
if (documents.count > 0) {
await sealDocument({ documentId: document.id, requestMetadata });
2023-08-17 19:56:18 +10:00
}
2024-02-16 11:04:11 +02:00
const updatedDocument = await getDocument({ token, documentId });
await triggerWebhook({
event: WebhookTriggerEvents.DOCUMENT_SIGNED,
data: updatedDocument,
userId: updatedDocument.userId,
teamId: updatedDocument.teamId ?? undefined,
2024-02-16 11:04:11 +02:00
});
2023-08-17 19:56:18 +10:00
};