mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
## Description Adds support for rejecting a given document informing the document owner. Flows for resolving a rejection don't currently exist so it's up to the document owner to reach out to the recipient and work out a way to move forward via a new document or offline agreement. ## Related Issue ## Changes Made - Added new rejection properties to the recipient schema - Added API endpoints to support rejection - Added email templates for notifying the document owner and recipient - Added a dialog on the signing page to start the rejection flow. ## Testing Performed - Manually tested the flow end to end - Automated tests are planned
93 lines
2.2 KiB
TypeScript
93 lines
2.2 KiB
TypeScript
import { SigningStatus } from '@prisma/client';
|
|
import { TRPCError } from '@trpc/server';
|
|
|
|
import { jobs } from '@documenso/lib/jobs/client';
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
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';
|
|
|
|
export type RejectDocumentWithTokenOptions = {
|
|
token: string;
|
|
documentId: number;
|
|
reason: string;
|
|
requestMetadata?: RequestMetadata;
|
|
};
|
|
|
|
export async function rejectDocumentWithToken({
|
|
token,
|
|
documentId,
|
|
reason,
|
|
requestMetadata,
|
|
}: RejectDocumentWithTokenOptions) {
|
|
// Find the recipient and document in a single query
|
|
const recipient = await prisma.recipient.findFirst({
|
|
where: {
|
|
token,
|
|
documentId,
|
|
},
|
|
include: {
|
|
Document: {
|
|
include: {
|
|
User: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const document = recipient?.Document;
|
|
|
|
if (!recipient || !document) {
|
|
throw new TRPCError({
|
|
code: 'NOT_FOUND',
|
|
message: 'Document or recipient not found',
|
|
});
|
|
}
|
|
|
|
// Add the audit log entry before updating the recipient
|
|
|
|
// Update the recipient status to rejected
|
|
const [updatedRecipient] = await prisma.$transaction([
|
|
prisma.recipient.update({
|
|
where: {
|
|
id: recipient.id,
|
|
},
|
|
data: {
|
|
signedAt: new Date(),
|
|
signingStatus: SigningStatus.REJECTED,
|
|
rejectionReason: reason,
|
|
},
|
|
}),
|
|
prisma.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
documentId,
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_RECIPIENT_REJECTED,
|
|
user: {
|
|
name: recipient.name,
|
|
email: recipient.email,
|
|
},
|
|
data: {
|
|
recipientEmail: recipient.email,
|
|
recipientName: recipient.name,
|
|
recipientId: recipient.id,
|
|
recipientRole: recipient.role,
|
|
reason,
|
|
},
|
|
requestMetadata,
|
|
}),
|
|
}),
|
|
]);
|
|
|
|
// Send email notifications
|
|
await jobs.triggerJob({
|
|
name: 'send.signing.rejected.emails',
|
|
payload: {
|
|
recipientId: recipient.id,
|
|
documentId,
|
|
},
|
|
});
|
|
|
|
return updatedRecipient;
|
|
}
|