2025-01-02 04:33:37 +00:00
|
|
|
import type { DocumentDistributionMethod, DocumentSigningOrder } from '@prisma/client';
|
2023-09-22 12:27:54 +00:00
|
|
|
|
2024-02-12 01:04:53 +00:00
|
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
2025-01-11 04:33:20 +00:00
|
|
|
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
2024-02-12 01:04:53 +00:00
|
|
|
import {
|
|
|
|
|
createDocumentAuditLogData,
|
|
|
|
|
diffDocumentMetaChanges,
|
|
|
|
|
} from '@documenso/lib/utils/document-audit-logs';
|
2023-09-22 12:27:54 +00:00
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
|
|
2024-11-05 00:52:54 +00:00
|
|
|
import type { SupportedLanguageCodes } from '../../constants/i18n';
|
2025-01-13 13:43:35 +00:00
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
2024-11-08 04:32:13 +00:00
|
|
|
import type { TDocumentEmailSettings } from '../../types/document-email';
|
2025-06-10 01:49:52 +00:00
|
|
|
import { getDocumentWhereInput } from '../document/get-document-by-id';
|
2024-11-05 00:52:54 +00:00
|
|
|
|
2023-09-22 12:27:54 +00:00
|
|
|
export type CreateDocumentMetaOptions = {
|
2025-01-11 04:33:20 +00:00
|
|
|
userId: number;
|
2025-06-10 01:49:52 +00:00
|
|
|
teamId: number;
|
2023-09-22 12:27:54 +00:00
|
|
|
documentId: number;
|
2024-01-12 15:24:59 +00:00
|
|
|
subject?: string;
|
|
|
|
|
message?: string;
|
|
|
|
|
timezone?: string;
|
2024-01-17 06:17:08 +00:00
|
|
|
password?: string;
|
2024-01-12 15:24:59 +00:00
|
|
|
dateFormat?: string;
|
2024-01-31 12:47:43 +00:00
|
|
|
redirectUrl?: string;
|
2025-07-24 06:05:00 +00:00
|
|
|
emailId?: string | null;
|
|
|
|
|
emailReplyTo?: string | null;
|
2024-11-08 04:32:13 +00:00
|
|
|
emailSettings?: TDocumentEmailSettings;
|
2024-09-16 12:36:45 +00:00
|
|
|
signingOrder?: DocumentSigningOrder;
|
2025-03-21 02:27:04 +00:00
|
|
|
allowDictateNextSigner?: boolean;
|
2024-11-08 04:32:13 +00:00
|
|
|
distributionMethod?: DocumentDistributionMethod;
|
2024-10-18 03:25:19 +00:00
|
|
|
typedSignatureEnabled?: boolean;
|
2025-03-24 04:25:29 +00:00
|
|
|
uploadSignatureEnabled?: boolean;
|
|
|
|
|
drawSignatureEnabled?: boolean;
|
2024-11-05 00:52:54 +00:00
|
|
|
language?: SupportedLanguageCodes;
|
2025-01-11 04:33:20 +00:00
|
|
|
requestMetadata: ApiRequestMetadata;
|
2023-09-22 12:27:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const upsertDocumentMeta = async ({
|
2025-01-11 04:33:20 +00:00
|
|
|
userId,
|
|
|
|
|
teamId,
|
2023-09-22 12:27:54 +00:00
|
|
|
subject,
|
|
|
|
|
message,
|
2023-12-26 23:50:40 +00:00
|
|
|
timezone,
|
|
|
|
|
dateFormat,
|
2023-09-22 12:27:54 +00:00
|
|
|
documentId,
|
2024-01-17 06:17:08 +00:00
|
|
|
password,
|
2024-01-31 12:47:43 +00:00
|
|
|
redirectUrl,
|
2024-09-16 12:36:45 +00:00
|
|
|
signingOrder,
|
2025-03-21 02:27:04 +00:00
|
|
|
allowDictateNextSigner,
|
2025-07-24 06:05:00 +00:00
|
|
|
emailId,
|
|
|
|
|
emailReplyTo,
|
2024-11-08 04:32:13 +00:00
|
|
|
emailSettings,
|
|
|
|
|
distributionMethod,
|
2024-10-18 03:25:19 +00:00
|
|
|
typedSignatureEnabled,
|
2025-03-24 04:25:29 +00:00
|
|
|
uploadSignatureEnabled,
|
|
|
|
|
drawSignatureEnabled,
|
2024-11-05 00:52:54 +00:00
|
|
|
language,
|
2024-02-12 01:04:53 +00:00
|
|
|
requestMetadata,
|
2023-09-22 12:27:54 +00:00
|
|
|
}: CreateDocumentMetaOptions) => {
|
2025-07-24 06:05:00 +00:00
|
|
|
const { documentWhereInput, team } = await getDocumentWhereInput({
|
2025-06-10 01:49:52 +00:00
|
|
|
documentId,
|
|
|
|
|
userId,
|
|
|
|
|
teamId,
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-13 13:43:35 +00:00
|
|
|
const document = await prisma.document.findFirst({
|
2025-06-10 01:49:52 +00:00
|
|
|
where: documentWhereInput,
|
2024-02-12 01:04:53 +00:00
|
|
|
include: {
|
|
|
|
|
documentMeta: true,
|
|
|
|
|
},
|
2024-01-03 09:10:50 +00:00
|
|
|
});
|
|
|
|
|
|
2025-01-13 13:43:35 +00:00
|
|
|
if (!document) {
|
|
|
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
|
|
|
message: 'Document not found',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { documentMeta: originalDocumentMeta } = document;
|
|
|
|
|
|
2025-07-24 06:05:00 +00:00
|
|
|
// Validate the emailId belongs to the organisation.
|
|
|
|
|
if (emailId) {
|
|
|
|
|
const email = await prisma.organisationEmail.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
id: emailId,
|
|
|
|
|
organisationId: team.organisationId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!email) {
|
|
|
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
|
|
|
message: 'Email not found',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-12 01:04:53 +00:00
|
|
|
return await prisma.$transaction(async (tx) => {
|
|
|
|
|
const upsertedDocumentMeta = await tx.documentMeta.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
documentId,
|
|
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
subject,
|
|
|
|
|
message,
|
|
|
|
|
password,
|
|
|
|
|
dateFormat,
|
|
|
|
|
timezone,
|
|
|
|
|
documentId,
|
|
|
|
|
redirectUrl,
|
2024-09-16 12:36:45 +00:00
|
|
|
signingOrder,
|
2025-03-21 02:27:04 +00:00
|
|
|
allowDictateNextSigner,
|
2025-07-24 06:05:00 +00:00
|
|
|
emailId,
|
|
|
|
|
emailReplyTo,
|
2024-11-08 04:32:13 +00:00
|
|
|
emailSettings,
|
|
|
|
|
distributionMethod,
|
2024-10-18 03:25:19 +00:00
|
|
|
typedSignatureEnabled,
|
2025-03-24 04:25:29 +00:00
|
|
|
uploadSignatureEnabled,
|
|
|
|
|
drawSignatureEnabled,
|
2024-11-05 00:52:54 +00:00
|
|
|
language,
|
2024-02-12 01:04:53 +00:00
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
subject,
|
|
|
|
|
message,
|
|
|
|
|
password,
|
|
|
|
|
dateFormat,
|
|
|
|
|
timezone,
|
|
|
|
|
redirectUrl,
|
2024-09-16 12:36:45 +00:00
|
|
|
signingOrder,
|
2025-03-21 02:27:04 +00:00
|
|
|
allowDictateNextSigner,
|
2025-07-24 06:05:00 +00:00
|
|
|
emailId,
|
|
|
|
|
emailReplyTo,
|
2024-11-08 04:32:13 +00:00
|
|
|
emailSettings,
|
|
|
|
|
distributionMethod,
|
2024-10-18 03:25:19 +00:00
|
|
|
typedSignatureEnabled,
|
2025-03-24 04:25:29 +00:00
|
|
|
uploadSignatureEnabled,
|
|
|
|
|
drawSignatureEnabled,
|
2024-11-05 00:52:54 +00:00
|
|
|
language,
|
2024-02-12 01:04:53 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-15 07:20:10 +00:00
|
|
|
const changes = diffDocumentMetaChanges(originalDocumentMeta ?? {}, upsertedDocumentMeta);
|
|
|
|
|
|
|
|
|
|
if (changes.length > 0) {
|
|
|
|
|
await tx.documentAuditLog.create({
|
|
|
|
|
data: createDocumentAuditLogData({
|
|
|
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_META_UPDATED,
|
|
|
|
|
documentId,
|
2025-01-11 04:33:20 +00:00
|
|
|
metadata: requestMetadata,
|
2024-02-15 07:20:10 +00:00
|
|
|
data: {
|
|
|
|
|
changes: diffDocumentMetaChanges(originalDocumentMeta ?? {}, upsertedDocumentMeta),
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-12 01:04:53 +00:00
|
|
|
|
|
|
|
|
return upsertedDocumentMeta;
|
2023-09-22 12:27:54 +00:00
|
|
|
});
|
|
|
|
|
};
|