2026-03-15 08:47:52 +00:00
|
|
|
import { EnvelopeType, ReadStatus, SendStatus, WebhookTriggerEvents } from '@prisma/client';
|
2025-01-02 04:33:37 +00:00
|
|
|
|
2024-02-12 01:04:53 +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,
|
2025-10-14 10:56:36 +00:00
|
|
|
mapEnvelopeToWebhookDocumentPayload,
|
2025-01-13 02:41:53 +00:00
|
|
|
} from '../../types/webhook-payload';
|
2024-02-27 04:16:14 +00:00
|
|
|
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
2023-08-17 09:56:18 +00:00
|
|
|
|
|
|
|
|
export type ViewedDocumentOptions = {
|
|
|
|
|
token: string;
|
2025-06-06 14:27:19 +00:00
|
|
|
recipientAccessAuth?: TDocumentAccessAuthTypes[];
|
2024-02-12 01:04:53 +00:00
|
|
|
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,
|
2025-10-14 10:56:36 +00:00
|
|
|
envelope: {
|
|
|
|
|
type: EnvelopeType.DOCUMENT,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-08-17 09:56:18 +00:00
|
|
|
});
|
|
|
|
|
|
2025-10-14 10:56:36 +00:00
|
|
|
if (!recipient) {
|
2023-08-17 09:56:18 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 09:11:16 +00:00
|
|
|
await prisma.documentAuditLog.create({
|
|
|
|
|
data: createDocumentAuditLogData({
|
|
|
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_VIEWED,
|
2025-11-18 05:57:14 +00:00
|
|
|
envelopeId: recipient.envelopeId,
|
2025-06-30 09:11:16 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-12 01:04:53 +00:00
|
|
|
await prisma.$transaction(async (tx) => {
|
|
|
|
|
await tx.recipient.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: recipient.id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
2024-12-31 03:54:19 +00:00
|
|
|
// This handles cases where distribution is done manually
|
|
|
|
|
sendStatus: SendStatus.SENT,
|
2024-02-12 01:04:53 +00:00
|
|
|
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() } : {}),
|
2024-02-12 01:04:53 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.documentAuditLog.create({
|
|
|
|
|
data: createDocumentAuditLogData({
|
|
|
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED,
|
2025-11-18 05:57:14 +00:00
|
|
|
envelopeId: recipient.envelopeId,
|
2024-02-12 01:04:53 +00:00
|
|
|
user: {
|
|
|
|
|
name: recipient.name,
|
|
|
|
|
email: recipient.email,
|
|
|
|
|
},
|
|
|
|
|
requestMetadata,
|
|
|
|
|
data: {
|
|
|
|
|
recipientEmail: recipient.email,
|
|
|
|
|
recipientId: recipient.id,
|
|
|
|
|
recipientName: recipient.name,
|
|
|
|
|
recipientRole: recipient.role,
|
2025-06-06 14:27:19 +00:00
|
|
|
accessAuth: recipientAccessAuth ?? [],
|
2024-02-12 01:04:53 +00:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
});
|
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.
|
|
|
|
|
|
2025-11-18 05:57:14 +00:00
|
|
|
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({
|
2024-02-27 04:16:14 +00:00
|
|
|
event: WebhookTriggerEvents.DOCUMENT_OPENED,
|
2025-10-14 10:56:36 +00:00
|
|
|
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
|
|
|
};
|