From 9232e8f654c53a802dd5c016bd6b5d300d3d57ab Mon Sep 17 00:00:00 2001 From: Potenz Date: Wed, 29 Oct 2025 21:20:53 +0530 Subject: [PATCH] feat(check-all-eligibility) - v3 --- apps/Backend/src/routes/insuranceStatus.ts | 28 +++-------------- apps/Backend/src/utils/dateUtils.ts | 35 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/apps/Backend/src/routes/insuranceStatus.ts b/apps/Backend/src/routes/insuranceStatus.ts index 534758b..6b9b932 100644 --- a/apps/Backend/src/routes/insuranceStatus.ts +++ b/apps/Backend/src/routes/insuranceStatus.ts @@ -13,6 +13,7 @@ import { InsertPatient, insertPatientSchema, } from "../../../../packages/db/types/patient-types"; +import { formatDobForAgent } from "../utils/dateUtils"; const router = Router(); @@ -550,30 +551,9 @@ router.post( } // Convert Date object → YYYY-MM-DD string - req for selenium agent. - let dobStr: string; - try { - if (dob instanceof Date) { - const year = dob.getFullYear(); - const month = String(dob.getMonth() + 1).padStart(2, "0"); - const day = String(dob.getDate()).padStart(2, "0"); - dobStr = `${year}-${month}-${day}`; - } else if (typeof dob === "string") { - // handle stored string already formatted - const dateObj = new Date(dob); - if (!isNaN(dateObj.getTime())) { - const year = dateObj.getFullYear(); - const month = String(dateObj.getMonth() + 1).padStart(2, "0"); - const day = String(dateObj.getDate()).padStart(2, "0"); - dobStr = `${year}-${month}-${day}`; - } else { - // assume string is already "YYYY-MM-DD" - dobStr = dob; - } - } else { - throw new Error("Unsupported DOB format"); - } - } catch (fmtErr) { - resultItem.error = `Invalid DOB format for ${patientLabel} — skipping ${aptLabel}`; + const dobStr = formatDobForAgent(dob); + if (!dobStr) { + resultItem.error = `Invalid or missing DOB for ${patientLabel} — skipping ${aptLabel}`; results.push(resultItem); continue; } diff --git a/apps/Backend/src/utils/dateUtils.ts b/apps/Backend/src/utils/dateUtils.ts index 6c81bcc..5df486f 100644 --- a/apps/Backend/src/utils/dateUtils.ts +++ b/apps/Backend/src/utils/dateUtils.ts @@ -24,3 +24,38 @@ export function convertOCRDate(input: string | number | null | undefined): Date return new Date(year, month, day); } + +/** + * Normalize a DOB value to "YYYY-MM-DD" string expected by the Python agent. + * - If dob is already "YYYY-MM-DD" string, returns it. + * - If dob is an ISO datetime string or Date, returns YYYY-MM-DD derived from UTC parts (no timezone shifts). + * - Returns null for invalid values. + */ +export function formatDobForAgent(dob: Date | string | null | undefined): string | null { + if (!dob) return null; + + // If it's a string in exact YYYY-MM-DD format, return as-is (most ideal). + if (typeof dob === "string") { + const simpleDateMatch = /^\d{4}-\d{2}-\d{2}$/.test(dob); + if (simpleDateMatch) return dob; + + // Otherwise try parsing as a Date/ISO string and use UTC parts + const parsed = new Date(dob); + if (isNaN(parsed.getTime())) return null; + const y = parsed.getUTCFullYear(); + const m = String(parsed.getUTCMonth() + 1).padStart(2, "0"); + const d = String(parsed.getUTCDate()).padStart(2, "0"); + return `${y}-${m}-${d}`; + } + + // If it's a Date object, use UTC getters to avoid timezone shifts + if (dob instanceof Date) { + if (isNaN(dob.getTime())) return null; + const y = dob.getUTCFullYear(); + const m = String(dob.getUTCMonth() + 1).padStart(2, "0"); + const d = String(dob.getUTCDate()).padStart(2, "0"); + return `${y}-${m}-${d}`; + } + + return null; +}