From 54f83ab398d7e55c22ea3ff8f533abeeb8d8d5be Mon Sep 17 00:00:00 2001 From: Potenz Date: Thu, 30 Oct 2025 21:13:33 +0530 Subject: [PATCH] feat(patient-status schema updated)- applied changes --- apps/Backend/src/routes/insuranceStatus.ts | 5 +- .../src/components/patients/patient-form.tsx | 59 +++++++++++------- .../src/components/patients/patient-table.tsx | 61 ++++++++----------- .../src/pages/insurance-status-page.tsx | 1 - apps/Frontend/src/pages/patients-page.tsx | 1 - migratedb.txt | 56 +++++++++++++++++ packages/db/prisma/schema.prisma | 8 ++- packages/db/types/patient-types.ts | 17 +++++- packages/db/usedSchemas/index.ts | 1 + 9 files changed, 146 insertions(+), 63 deletions(-) create mode 100644 migratedb.txt diff --git a/apps/Backend/src/routes/insuranceStatus.ts b/apps/Backend/src/routes/insuranceStatus.ts index 6b9b932..546aad8 100644 --- a/apps/Backend/src/routes/insuranceStatus.ts +++ b/apps/Backend/src/routes/insuranceStatus.ts @@ -77,7 +77,6 @@ async function createOrUpdatePatientByInsuranceId(options: { gender: "", phone: "", userId, - status: "inactive", insuranceId, }; @@ -219,7 +218,7 @@ router.post( if (patient && patient.id !== undefined) { const newStatus = - seleniumResult.eligibility === "Y" ? "active" : "inactive"; + seleniumResult.eligibility === "Y" ? "ACTIVE" : "INACTIVE"; await storage.updatePatient(patient.id, { status: newStatus }); outputResult.patientUpdateStatus = `Patient status updated to ${newStatus}`; @@ -647,7 +646,7 @@ router.post( // Update patient status based on seleniumResult.eligibility const newStatus = - seleniumResult?.eligibility === "Y" ? "active" : "inactive"; + seleniumResult?.eligibility === "Y" ? "ACTIVE" : "INACTIVE"; await storage.updatePatient(updatedPatient.id, { status: newStatus }); resultItem.patientUpdateStatus = `Patient status updated to ${newStatus}`; diff --git a/apps/Frontend/src/components/patients/patient-form.tsx b/apps/Frontend/src/components/patients/patient-form.tsx index 84bfc59..64e28ed 100644 --- a/apps/Frontend/src/components/patients/patient-form.tsx +++ b/apps/Frontend/src/components/patients/patient-form.tsx @@ -24,11 +24,14 @@ import { InsertPatient, insertPatientSchema, Patient, + PatientStatus, + patientStatusOptions, UpdatePatient, updatePatientSchema, } from "@repo/db/types"; import { z } from "zod"; import { DateInputField } from "@/components/ui/dateInputField"; +import { PatientStatusSchema } from "@repo/db/usedSchemas"; interface PatientFormProps { patient?: Patient; @@ -84,7 +87,7 @@ export const PatientForm = forwardRef( policyHolder: "", allergies: "", medicalConditions: "", - status: "active", + status: "UNKNOWN", userId: user?.id, }; }, [isEditing, patient, extractedInfo, user?.id]); @@ -297,27 +300,39 @@ export const PatientForm = forwardRef( ( - - Status * - - - - )} + render={({ field }) => { + const options = Object.values( + patientStatusOptions + ) as PatientStatus[]; // ['ACTIVE','INACTIVE','UNKNOWN'] + const toLabel = (v: PatientStatus) => + v[0] + v.slice(1).toLowerCase(); // ACTIVE -> Active + + return ( + + Status * + + + + ); + }} /> 2) range.push("..."); - - for (let i = left; i <= right; i++) { - range.push(i); - } - - if (right < total - 1) range.push("..."); - if (total > 1) range.push(total); - - return range; - } - return (
@@ -440,18 +422,26 @@ export function PatientTable({
- - {patient.status === "active" ? "Active" : "Inactive"} - + {patient.status === "ACTIVE" && ( + + Active + + )} + + {patient.status === "INACTIVE" && ( + + Inactive + + )} + + {patient.status === "UNKNOWN" && ( + + Unknown + + )}
+
{allowDelete && ( @@ -573,15 +563,18 @@ export function PatientTable({

Status:{" "} {currentPatient.status ? currentPatient.status.charAt(0).toUpperCase() + - currentPatient.status.slice(1) + currentPatient.status.slice(1).toLowerCase() : "Unknown"}

diff --git a/apps/Frontend/src/pages/insurance-status-page.tsx b/apps/Frontend/src/pages/insurance-status-page.tsx index d13cddd..997c3b4 100644 --- a/apps/Frontend/src/pages/insurance-status-page.tsx +++ b/apps/Frontend/src/pages/insurance-status-page.tsx @@ -251,7 +251,6 @@ export default function InsuranceStatusPage() { gender: "", phone: "", userId: user?.id ?? 1, - status: "active", insuranceId: memberId, }; await addPatientMutation.mutateAsync(newPatient); diff --git a/apps/Frontend/src/pages/patients-page.tsx b/apps/Frontend/src/pages/patients-page.tsx index 33cc589..1e5b077 100644 --- a/apps/Frontend/src/pages/patients-page.tsx +++ b/apps/Frontend/src/pages/patients-page.tsx @@ -254,7 +254,6 @@ export default function PatientsPage() { gender: "", phone: "", userId: user?.id ?? 1, - status: "active", insuranceId: data.memberId || "", }; diff --git a/migratedb.txt b/migratedb.txt new file mode 100644 index 0000000..41df6cb --- /dev/null +++ b/migratedb.txt @@ -0,0 +1,56 @@ +1. +``` +npx prisma migrate dev --create-only --name add_patient_status_enum +``` + +2. +``` + +-- Create the enum type (quoted name keeps exact case) +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'PatientStatus') THEN + CREATE TYPE "PatientStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'UNKNOWN'); + END IF; +END$$; + +-- 1) Add new enum column (nullable for backfill) +ALTER TABLE "Patient" + ADD COLUMN IF NOT EXISTS "status_new" "PatientStatus"; + +-- 2) Backfill from old text column to enum (case-insensitive) +UPDATE "Patient" +SET "status_new" = CASE + WHEN "status" IS NULL THEN 'UNKNOWN'::"PatientStatus" + WHEN lower("status") = 'active' THEN 'ACTIVE'::"PatientStatus" + WHEN lower("status") = 'inactive' THEN 'INACTIVE'::"PatientStatus" + ELSE 'UNKNOWN'::"PatientStatus" +END +WHERE "status_new" IS NULL; + +-- 3) Safety: ensure no NULLs remain +DO $$ +DECLARE cnt INTEGER; +BEGIN + SELECT count(*) INTO cnt FROM "Patient" WHERE "status_new" IS NULL; + IF cnt > 0 THEN + RAISE EXCEPTION 'Migration abort: % rows have NULL status_new', cnt; + END IF; +END$$; + +-- 4) Make new column NOT NULL and set DB default to UNKNOWN +ALTER TABLE "Patient" + ALTER COLUMN "status_new" SET DEFAULT 'UNKNOWN', + ALTER COLUMN "status_new" SET NOT NULL; + +-- 5) Drop old column and rename new -> status +ALTER TABLE "Patient" DROP COLUMN IF EXISTS "status"; +ALTER TABLE "Patient" RENAME COLUMN "status_new" TO "status"; + +``` + + +3. +``` +npx prisma migrate dev +``` \ No newline at end of file diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index 7424fe6..7b94162 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -52,7 +52,7 @@ model Patient { policyHolder String? allergies String? medicalConditions String? - status String @default("active") + status PatientStatus @default(UNKNOWN) userId Int createdAt DateTime @default(now()) user User @relation(fields: [userId], references: [id]) @@ -65,6 +65,12 @@ model Patient { @@index([createdAt]) } +enum PatientStatus { + ACTIVE + INACTIVE + UNKNOWN +} + model Appointment { id Int @id @default(autoincrement()) patientId Int diff --git a/packages/db/types/patient-types.ts b/packages/db/types/patient-types.ts index 9111312..bac761c 100644 --- a/packages/db/types/patient-types.ts +++ b/packages/db/types/patient-types.ts @@ -1,5 +1,9 @@ -import { PatientUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas"; +import { + PatientStatusSchema, + PatientUncheckedCreateInputObjectSchema, +} from "@repo/db/usedSchemas"; import { z } from "zod"; +import { makeEnumOptions } from "../utils"; export type Patient = z.infer; @@ -28,6 +32,17 @@ export const insuranceIdSchema = z.preprocess( .nullable() ); +//patient status +export type PatientStatus = z.infer; + +// enum → select options +export const patientStatusOptions = + makeEnumOptions< + typeof PatientStatusSchema extends z.ZodTypeAny + ? z.infer + : string + >(PatientStatusSchema); + export const insertPatientSchema = ( PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject ) diff --git a/packages/db/usedSchemas/index.ts b/packages/db/usedSchemas/index.ts index 39e203d..521e2cf 100644 --- a/packages/db/usedSchemas/index.ts +++ b/packages/db/usedSchemas/index.ts @@ -1,6 +1,7 @@ // using this, as the browser load only the required files , not whole db/shared/schemas/ files. export * from '../shared/schemas/objects/AppointmentUncheckedCreateInput.schema'; export * from '../shared/schemas/objects/PatientUncheckedCreateInput.schema'; +export * from '../shared/schemas/enums/PatientStatus.schema'; export * from '../shared/schemas/objects/UserUncheckedCreateInput.schema'; export * from '../shared/schemas/objects/StaffUncheckedCreateInput.schema' export * from '../shared/schemas/objects/ClaimUncheckedCreateInput.schema'