documenso/packages/lib/server-only/document/viewed-document.ts

117 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-03-15 08:47:52 +00:00
import { EnvelopeType, ReadStatus, SendStatus, WebhookTriggerEvents } from '@prisma/client';
2025-01-02 04:33:37 +00:00
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 09:56:18 +00:00
import { prisma } from '@documenso/prisma';
2024-02-24 09:18:58 +00:00
2024-03-28 05:13:29 +00:00
import type { TDocumentAccessAuthTypes } from '../../types/document-auth';
2025-01-13 02:41:53 +00:00
import {
ZWebhookDocumentSchema,
mapEnvelopeToWebhookDocumentPayload,
2025-01-13 02:41:53 +00:00
} from '../../types/webhook-payload';
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
2023-08-17 09:56:18 +00:00
export type ViewedDocumentOptions = {
token: string;
recipientAccessAuth?: TDocumentAccessAuthTypes[];
requestMetadata?: RequestMetadata;
2023-08-17 09:56:18 +00:00
};
2024-03-28 05:13:29 +00:00
export const viewedDocument = async ({
token,
recipientAccessAuth,
requestMetadata,
}: ViewedDocumentOptions) => {
2023-08-17 09:56:18 +00:00
const recipient = await prisma.recipient.findFirst({
where: {
token,
envelope: {
type: EnvelopeType.DOCUMENT,
},
},
2023-08-17 09:56:18 +00:00
});
if (!recipient) {
2023-08-17 09:56:18 +00:00
return;
}
await prisma.documentAuditLog.create({
data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_VIEWED,
envelopeId: recipient.envelopeId,
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,
2026-04-14 11:01:53 +00:00
// Only set sentAt if not already set (email may have been sent before they opened).
...(!recipient.sentAt ? { sentAt: new Date() } : {}),
},
});
await tx.documentAuditLog.create({
data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED,
envelopeId: recipient.envelopeId,
user: {
name: recipient.name,
email: recipient.email,
},
requestMetadata,
data: {
recipientEmail: recipient.email,
recipientId: recipient.id,
recipientName: recipient.name,
recipientRole: recipient.role,
accessAuth: recipientAccessAuth ?? [],
},
}),
});
2023-08-17 09:56:18 +00:00
});
2024-02-24 09:18:58 +00:00
2026-04-14 11:01:53 +00:00
// Don't schedule reminders for manually distributed documents —
// there's no email pathway to send them through.
const envelope = await prisma.envelope.findUniqueOrThrow({
where: {
id: recipient.envelopeId,
},
include: {
documentMeta: true,
recipients: true,
},
});
2024-02-24 09:18:58 +00:00
await triggerWebhook({
event: WebhookTriggerEvents.DOCUMENT_OPENED,
data: ZWebhookDocumentSchema.parse(mapEnvelopeToWebhookDocumentPayload(envelope)),
userId: envelope.userId,
teamId: envelope.teamId,
2024-02-24 09:18:58 +00:00
});
2023-08-17 09:56:18 +00:00
};