task-h.5.3

This commit is contained in:
phaichayon
2026-06-19 17:15:28 +07:00
parent fd01ebf7c7
commit 51d67ef7c2
35 changed files with 4301 additions and 633 deletions

View File

@@ -704,6 +704,44 @@ function applyFormatMask(value: unknown, formatMask?: string | null) {
return value;
}
function parseTableDefaultValue(defaultValue?: string | null) {
if (!defaultValue) {
return null;
}
try {
const parsed = JSON.parse(defaultValue) as unknown;
return Array.isArray(parsed) ? parsed : null;
} catch {
return null;
}
}
function isBlankString(value: unknown) {
return typeof value === 'string' && value.trim().length === 0;
}
function resolveScalarMappingValue(
rawValue: unknown,
formattedValue: unknown,
defaultValue?: string | null
) {
if (formattedValue !== undefined && formattedValue !== null && !isBlankString(formattedValue)) {
return formattedValue;
}
if (rawValue !== undefined && rawValue !== null && !isBlankString(rawValue)) {
return rawValue;
}
if (defaultValue !== undefined && defaultValue !== null && defaultValue.trim().length > 0) {
return defaultValue;
}
return '-';
}
export function mapDocumentDataToTemplateInput(
documentData: Record<string, unknown>,
mappings: DocumentTemplateMappingWithColumns[]
@@ -715,9 +753,10 @@ export function mapDocumentDataToTemplateInput(
if (mapping.dataType === 'table') {
const rows = Array.isArray(rawValue) ? rawValue : [];
const defaultRows = parseTableDefaultValue(mapping.defaultValue);
if (rows.length === 0) {
result[mapping.placeholderKey] = normalizePdfmeTable([]);
result[mapping.placeholderKey] = normalizePdfmeTable(defaultRows ?? []);
continue;
}
@@ -760,17 +799,23 @@ export function mapDocumentDataToTemplateInput(
})
.filter(Boolean)
.join('\n\n');
result[mapping.placeholderKey] = normalized || mapping.defaultValue || '';
result[mapping.placeholderKey] = normalized || mapping.defaultValue || '-';
continue;
}
result[mapping.placeholderKey] =
typeof rawValue === 'string' ? rawValue : mapping.defaultValue || '';
typeof rawValue === 'string' && rawValue.trim().length > 0
? rawValue
: mapping.defaultValue || '-';
continue;
}
result[mapping.placeholderKey] =
applyFormatMask(rawValue, mapping.formatMask) ?? mapping.defaultValue ?? '-';
const formattedValue = applyFormatMask(rawValue, mapping.formatMask);
result[mapping.placeholderKey] = resolveScalarMappingValue(
rawValue,
formattedValue,
mapping.defaultValue
);
}
return result;