mirror of
https://github.com/temetro/temetro.git
synced 2026-08-13 12:06:52 +00:00
d37a4e9425
Backend: - DELETE /api/patients/:fileNumber/labs (remove one lab result, lab:write) - DELETE /api/dispenses/:id (void a ledger entry, inventory:write) Frontend (all guarded by ConfirmDialog): - lab "Recent results" rows: delete result - pharmacy "Recently dispensed" rows: delete record - prescriptions/tasks detail sheets + inventory detail dialog gain a delete action (prescription delete stays off the shared Pharmacy sheet) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import { and, desc, eq } from "drizzle-orm";
|
|
|
|
import { db } from "../db/index.js";
|
|
import { dispenses } from "../db/schema/dispenses.js";
|
|
import type { DispenseInput } from "../lib/dispense-validation.js";
|
|
import type { Dispense } from "../types/dispense.js";
|
|
|
|
type DispenseRow = typeof dispenses.$inferSelect;
|
|
|
|
function toDispense(row: DispenseRow): Dispense {
|
|
return {
|
|
id: row.id,
|
|
fileNumber: row.patientFileNumber,
|
|
name: row.patientName,
|
|
initials: row.patientInitials,
|
|
medication: row.medication,
|
|
dose: row.dose,
|
|
quantity: row.quantity,
|
|
unit: row.unit,
|
|
prescriptionId: row.prescriptionId,
|
|
dispensedBy: row.dispensedBy,
|
|
dispensedByName: row.dispensedByName,
|
|
dispensedAt: row.dispensedAt.toISOString(),
|
|
notes: row.notes,
|
|
createdAt: row.createdAt.toISOString(),
|
|
};
|
|
}
|
|
|
|
export async function listDispenses(orgId: string): Promise<Dispense[]> {
|
|
const rows = await db
|
|
.select()
|
|
.from(dispenses)
|
|
.where(eq(dispenses.organizationId, orgId))
|
|
.orderBy(desc(dispenses.dispensedAt));
|
|
return rows.map(toDispense);
|
|
}
|
|
|
|
export async function createDispense(
|
|
orgId: string,
|
|
dispenser: { id: string; name: string },
|
|
input: DispenseInput,
|
|
): Promise<Dispense> {
|
|
const [row] = await db
|
|
.insert(dispenses)
|
|
.values({
|
|
organizationId: orgId,
|
|
patientFileNumber: input.fileNumber,
|
|
patientName: input.name,
|
|
patientInitials: input.initials,
|
|
medication: input.medication,
|
|
dose: input.dose,
|
|
quantity: input.quantity,
|
|
unit: input.unit,
|
|
prescriptionId: input.prescriptionId ?? null,
|
|
dispensedBy: dispenser.id,
|
|
dispensedByName: dispenser.name,
|
|
notes: input.notes ?? null,
|
|
})
|
|
.returning();
|
|
return toDispense(row!);
|
|
}
|
|
|
|
export async function deleteDispense(
|
|
orgId: string,
|
|
id: string,
|
|
): Promise<boolean> {
|
|
const deleted = await db
|
|
.delete(dispenses)
|
|
.where(and(eq(dispenses.organizationId, orgId), eq(dispenses.id, id)))
|
|
.returning({ id: dispenses.id });
|
|
return deleted.length > 0;
|
|
}
|