diff --git a/apps/Frontend/src/components/claims/claim-form.tsx b/apps/Frontend/src/components/claims/claim-form.tsx index a116970..4a79eec 100644 --- a/apps/Frontend/src/components/claims/claim-form.tsx +++ b/apps/Frontend/src/components/claims/claim-form.tsx @@ -35,6 +35,7 @@ import { TooltipTrigger, } from "@/components/ui/tooltip"; import procedureCodes from "../../assets/data/procedureCodes.json"; +import exp from "constants"; const PatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject @@ -178,14 +179,32 @@ export function ClaimForm({ }, [staffMembersRaw, staff]); // Service date state + function parseLocalDate(dateInput: Date | string): Date { + if (dateInput instanceof Date) return dateInput; + + const parts = dateInput.split("-"); + if (parts.length !== 3) { + throw new Error(`Invalid date format: ${dateInput}`); + } + + const year = Number(parts[0]); + const month = Number(parts[1]); + const day = Number(parts[2]); + + if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(day)) { + throw new Error(`Invalid date parts in date string: ${dateInput}`); + } + + return new Date(year, month - 1, day); // month is 0-indexed + } + const [serviceDateValue, setServiceDateValue] = useState(new Date()); const [serviceDate, setServiceDate] = useState( new Date().toLocaleDateString("en-CA") // "YYYY-MM-DD" ); - useEffect(() => { if (extractedData?.serviceDate) { - const parsed = new Date(extractedData.serviceDate); + const parsed = parseLocalDate(extractedData.serviceDate); const isoFormatted = parsed.toLocaleDateString("en-CA"); setServiceDateValue(parsed); setServiceDate(isoFormatted); diff --git a/apps/Frontend/src/pages/claims-page.tsx b/apps/Frontend/src/pages/claims-page.tsx index aa47614..6e8a509 100644 --- a/apps/Frontend/src/pages/claims-page.tsx +++ b/apps/Frontend/src/pages/claims-page.tsx @@ -9,7 +9,7 @@ import { useAuth } from "@/hooks/use-auth"; import { PatientUncheckedCreateInputObjectSchema, AppointmentUncheckedCreateInputObjectSchema, - ClaimUncheckedCreateInputObjectSchema + ClaimUncheckedCreateInputObjectSchema, } from "@repo/db/usedSchemas"; import { AlertCircle, CheckCircle, Clock, FileCheck } from "lucide-react"; import { parse, format } from "date-fns"; @@ -218,13 +218,13 @@ export default function ClaimsPage() { // selenium pdf download handler const handleSeleniumPopup = async (actionType: string) => { try { - if (!claimRes?.id || !selectedPatient) { - throw new Error("Missing claim or patient selection"); - } + if (!claimRes?.id || !selectedPatient) { + throw new Error("Missing claim or patient selection"); + } const res = await apiRequest("POST", "/api/claims/selenium/fetchpdf", { action: actionType, - patientId:selectedPatient, - claimId:claimRes.id + patientId: selectedPatient, + claimId: claimRes.id, }); const result = await res.json(); @@ -388,9 +388,11 @@ export default function ClaimsPage() { }; const prefillClaimForm = (patient: Patient) => { - const lastAppointment = appointments.find( - (a) => a.patientId === patient.id - ); + const patientAppointments = appointments + .filter((a) => a.patientId === patient.id) + .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); + + const lastAppointment = patientAppointments[0]; // most recent const dateToUse = lastAppointment ? parseLocalDate(lastAppointment.date) @@ -605,34 +607,38 @@ export default function ClaimsPage() { ) : patientsWithAppointments.length > 0 ? (
- {patientsWithAppointments.map((item: typeof patientsWithAppointments[number]) => ( -
handleNewClaim(item.patientId)} - > -
-

{item.patientName}

-
- - Insurance:{" "} - {getDisplayProvider(item.insuranceProvider)} - + {patientsWithAppointments.map( + (item: (typeof patientsWithAppointments)[number]) => ( +
handleNewClaim(item.patientId)} + > +
+

{item.patientName}

+
+ + Insurance:{" "} + {getDisplayProvider(item.insuranceProvider)} + - - ID: {item.insuranceId} - - - Last Visit:{" "} - {parseLocalDate(item.lastAppointment).toLocaleDateString("en-CA")} - + + ID: {item.insuranceId} + + + Last Visit:{" "} + {parseLocalDate( + item.lastAppointment + ).toLocaleDateString("en-CA")} + +
+
+
+
-
- -
-
- ))} + ) + )}
) : (