mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: enable optional fields (#1470)
This commit is contained in:
@ -23,6 +23,7 @@ import { DOCUMENT_AUDIT_LOG_TYPE } from '../../../types/document-audit-logs';
|
||||
import { ZWebhookDocumentSchema } from '../../../types/webhook-payload';
|
||||
import { getFile } from '../../../universal/upload/get-file';
|
||||
import { putPdfFile } from '../../../universal/upload/put-file';
|
||||
import { fieldsContainUnsignedRequiredField } from '../../../utils/advanced-fields-helpers';
|
||||
import { createDocumentAuditLogData } from '../../../utils/document-audit-logs';
|
||||
import type { JobRunIO } from '../../client/_internal/job';
|
||||
import type { TSealDocumentJobDefinition } from './seal-document';
|
||||
@ -105,8 +106,8 @@ export const run = async ({
|
||||
},
|
||||
});
|
||||
|
||||
if (fields.some((field) => !field.inserted)) {
|
||||
throw new Error(`Document ${document.id} has unsigned fields`);
|
||||
if (fieldsContainUnsignedRequiredField(fields)) {
|
||||
throw new Error(`Document ${document.id} has unsigned required fields`);
|
||||
}
|
||||
|
||||
if (isResealing) {
|
||||
@ -147,7 +148,9 @@ export const run = async ({
|
||||
}
|
||||
|
||||
for (const field of fields) {
|
||||
await insertFieldInPDF(pdfDoc, field);
|
||||
if (field.inserted) {
|
||||
await insertFieldInPDF(pdfDoc, field);
|
||||
}
|
||||
}
|
||||
|
||||
// Re-flatten the form to handle our checkbox and radio fields that
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
||||
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||
import { fieldsContainUnsignedRequiredField } from '@documenso/lib/utils/advanced-fields-helpers';
|
||||
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import {
|
||||
@ -85,7 +86,7 @@ export const completeDocumentWithToken = async ({
|
||||
},
|
||||
});
|
||||
|
||||
if (fields.some((field) => !field.inserted)) {
|
||||
if (fieldsContainUnsignedRequiredField(fields)) {
|
||||
throw new Error(`Recipient ${recipient.id} has unsigned fields`);
|
||||
}
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ import { ZWebhookDocumentSchema } from '../../types/webhook-payload';
|
||||
import type { RequestMetadata } from '../../universal/extract-request-metadata';
|
||||
import { getFile } from '../../universal/upload/get-file';
|
||||
import { putPdfFile } from '../../universal/upload/put-file';
|
||||
import { fieldsContainUnsignedRequiredField } from '../../utils/advanced-fields-helpers';
|
||||
import { getCertificatePdf } from '../htmltopdf/get-certificate-pdf';
|
||||
import { flattenAnnotations } from '../pdf/flatten-annotations';
|
||||
import { flattenForm } from '../pdf/flatten-form';
|
||||
@ -92,8 +93,8 @@ export const sealDocument = async ({
|
||||
},
|
||||
});
|
||||
|
||||
if (fields.some((field) => !field.inserted)) {
|
||||
throw new Error(`Document ${document.id} has unsigned fields`);
|
||||
if (fieldsContainUnsignedRequiredField(fields)) {
|
||||
throw new Error(`Document ${document.id} has unsigned required fields`);
|
||||
}
|
||||
|
||||
if (isResealing) {
|
||||
|
||||
51
packages/lib/utils/advanced-fields-helpers.ts
Normal file
51
packages/lib/utils/advanced-fields-helpers.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { type Field, FieldType } from '@documenso/prisma/client';
|
||||
|
||||
import { ZFieldMetaSchema } from '../types/field-meta';
|
||||
|
||||
// Currently it seems that the majority of fields have advanced fields for font reasons.
|
||||
// This array should only contain fields that have an optional setting in the fieldMeta.
|
||||
const ADVANCED_FIELD_TYPES_WITH_OPTIONAL_SETTING: FieldType[] = [
|
||||
FieldType.NUMBER,
|
||||
FieldType.TEXT,
|
||||
FieldType.DROPDOWN,
|
||||
FieldType.RADIO,
|
||||
FieldType.CHECKBOX,
|
||||
];
|
||||
|
||||
/**
|
||||
* Whether a field is required to be inserted.
|
||||
*/
|
||||
export const isRequiredField = (field: Field) => {
|
||||
// All fields without the optional metadata are assumed to be required.
|
||||
if (!ADVANCED_FIELD_TYPES_WITH_OPTIONAL_SETTING.includes(field.type)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Not sure why fieldMeta can be optional for advanced fields, but it is.
|
||||
// Therefore we must assume if there is no fieldMeta, then the field is optional.
|
||||
if (!field.fieldMeta) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const parsedData = ZFieldMetaSchema.safeParse(field.fieldMeta);
|
||||
|
||||
// If it fails, assume the field is optional.
|
||||
// This needs to be logged somewhere.
|
||||
if (!parsedData.success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parsedData.data?.required === true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether the provided field is required and not inserted.
|
||||
*/
|
||||
export const isFieldUnsignedAndRequired = (field: Field) =>
|
||||
isRequiredField(field) && !field.inserted;
|
||||
|
||||
/**
|
||||
* Whether the provided fields contains a field that is required to be inserted.
|
||||
*/
|
||||
export const fieldsContainUnsignedRequiredField = (fields: Field[]) =>
|
||||
fields.some(isFieldUnsignedAndRequired);
|
||||
@ -245,7 +245,7 @@ export const AddTemplateSettingsFormPartial = ({
|
||||
</li>
|
||||
<li>
|
||||
<Trans>
|
||||
<strong>Links</strong> - We will generate links which you can send to
|
||||
<strong>None</strong> - We will generate links which you can send to
|
||||
the recipients manually.
|
||||
</Trans>
|
||||
</li>
|
||||
|
||||
Reference in New Issue
Block a user