Files
temetro/backend/src/services/appointments.ts
T
Khalid Abdi b946dc9226 fix: chat input clipping, AI appointments create patients, batched approval
1. Chat input toolbar was clipped in the empty state: the form's overflow-hidden
   made its flex min-height resolve to 0, so the vertically-centered layout
   squeezed it and hid the bottom toolbar (attach/add-patient/model/mic/send).
   Add `shrink-0` to the form; let the empty-state column scroll.
2. AI-imported appointments now create/link a patient: services.ensurePatient
   reuses a same-name patient or creates one (auto file number, source "ai");
   appointments.createAppointment calls it when the booking has no file number,
   so imported people appear on the Patients page.
3. Many proposals collapse into one BatchActionPreviewCard → a review dialog
   with per-row remove and "Add all" (commits sequentially), instead of one
   Add/Discard card per record.

Plus: widen the Live chart right margin so the value pill stays inside the card.
Verified with `next build`.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 21:23:25 +03:00

105 lines
3.0 KiB
TypeScript

import { and, asc, eq } from "drizzle-orm";
import { db } from "../db/index.js";
import { appointments } from "../db/schema/appointments.js";
import type { AppointmentInput } from "../lib/appointment-validation.js";
import type { Appointment } from "../types/appointment.js";
import * as patients from "./patients.js";
type AppointmentRow = typeof appointments.$inferSelect;
// Postgres throws on a malformed uuid; treat non-uuid ids as "not found".
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
function toAppointment(row: AppointmentRow): Appointment {
return {
id: row.id,
fileNumber: row.patientFileNumber,
name: row.patientName,
initials: row.patientInitials,
date: row.date,
time: row.time,
type: row.type,
provider: row.provider,
status: row.status,
source: row.source,
createdAt: row.createdAt.toISOString(),
updatedAt: row.updatedAt.toISOString(),
};
}
function columns(orgId: string, input: AppointmentInput, createdBy?: string) {
return {
organizationId: orgId,
patientFileNumber: input.fileNumber,
patientName: input.name,
patientInitials: input.initials,
date: input.date,
time: input.time,
type: input.type,
provider: input.provider,
status: input.status,
source: input.source,
...(createdBy ? { createdBy } : {}),
};
}
export async function listAppointments(orgId: string): Promise<Appointment[]> {
const rows = await db
.select()
.from(appointments)
.where(eq(appointments.organizationId, orgId))
.orderBy(asc(appointments.date), asc(appointments.time));
return rows.map(toAppointment);
}
export async function createAppointment(
orgId: string,
userId: string,
input: AppointmentInput,
): Promise<Appointment> {
// Link to a patient — creating one when the booking has no file number (e.g.
// an AI-imported appointment), so the person shows up on the Patients page.
const fileNumber = await patients.ensurePatient(orgId, userId, {
fileNumber: input.fileNumber,
name: input.name,
initials: input.initials,
});
const [row] = await db
.insert(appointments)
.values(columns(orgId, { ...input, fileNumber }, userId))
.returning();
return toAppointment(row!);
}
export async function updateAppointment(
orgId: string,
id: string,
input: AppointmentInput,
): Promise<Appointment | null> {
if (!UUID_RE.test(id)) return null;
const [row] = await db
.update(appointments)
.set(columns(orgId, input))
.where(
and(eq(appointments.id, id), eq(appointments.organizationId, orgId)),
)
.returning();
return row ? toAppointment(row) : null;
}
export async function deleteAppointment(
orgId: string,
id: string,
): Promise<boolean> {
if (!UUID_RE.test(id)) return false;
const deleted = await db
.delete(appointments)
.where(
and(eq(appointments.id, id), eq(appointments.organizationId, orgId)),
)
.returning({ id: appointments.id });
return deleted.length > 0;
}