From 4e4c1e180f59802709dba84daea6c59b6be3d25b Mon Sep 17 00:00:00 2001 From: Potenz Date: Sat, 11 Oct 2025 00:00:53 +0530 Subject: [PATCH] fix(date issue while patient upsert) --- apps/Backend/src/routes/insuranceStatus.ts | 28 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/Backend/src/routes/insuranceStatus.ts b/apps/Backend/src/routes/insuranceStatus.ts index 7bd2618..acf05d9 100644 --- a/apps/Backend/src/routes/insuranceStatus.ts +++ b/apps/Backend/src/routes/insuranceStatus.ts @@ -9,7 +9,10 @@ import { forwardToSeleniumInsuranceClaimStatusAgent } from "../services/selenium import fsSync from "fs"; import { emptyFolderContainingFile } from "../utils/emptyTempFolder"; import forwardToPatientDataExtractorService from "../services/patientDataExtractorService"; -import { InsertPatient } from "../../../../packages/db/types/patient-types"; +import { + InsertPatient, + insertPatientSchema, +} from "../../../../packages/db/types/patient-types"; const router = Router(); @@ -65,11 +68,11 @@ async function createOrUpdatePatientByInsuranceId(options: { } return; } else { - // If patient doesn't exist -> create - const createPayload: InsertPatient = { + // inside createOrUpdatePatientByInsuranceId, when creating: + const createPayload: any = { firstName: incomingFirst, lastName: incomingLast, - dateOfBirth: dob, + dateOfBirth: dob, // raw from caller (string | Date | null) gender: "", phone: "", userId, @@ -77,7 +80,22 @@ async function createOrUpdatePatientByInsuranceId(options: { insuranceId, }; - await storage.createPatient(createPayload); + let patientData: InsertPatient; + try { + patientData = insertPatientSchema.parse(createPayload); + } catch (err) { + // handle malformed dob or other validation errors conservatively + console.warn( + "Failed to validate patient payload in insurance flow:", + err + ); + // either rethrow or drop invalid fields — here we drop dob and proceed + const safePayload = { ...createPayload }; + delete (safePayload as any).dateOfBirth; + patientData = insertPatientSchema.parse(safePayload); + } + + await storage.createPatient(patientData); } }