From 33bea3822c931dbe3a7920ed25af2238178b04ee Mon Sep 17 00:00:00 2001 From: Gitead Date: Thu, 5 Nov 2026 08:38:13 -0500 Subject: [PATCH] feat: MassHealth service history extraction, shown in Patient Details Co-Authored-By: Claude Opus 5.5 --- ...mspEligibilityHistoryRemainingProcessor.ts | 11 + apps/Backend/src/routes/patients.ts | 13 + apps/Backend/src/storage/patients-storage.ts | 37 + .../src/components/patients/patient-table.tsx | 55 +- apps/SeleniumService/mh_service_history.py | 75 + ..._eligibilityHistoryRemainingCheckWorker.py | 5 + packages/db/generated/prisma/edge.js | 20 +- packages/db/generated/prisma/index-browser.js | 12 + packages/db/generated/prisma/index.d.ts | 1844 ++++++++++++++++- packages/db/generated/prisma/index.js | 20 +- packages/db/generated/prisma/package.json | 2 +- packages/db/generated/prisma/schema.prisma | 36 +- .../migration.sql | 19 + packages/db/prisma/schema.prisma | 18 + .../.prisma-zod-generator-manifest.json | 89 +- .../aggregatePatientServiceHistory.schema.ts | 14 + .../countPatientServiceHistory.schema.ts | 10 + ...nyAndReturnPatientServiceHistory.schema.ts | 8 + .../createManyPatientServiceHistory.schema.ts | 7 + .../createOnePatientServiceHistory.schema.ts | 10 + .../deleteManyPatientServiceHistory.schema.ts | 7 + .../deleteOnePatientServiceHistory.schema.ts | 9 + ...entServiceHistoryScalarFieldEnum.schema.ts | 5 + .../schemas/findFirstOrThrowPatient.schema.ts | 2 + ...irstOrThrowPatientServiceHistory.schema.ts | 38 + .../shared/schemas/findFirstPatient.schema.ts | 2 + .../findFirstPatientServiceHistory.schema.ts | 38 + .../shared/schemas/findManyPatient.schema.ts | 2 + .../findManyPatientServiceHistory.schema.ts | 38 + ...iqueOrThrowPatientServiceHistory.schema.ts | 9 + .../findUniquePatientServiceHistory.schema.ts | 9 + .../groupByPatientServiceHistory.schema.ts | 15 + packages/db/shared/schemas/index.ts | 34 + ...utputTypeCountServiceHistoryArgs.schema.ts | 9 + .../PatientCountOutputTypeSelect.schema.ts | 6 +- .../objects/PatientCreateInput.schema.ts | 6 +- ...tedOneWithoutServiceHistoryInput.schema.ts | 14 + ...onnectWithoutServiceHistoryInput.schema.ts | 12 + ...ntCreateWithoutAppointmentsInput.schema.ts | 6 +- .../PatientCreateWithoutClaimsInput.schema.ts | 6 +- ...ntCreateWithoutCloudFoldersInput.schema.ts | 6 +- ...CreateWithoutCommunicationsInput.schema.ts | 6 +- ...ntCreateWithoutConversationInput.schema.ts | 6 +- ...tientCreateWithoutDocumentsInput.schema.ts | 6 +- .../PatientCreateWithoutGroupsInput.schema.ts | 6 +- ...PatientCreateWithoutPaymentInput.schema.ts | 6 +- ...ientCreateWithoutProceduresInput.schema.ts | 6 +- ...CreateWithoutServiceHistoryInput.schema.ts | 52 + .../PatientCreateWithoutUserInput.schema.ts | 6 +- .../schemas/objects/PatientInclude.schema.ts | 2 + .../PatientOrderByWithRelationInput.schema.ts | 6 +- .../schemas/objects/PatientSelect.schema.ts | 2 + .../PatientServiceHistoryArgs.schema.ts | 11 + ...tServiceHistoryAvgAggregateInput.schema.ts | 10 + ...eHistoryAvgOrderByAggregateInput.schema.ts | 10 + ...erviceHistoryCountAggregateInput.schema.ts | 17 + ...istoryCountOrderByAggregateInput.schema.ts | 16 + ...PatientServiceHistoryCreateInput.schema.ts | 15 + ...entServiceHistoryCreateManyInput.schema.ts | 16 + ...iceHistoryCreateManyPatientInput.schema.ts | 15 + ...ryCreateManyPatientInputEnvelope.schema.ts | 10 + ...ateNestedManyWithoutPatientInput.schema.ts | 16 + ...eateOrConnectWithoutPatientInput.schema.ts | 12 + ...HistoryCreateWithoutPatientInput.schema.ts | 14 + .../PatientServiceHistoryInclude.schema.ts | 9 + ...ServiceHistoryListRelationFilter.schema.ts | 11 + ...tServiceHistoryMaxAggregateInput.schema.ts | 16 + ...eHistoryMaxOrderByAggregateInput.schema.ts | 16 + ...tServiceHistoryMinAggregateInput.schema.ts | 16 + ...eHistoryMinOrderByAggregateInput.schema.ts | 16 + ...oryOrderByRelationAggregateInput.schema.ts | 9 + ...storyOrderByWithAggregationInput.schema.ts | 27 + ...eHistoryOrderByWithRelationInput.schema.ts | 19 + ...ntServiceHistoryScalarWhereInput.schema.ts | 22 + ...ryScalarWhereWithAggregatesInput.schema.ts | 22 + .../PatientServiceHistorySelect.schema.ts | 17 + ...tServiceHistorySumAggregateInput.schema.ts | 10 + ...eHistorySumOrderByAggregateInput.schema.ts | 10 + ...rviceHistoryUncheckedCreateInput.schema.ts | 16 + ...ateNestedManyWithoutPatientInput.schema.ts | 16 + ...checkedCreateWithoutPatientInput.schema.ts | 15 + ...rviceHistoryUncheckedUpdateInput.schema.ts | 19 + ...eHistoryUncheckedUpdateManyInput.schema.ts | 19 + ...kedUpdateManyWithoutPatientInput.schema.ts | 18 + ...ateManyWithoutPatientNestedInput.schema.ts | 27 + ...checkedUpdateWithoutPatientInput.schema.ts | 18 + ...PatientServiceHistoryUpdateInput.schema.ts | 18 + ...ceHistoryUpdateManyMutationInput.schema.ts | 16 + ...ManyWithWhereWithoutPatientInput.schema.ts | 12 + ...ateManyWithoutPatientNestedInput.schema.ts | 27 + ...thWhereUniqueWithoutPatientInput.schema.ts | 12 + ...HistoryUpdateWithoutPatientInput.schema.ts | 16 + ...thWhereUniqueWithoutPatientInput.schema.ts | 15 + .../PatientServiceHistoryWhereInput.schema.ts | 25 + ...ntServiceHistoryWhereUniqueInput.schema.ts | 9 + .../PatientUncheckedCreateInput.schema.ts | 6 +- ...edCreateWithoutAppointmentsInput.schema.ts | 6 +- ...ncheckedCreateWithoutClaimsInput.schema.ts | 6 +- ...edCreateWithoutCloudFoldersInput.schema.ts | 6 +- ...CreateWithoutCommunicationsInput.schema.ts | 6 +- ...edCreateWithoutConversationInput.schema.ts | 6 +- ...eckedCreateWithoutDocumentsInput.schema.ts | 6 +- ...ncheckedCreateWithoutGroupsInput.schema.ts | 6 +- ...checkedCreateWithoutPaymentInput.schema.ts | 6 +- ...ckedCreateWithoutProceduresInput.schema.ts | 6 +- ...CreateWithoutServiceHistoryInput.schema.ts | 52 + ...tUncheckedCreateWithoutUserInput.schema.ts | 6 +- .../PatientUncheckedUpdateInput.schema.ts | 6 +- ...edUpdateWithoutAppointmentsInput.schema.ts | 6 +- ...ncheckedUpdateWithoutClaimsInput.schema.ts | 6 +- ...edUpdateWithoutCloudFoldersInput.schema.ts | 6 +- ...UpdateWithoutCommunicationsInput.schema.ts | 6 +- ...edUpdateWithoutConversationInput.schema.ts | 6 +- ...eckedUpdateWithoutDocumentsInput.schema.ts | 6 +- ...ncheckedUpdateWithoutGroupsInput.schema.ts | 6 +- ...checkedUpdateWithoutPaymentInput.schema.ts | 6 +- ...ckedUpdateWithoutProceduresInput.schema.ts | 6 +- ...UpdateWithoutServiceHistoryInput.schema.ts | 59 + ...tUncheckedUpdateWithoutUserInput.schema.ts | 6 +- .../objects/PatientUpdateInput.schema.ts | 6 +- ...WithoutServiceHistoryNestedInput.schema.ts | 20 + ...hWhereWithoutServiceHistoryInput.schema.ts | 12 + ...ntUpdateWithoutAppointmentsInput.schema.ts | 6 +- .../PatientUpdateWithoutClaimsInput.schema.ts | 6 +- ...ntUpdateWithoutCloudFoldersInput.schema.ts | 6 +- ...UpdateWithoutCommunicationsInput.schema.ts | 6 +- ...ntUpdateWithoutConversationInput.schema.ts | 6 +- ...tientUpdateWithoutDocumentsInput.schema.ts | 6 +- .../PatientUpdateWithoutGroupsInput.schema.ts | 6 +- ...PatientUpdateWithoutPaymentInput.schema.ts | 6 +- ...ientUpdateWithoutProceduresInput.schema.ts | 6 +- ...UpdateWithoutServiceHistoryInput.schema.ts | 58 + .../PatientUpdateWithoutUserInput.schema.ts | 6 +- ...UpsertWithoutServiceHistoryInput.schema.ts | 15 + .../objects/PatientWhereInput.schema.ts | 6 +- packages/db/shared/schemas/objects/index.ts | 53 + .../results/PatientAggregateResult.schema.ts | 3 +- .../results/PatientCreateResult.schema.ts | 3 +- .../results/PatientDeleteResult.schema.ts | 3 +- .../results/PatientFindFirstResult.schema.ts | 3 +- .../results/PatientFindManyResult.schema.ts | 3 +- .../results/PatientFindUniqueResult.schema.ts | 3 +- .../results/PatientGroupByResult.schema.ts | 3 +- ...entServiceHistoryAggregateResult.schema.ts | 40 + ...PatientServiceHistoryCountResult.schema.ts | 2 + ...ntServiceHistoryCreateManyResult.schema.ts | 4 + ...atientServiceHistoryCreateResult.schema.ts | 12 + ...ntServiceHistoryDeleteManyResult.schema.ts | 4 + ...atientServiceHistoryDeleteResult.schema.ts | 12 + ...entServiceHistoryFindFirstResult.schema.ts | 12 + ...ientServiceHistoryFindManyResult.schema.ts | 22 + ...ntServiceHistoryFindUniqueResult.schema.ts | 12 + ...tientServiceHistoryGroupByResult.schema.ts | 50 + ...ntServiceHistoryUpdateManyResult.schema.ts | 4 + ...atientServiceHistoryUpdateResult.schema.ts | 12 + ...atientServiceHistoryUpsertResult.schema.ts | 12 + .../results/PatientUpdateResult.schema.ts | 3 +- .../results/PatientUpsertResult.schema.ts | 3 +- packages/db/shared/schemas/results/index.ts | 13 + ...nyAndReturnPatientServiceHistory.schema.ts | 9 + .../updateManyPatientServiceHistory.schema.ts | 8 + .../updateOnePatientServiceHistory.schema.ts | 11 + .../upsertOnePatientServiceHistory.schema.ts | 13 + .../schemas/variants/input/Patient.input.ts | 3 +- .../input/PatientServiceHistory.input.ts | 15 + .../db/shared/schemas/variants/input/index.ts | 1 + .../schemas/variants/pure/Patient.pure.ts | 3 +- .../pure/PatientServiceHistory.pure.ts | 15 + .../db/shared/schemas/variants/pure/index.ts | 1 + .../schemas/variants/result/Patient.result.ts | 3 +- .../result/PatientServiceHistory.result.ts | 15 + .../shared/schemas/variants/result/index.ts | 1 + 172 files changed, 4044 insertions(+), 127 deletions(-) create mode 100644 apps/SeleniumService/mh_service_history.py create mode 100644 packages/db/prisma/migrations/20261104000000_add_patient_service_history/migration.sql create mode 100644 packages/db/shared/schemas/aggregatePatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/countPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/createManyAndReturnPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/createManyPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/createOnePatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/deleteManyPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/deleteOnePatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/enums/PatientServiceHistoryScalarFieldEnum.schema.ts create mode 100644 packages/db/shared/schemas/findFirstOrThrowPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/findFirstPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/findManyPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/findUniqueOrThrowPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/findUniquePatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/groupByPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientCountOutputTypeCountServiceHistoryArgs.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientCreateNestedOneWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientCreateOrConnectWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientCreateWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryArgs.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryAvgAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryAvgOrderByAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCountAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCountOrderByAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCreateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCreateManyInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCreateManyPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCreateManyPatientInputEnvelope.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCreateNestedManyWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCreateOrConnectWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryCreateWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryInclude.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryListRelationFilter.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryMaxAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryMaxOrderByAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryMinAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryMinOrderByAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryOrderByRelationAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryOrderByWithAggregationInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryOrderByWithRelationInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryScalarWhereInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryScalarWhereWithAggregatesInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistorySelect.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistorySumAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistorySumOrderByAggregateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedCreateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedCreateNestedManyWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedCreateWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedUpdateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedUpdateManyInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedUpdateManyWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedUpdateManyWithoutPatientNestedInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUncheckedUpdateWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUpdateInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUpdateManyMutationInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUpdateManyWithWhereWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUpdateManyWithoutPatientNestedInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUpdateWithWhereUniqueWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUpdateWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryUpsertWithWhereUniqueWithoutPatientInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryWhereInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientServiceHistoryWhereUniqueInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientUncheckedCreateWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientUncheckedUpdateWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientUpdateOneRequiredWithoutServiceHistoryNestedInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientUpdateToOneWithWhereWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientUpdateWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/objects/PatientUpsertWithoutServiceHistoryInput.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryAggregateResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryCountResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryCreateManyResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryCreateResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryDeleteManyResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryDeleteResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryFindFirstResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryFindManyResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryFindUniqueResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryGroupByResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryUpdateManyResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryUpdateResult.schema.ts create mode 100644 packages/db/shared/schemas/results/PatientServiceHistoryUpsertResult.schema.ts create mode 100644 packages/db/shared/schemas/updateManyAndReturnPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/updateManyPatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/updateOnePatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/upsertOnePatientServiceHistory.schema.ts create mode 100644 packages/db/shared/schemas/variants/input/PatientServiceHistory.input.ts create mode 100644 packages/db/shared/schemas/variants/pure/PatientServiceHistory.pure.ts create mode 100644 packages/db/shared/schemas/variants/result/PatientServiceHistory.result.ts 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} + + ))} + +
+ )} +
+