feat: MassHealth service history extraction, shown in Patient Details
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -422,4 +422,17 @@ router.get("/:id/communications", async (req: Request, res: Response): Promise<a
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/patients/:id/service-history
|
||||
router.get("/:id/service-history", async (req: Request, res: Response): Promise<any> => {
|
||||
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;
|
||||
|
||||
@@ -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<Patient | undefined>;
|
||||
@@ -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<void>;
|
||||
}
|
||||
|
||||
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<void> {
|
||||
try {
|
||||
await db.patient.delete({ where: { id } });
|
||||
|
||||
Reference in New Issue
Block a user