mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
Add the ability to insert typed signatures. Once the signature field is placed on the document, a checkbox appears in the document editor where the document owner can allow signers to add typed signatures. Typed signatures are disabled by default. 
120 lines
2.6 KiB
TypeScript
120 lines
2.6 KiB
TypeScript
'use server';
|
|
|
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
|
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
|
import {
|
|
createDocumentAuditLogData,
|
|
diffDocumentMetaChanges,
|
|
} from '@documenso/lib/utils/document-audit-logs';
|
|
import { prisma } from '@documenso/prisma';
|
|
import type { DocumentSigningOrder } from '@documenso/prisma/client';
|
|
|
|
export type CreateDocumentMetaOptions = {
|
|
documentId: number;
|
|
subject?: string;
|
|
message?: string;
|
|
timezone?: string;
|
|
password?: string;
|
|
dateFormat?: string;
|
|
redirectUrl?: string;
|
|
signingOrder?: DocumentSigningOrder;
|
|
typedSignatureEnabled?: boolean;
|
|
userId: number;
|
|
requestMetadata: RequestMetadata;
|
|
};
|
|
|
|
export const upsertDocumentMeta = async ({
|
|
subject,
|
|
message,
|
|
timezone,
|
|
dateFormat,
|
|
documentId,
|
|
password,
|
|
userId,
|
|
redirectUrl,
|
|
signingOrder,
|
|
typedSignatureEnabled,
|
|
requestMetadata,
|
|
}: CreateDocumentMetaOptions) => {
|
|
const user = await prisma.user.findFirstOrThrow({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
name: true,
|
|
},
|
|
});
|
|
|
|
const { documentMeta: originalDocumentMeta } = await prisma.document.findFirstOrThrow({
|
|
where: {
|
|
id: documentId,
|
|
OR: [
|
|
{
|
|
userId: user.id,
|
|
},
|
|
{
|
|
team: {
|
|
members: {
|
|
some: {
|
|
userId: user.id,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
include: {
|
|
documentMeta: true,
|
|
},
|
|
});
|
|
|
|
return await prisma.$transaction(async (tx) => {
|
|
const upsertedDocumentMeta = await tx.documentMeta.upsert({
|
|
where: {
|
|
documentId,
|
|
},
|
|
create: {
|
|
subject,
|
|
message,
|
|
password,
|
|
dateFormat,
|
|
timezone,
|
|
documentId,
|
|
redirectUrl,
|
|
signingOrder,
|
|
typedSignatureEnabled,
|
|
},
|
|
update: {
|
|
subject,
|
|
message,
|
|
password,
|
|
dateFormat,
|
|
timezone,
|
|
redirectUrl,
|
|
signingOrder,
|
|
typedSignatureEnabled,
|
|
},
|
|
});
|
|
|
|
const changes = diffDocumentMetaChanges(originalDocumentMeta ?? {}, upsertedDocumentMeta);
|
|
|
|
if (changes.length > 0) {
|
|
await tx.documentAuditLog.create({
|
|
data: createDocumentAuditLogData({
|
|
type: DOCUMENT_AUDIT_LOG_TYPE.DOCUMENT_META_UPDATED,
|
|
documentId,
|
|
user,
|
|
requestMetadata,
|
|
data: {
|
|
changes: diffDocumentMetaChanges(originalDocumentMeta ?? {}, upsertedDocumentMeta),
|
|
},
|
|
}),
|
|
});
|
|
}
|
|
|
|
return upsertedDocumentMeta;
|
|
});
|
|
};
|