10000 fix: allow prefilling date field by Mythie · Pull Request #1794 · documenso/documenso · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: allow prefilling date field #1794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions packages/lib/server-only/template/create-document-from-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
SigningStatus,
WebhookTriggerEvents,
} from '@prisma/client';
import { DateTime } from 'luxon';
import { match } from 'ts-pattern';

import { nanoid, prefixedId } from '@documenso/lib/universal/id';
import { prisma } from '@documenso/prisma';

import { DEFAULT_DOCUMENT_DATE_FORMAT } from '../../constants/date-formats';
import type { SupportedLanguageCodes } from '../../constants/i18n';
import { AppError, AppErrorCode } from '../../errors/app-error';
import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';
Expand Down Expand Up @@ -508,10 +510,8 @@ export const createDocumentFromTemplate = async ({
fieldsToCreate = fieldsToCreate.concat(
fields.map((field) => {
const prefillField = prefillFields?.find((value) => value.id === field.id);
// Use type assertion to help TypeScript understand the structure
const updatedFieldMeta = getUpdatedFieldMeta(field, prefillField);

return {
const payload = {
documentId: document.id,
recipientId: recipient.id,
type: field.type,
Expand All @@ -522,8 +522,38 @@ export const createDocumentFromTemplate = async ({
height: field.height,
customText: '',
inserted: false,
fieldMeta: updatedFieldMeta,
fieldMeta: field.fieldMeta,
};

if (prefillField) {
match(prefillField)
.with({ type: 'date' }, (selector) => {
if (!selector.value) {
throw new AppError(AppErrorCode.INVALID_BODY, {
message: `Date value is required for field ${field.id}`,
});
}

const date = new Date(selector.value);

if (isNaN(date.getTime())) {
throw new AppError(AppErrorCode.INVALID_BODY, {
message: `Invalid date value for field ${field.id}: ${selector.value}`,
});
}

payload.customText = DateTime.fromJSDate(date).toFormat(
template.templateMeta?.dateFormat ?? DEFAULT_DOCUMENT_DATE_FORMAT,
);

payload.inserted = true;
})
.otherwise((selector) => {
payload.fieldMeta = getUpdatedFieldMeta(field, selector);
});
}

return payload;
}),
);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/types/field-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export const ZFieldMetaPrefillFieldsSchema = z
label: z.string().optional(),
value: z.string().optional(),
}),
z.object({
type: z.literal('date'),
value: z.string().optional(),
}),
]),
);

Expand Down
Loading
0