From da7a4f8890bc08814c7ff168e8ac44011df888f6 Mon Sep 17 00:00:00 2001 From: SepehrYahyaee <7heycallmegray@gmail.com> Date: Wed, 15 Jul 2026 10:33:50 +0330 Subject: [PATCH] YARA-994 and fixed metal plate bug --- .../claim-request-management.service.ts | 12 +++++++++ src/helpers/persian-pdf-document.ts | 26 ++++++++++++++----- src/helpers/persian-pdf-text.ts | 19 +++++++++++--- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/claim-request-management/claim-request-management.service.ts b/src/claim-request-management/claim-request-management.service.ts index d126782..e1d4af5 100644 --- a/src/claim-request-management/claim-request-management.service.ts +++ b/src/claim-request-management/claim-request-management.service.ts @@ -10205,6 +10205,18 @@ export class ClaimRequestManagementService { base: GetCaptureRequirementsV2ResponseDto, ): GetCaptureRequirementsV2ResponseDto { const skipMetalPlate = !!(blame as any)?.isMadeByFileMaker; + + // For V4/V5 files strip metal-plate keys from the base response entirely + // so they never appear in any phase — the front-end should not show them. + if (skipMetalPlate) { + base = { + ...base, + requiredDocuments: base.requiredDocuments.filter( + (d) => !OPTIONAL_CAPTURE_PHASE_DOC_KEYS_V4V5.includes(d.key as any), + ), + }; + } + const step = claimCase.workflow?.currentStep; const capturePartDone = this.claimV3StepCompleted( claimCase, diff --git a/src/helpers/persian-pdf-document.ts b/src/helpers/persian-pdf-document.ts index ebd5f85..9e31cf1 100644 --- a/src/helpers/persian-pdf-document.ts +++ b/src/helpers/persian-pdf-document.ts @@ -22,20 +22,18 @@ const LABEL_COLUMN_X = COLON_X + 10; function resolveFontFile(variant: "regular" | "bold"): string { const file = - variant === "bold" - ? "NotoKufiArabic-Bold.ttf" - : "NotoKufiArabic-Regular.ttf"; + variant === "bold" ? "Vazirmatn-Bold.ttf" : "Vazirmatn-Regular.ttf"; const candidates = [ + join(process.cwd(), "assets", file), join(process.cwd(), "assets", "fonts", file), - join(process.cwd(), "dist", "fonts", file), + join(process.cwd(), "dist", "assets", file), join(process.cwd(), "dist", "assets", "fonts", file), - join("/usr/share/fonts/truetype/noto", file), ]; for (const path of candidates) { if (existsSync(path)) return path; } throw new Error( - `Persian PDF font not found (${file}). Place it under assets/fonts/ or install noto fonts on the host.`, + `Persian PDF font not found (${file}). Place it under assets/ or assets/fonts/.`, ); } @@ -46,6 +44,16 @@ export type PersianPdfDocument = InstanceType & { addBlank: (lines?: number) => void; }; +/** Ensure at least `neededPts` of vertical space remain; add a page if not. */ +function ensureSpace( + doc: InstanceType, + neededPts: number, +): void { + if (doc.y + neededPts > doc.page.maxY()) { + doc.addPage(); + } +} + function drawRtlLine( doc: InstanceType, text: string, @@ -69,6 +77,9 @@ function drawKeyValueRow( label: string, value: string, ): void { + // Reserve one row height (font 10pt ≈ 14 pts with leading) before drawing. + ensureSpace(doc, 16); + const y = doc.y; const { label: faLabel, value: faValue } = pdfKitRtlKeyValue(label, value); @@ -118,6 +129,9 @@ export function createPersianPdfDocument(): PersianPdfDocument { }; doc.addSection = (text: string) => { + // Reserve space for the gap + section heading + at least one data row below it, + // so the heading is never stranded alone at the bottom of a page. + ensureSpace(doc, 60); doc.moveDown(0.5); drawRtlLine(doc, text, 13, true); doc.font("FaRegular").fontSize(10); diff --git a/src/helpers/persian-pdf-text.ts b/src/helpers/persian-pdf-text.ts index 8b59083..5d08c34 100644 --- a/src/helpers/persian-pdf-text.ts +++ b/src/helpers/persian-pdf-text.ts @@ -8,18 +8,31 @@ export function normalizePersianPdfText(text: string): string { .trim(); } +/** + * pdfkit renders glyphs strictly left-to-right. + * fontkit already reshapes each word's glyphs into visual (LTR) order, + * but it does NOT reorder words — so a multi-word RTL string like + * "نام صاحب خودرو" has its words placed LTR on the page and reads + * backwards. Reversing the word sequence here makes pdfkit emit the + * words in the correct visual order for a right-to-left PDF reader. + */ +function reverseRtlWords(text: string): string { + if (!/[\u0600-\u06FF]/.test(text)) return text; + return text.split(" ").reverse().join(" "); +} + export function pdfKitRtlKeyValue( label: string, value: string, ): { label: string; value: string } { return { - label: normalizePersianPdfText(label), - value: normalizePersianPdfText(value), + label: reverseRtlWords(normalizePersianPdfText(label)), + value: reverseRtlWords(normalizePersianPdfText(value)), }; } export function pdfKitRtlParagraph(text: string): string { - return normalizePersianPdfText(text); + return reverseRtlWords(normalizePersianPdfText(text)); } export function isMostlyAscii(text: string): boolean {