mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
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 { ReadStatus } from '@documenso/prisma/client';
|
|
import { WebhookTriggerEvents } from '@documenso/prisma/client';
|
|
|
|
import type { TDocumentAccessAuthTypes } from '../../types/document-auth';
|
|
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
|
import { getDocumentAndRecipientByToken } from './get-document-by-token';
|
|
|
|
export type ViewedDocumentOptions = {
|
|
token: string;
|
|
recipientAccessAuth?: TDocumentAccessAuthTypes | null;
|
|
requestMetadata?: RequestMetadata;
|
|
};
|
|
|
|
export const viewedDocument = async ({
|
|
token,
|
|
recipientAccessAuth,
|
|
requestMetadata,
|
|
}: ViewedDocumentOptions) => {
|
|
const recipient = await prisma.recipient.findFirst({
|
|
where: {
|
|
token,
|
|
readStatus: ReadStatus.NOT_OPENED,
|
|
},
|
|
});
|
|
|
|
if (!recipient || !recipient.documentId) {
|
|
return;
|
|
}
|
|
|
|
const { documentId } = recipient;
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
await tx.recipient.update({
|
|
where: {
|
|
id: recipient.id,
|
|
},
|
|
data: {
|
|
readStatus: ReadStatus.OPENED,
|
|
},
|
|
});
|
|
|
|
await tx.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_OPENED,
|
|
documentId,
|
|
user: {
|
|
name: recipient.name,
|
|
email: recipient.email,
|
|
},
|
|
requestMetadata,
|
|
data: {
|
|
recipientEmail: recipient.email,
|
|
recipientId: recipient.id,
|
|
recipientName: recipient.name,
|
|
recipientRole: recipient.role,
|
|
accessAuth: recipientAccessAuth || undefined,
|
|
},
|
|
}),
|
|
});
|
|
});
|
|
|
|
const document = await getDocumentAndRecipientByToken({ token, requireAccessAuth: false });
|
|
|
|
await triggerWebhook({
|
|
event: WebhookTriggerEvents.DOCUMENT_OPENED,
|
|
data: document,
|
|
userId: document.userId,
|
|
teamId: document.teamId ?? undefined,
|
|
});
|
|
};
|