mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +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.
113 lines
3.0 KiB
TypeScript
113 lines
3.0 KiB
TypeScript
import { EnvelopeType, ReadStatus, SendStatus } from '@prisma/client';
|
|
import { WebhookTriggerEvents } from '@prisma/client';
|
|
|
|
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 type { TDocumentAccessAuthTypes } from '../../types/document-auth';
|
|
import {
|
|
ZWebhookDocumentSchema,
|
|
mapEnvelopeToWebhookDocumentPayload,
|
|
} from '../../types/webhook-payload';
|
|
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
|
|
|
export type ViewedDocumentOptions = {
|
|
token: string;
|
|
recipientAccessAuth?: TDocumentAccessAuthTypes[];
|
|
requestMetadata?: RequestMetadata;
|
|
};
|
|
|
|
export const viewedDocument = async ({
|
|
token,
|
|
recipientAccessAuth,
|
|
requestMetadata,
|
|
}: ViewedDocumentOptions) => {
|
|
const recipient = await prisma.recipient.findFirst({
|
|
where: {
|
|
token,
|
|
envelope: {
|
|
type: EnvelopeType.DOCUMENT,
|
|
},
|
|
},
|
|
include: {
|
|
envelope: {
|
|
include: {
|
|
documentMeta: true,
|
|
recipients: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!recipient) {
|
|
return;
|
|
}
|
|
|
|
const { envelope } = recipient;
|
|
|
|
await prisma.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_VIEWED,
|
|
envelopeId: envelope.id,
|
|
user: {
|
|
name: recipient.name,
|
|
email: recipient.email,
|
|
},
|
|
requestMetadata,
|
|
data: {
|
|
recipientEmail: recipient.email,
|
|
recipientId: recipient.id,
|
|
recipientName: recipient.name,
|
|
recipientRole: recipient.role,
|
|
accessAuth: recipientAccessAuth ?? [],
|
|
},
|
|
}),
|
|
});
|
|
|
|
// Early return if already opened.
|
|
if (recipient.readStatus === ReadStatus.OPENED) {
|
|
return;
|
|
}
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
await tx.recipient.update({
|
|
where: {
|
|
id: recipient.id,
|
|
},
|
|
data: {
|
|
// This handles cases where distribution is done manually
|
|
sendStatus: SendStatus.SENT,
|
|
readStatus: ReadStatus.OPENED,
|
|
},
|
|
});
|
|
|
|
await tx.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED,
|
|
envelopeId: envelope.id,
|
|
user: {
|
|
name: recipient.name,
|
|
email: recipient.email,
|
|
},
|
|
requestMetadata,
|
|
data: {
|
|
recipientEmail: recipient.email,
|
|
recipientId: recipient.id,
|
|
recipientName: recipient.name,
|
|
recipientRole: recipient.role,
|
|
accessAuth: recipientAccessAuth ?? [],
|
|
},
|
|
}),
|
|
});
|
|
});
|
|
|
|
await triggerWebhook({
|
|
event: WebhookTriggerEvents.DOCUMENT_OPENED,
|
|
data: ZWebhookDocumentSchema.parse(mapEnvelopeToWebhookDocumentPayload(envelope)),
|
|
userId: envelope.userId,
|
|
teamId: envelope.teamId,
|
|
});
|
|
};
|