diff --git a/apps/Backend/src/queue/processors/cmspEligibilityHistoryRemainingProcessor.ts b/apps/Backend/src/queue/processors/cmspEligibilityHistoryRemainingProcessor.ts index fbac5308..0f9ca396 100644 --- a/apps/Backend/src/queue/processors/cmspEligibilityHistoryRemainingProcessor.ts +++ b/apps/Backend/src/queue/processors/cmspEligibilityHistoryRemainingProcessor.ts @@ -128,6 +128,17 @@ export async function runCmspEligibilityHistoryRemainingProcessor( if (Object.keys(updates).length > 0) { await storage.updatePatient(patient.id, updates); } + + // Only replace when rows came back — an empty read may just mean the portal + // was slow, and shouldn't wipe history saved by an earlier check. + if (Array.isArray(seleniumResult.service_history) && seleniumResult.service_history.length > 0) { + try { + await storage.replacePatientServiceHistory(patient.id, seleniumResult.service_history); + } catch (e: any) { + console.error("[cmspProcessor] saving service history failed:", e.message); + } + } + outputResult.patientUpdateStatus = newStatus ? `Patient status updated to ${newStatus}` : "Eligibility unknown — status not changed"; diff --git a/apps/Backend/src/routes/patients.ts b/apps/Backend/src/routes/patients.ts index 81728a46..44a86bcd 100755 --- a/apps/Backend/src/routes/patients.ts +++ b/apps/Backend/src/routes/patients.ts @@ -422,4 +422,17 @@ router.get("/:id/communications", async (req: Request, res: Response): Promise => { + try { + const patientId = parseInt(req.params.id); + if (isNaN(patientId)) return res.status(400).json({ message: "Invalid patient ID" }); + + const history = await storage.getPatientServiceHistory(patientId); + return res.status(200).json(history); + } catch (err) { + return res.status(500).json({ error: "Failed to fetch service history", details: String(err) }); + } +}); + export default router; diff --git a/apps/Backend/src/storage/patients-storage.ts b/apps/Backend/src/storage/patients-storage.ts index 9289cae3..e830d40b 100755 --- a/apps/Backend/src/storage/patients-storage.ts +++ b/apps/Backend/src/storage/patients-storage.ts @@ -6,6 +6,14 @@ import { } from "@repo/db/types"; import { prisma as db } from "@repo/db/client"; +export type ServiceHistoryRow = { + procedureCode: string; + description?: string | null; + toothQuadArchSurface?: string | null; + placeOfService?: string | null; + serviceDate?: string | null; +}; + export interface IStorage { // Patient methods getPatient(id: number): Promise; @@ -49,6 +57,11 @@ export interface IStorage { limit?: number, offset?: number ): Promise<{ rows: any[]; totalCount: number }>; + getPatientServiceHistory(patientId: number): Promise<(ServiceHistoryRow & { id: number })[]>; + replacePatientServiceHistory( + patientId: number, + rows: ServiceHistoryRow[] + ): Promise; } export const patientsStorage: IStorage = { @@ -157,6 +170,30 @@ export const patientsStorage: IStorage = { } catch (_) {} }, + // Rows are kept in the portal's order (newest service first) + async getPatientServiceHistory(patientId: number) { + return db.patientServiceHistory.findMany({ + where: { patientId }, + orderBy: { id: "asc" }, + }); + }, + + async replacePatientServiceHistory(patientId: number, rows: ServiceHistoryRow[]) { + await db.$transaction([ + db.patientServiceHistory.deleteMany({ where: { patientId } }), + db.patientServiceHistory.createMany({ + data: rows.map((row) => ({ + patientId, + procedureCode: row.procedureCode, + description: row.description ?? null, + toothQuadArchSurface: row.toothQuadArchSurface ?? null, + placeOfService: row.placeOfService ?? null, + serviceDate: row.serviceDate ?? null, + })), + }), + ]); + }, + async deletePatient(id: number): Promise { try { await db.patient.delete({ where: { id } }); diff --git a/apps/Frontend/src/components/patients/patient-table.tsx b/apps/Frontend/src/components/patients/patient-table.tsx index cb27af1c..a5a75663 100755 --- a/apps/Frontend/src/components/patients/patient-table.tsx +++ b/apps/Frontend/src/components/patients/patient-table.tsx @@ -51,6 +51,15 @@ import { type PatientDocument } from "@/lib/api/documents"; +interface ServiceHistoryRow { + id: number; + procedureCode: string; + description: string | null; + toothQuadArchSurface: string | null; + placeOfService: string | null; + serviceDate: string | null; +} + interface PatientApiResponse { patients: Patient[]; totalCount: number; @@ -707,6 +716,16 @@ export function PatientTable({ } }; + const { data: serviceHistory = [], isLoading: isServiceHistoryLoading } = useQuery({ + queryKey: ["patient-service-history", currentPatient?.id], + queryFn: async () => { + const res = await apiRequest("GET", `/api/patients/${currentPatient?.id}/service-history`); + if (!res.ok) throw new Error("Failed to load service history"); + return res.json(); + }, + enabled: isViewPatientOpen && !!currentPatient?.id, + }); + const searchKeyPart = debouncedSearchCriteria?.searchTerm || "recent"; const { @@ -1197,7 +1216,7 @@ export function PatientTable({ {/* View Patient Modal */} - + Patient Details @@ -1348,6 +1367,40 @@ export function PatientTable({ +
+

Service History

+ {isServiceHistoryLoading ? ( +

Loading...

+ ) : serviceHistory.length === 0 ? ( +

+ No service history on file. It is saved when MH eligibility is checked. +

+ ) : ( + + + + Procedure Code + Procedure Code Description + Tooth/Quad/Arch/Surface + Place of Service + Service Date + + + + {serviceHistory.map((row) => ( + + {row.procedureCode} + {row.description} + {row.toothQuadArchSurface} + {row.placeOfService} + {row.serviceDate} + + ))} + +
+ )} +
+