Files
temetro/frontend/lib/prescriptions.ts
T
Khalid Abdi 929bec8f31 feat: AI-added records save with placeholders + "Added by AI" provenance
Stop blocking AI imports/proposals on missing non-critical fields. Records
the chat agent drafts now save with safe placeholders, auto-generated file
numbers, and a source="ai" marker that surfaces an "Added by AI" badge so a
clinician can review/edit them later.

Backend:
- add `source` (manual|ai) column to patients/appointments/prescriptions
  (migration 0014) + canonical types, services, validation schemas
- relax patient/appointment validation: empty file number allowed, demographic
  + type/provider/initials fall back to placeholders (initials derived from name)
- patients.generateFileNumber() auto-assigns an MRN when one is missing
- proposeAppointment accepts a name when no file number resolves; AI commits +
  /api/ai/import stamp source="ai"

Frontend:
- `source` on Appointment/Patient/Prescription types; AI commits send source="ai"
- reusable <AiBadge> shown on the Patients table/detail and prescriptions list

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 19:27:17 +03:00

77 lines
2.0 KiB
TypeScript

import { apiFetch } from "@/lib/api-client";
// A prescription. Mirrors the backend `src/types/prescription.ts`. Scoped to the
// active clinic. `prescribedAt` is an ISO YYYY-MM-DD date.
export type RxStatus = "active" | "completed" | "expired";
export type Prescription = {
id: string;
fileNumber: string;
name: string;
initials: string;
medication: string;
dose: string;
frequency: string;
prescriber: string;
prescribedAt: string; // YYYY-MM-DD
status: RxStatus;
duration: string | null;
notes: string | null;
source: "manual" | "ai"; // "ai" = drafted by the chat agent, flag for review
createdAt: string;
updatedAt: string;
};
// The fields the "New prescription" dialog collects; the backend fills the
// prescriber (from the signed-in user), prescribedAt (today) and status.
export type PrescriptionInput = {
fileNumber: string;
name: string;
initials: string;
medication: string;
dose: string;
frequency: string;
duration?: string | null;
notes?: string | null;
prescriber?: string;
prescribedAt?: string;
status?: RxStatus;
source?: "manual" | "ai";
};
// "2026-06-05" -> "Jun 5, 2026" (matches the previous mock display format).
export function formatPrescribedAt(iso: string): string {
return new Date(`${iso}T00:00:00`).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
});
}
export function listPrescriptions(): Promise<Prescription[]> {
return apiFetch<Prescription[]>("/api/prescriptions");
}
export function createPrescription(
input: PrescriptionInput,
): Promise<Prescription> {
return apiFetch<Prescription>("/api/prescriptions", {
method: "POST",
body: JSON.stringify(input),
});
}
export function updatePrescription(
id: string,
input: PrescriptionInput,
): Promise<Prescription> {
return apiFetch<Prescription>(`/api/prescriptions/${id}`, {
method: "PUT",
body: JSON.stringify(input),
});
}
export function deletePrescription(id: string): Promise<void> {
return apiFetch<void>(`/api/prescriptions/${id}`, { method: "DELETE" });
}