feat: complete admission workspace
This commit is contained in:
109
app/api/admissions/[id]/route.ts
Normal file
109
app/api/admissions/[id]/route.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
|
||||
import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/api";
|
||||
import { recordAuditLog } from "@/modules/core/server/audit";
|
||||
import { requirePermission } from "@/modules/core/server/auth";
|
||||
import { getDatabase } from "@/modules/core/server/db";
|
||||
import { admissions, beds, elders } from "@/modules/core/server/schema";
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{
|
||||
id: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
type AdmissionRow = typeof admissions.$inferSelect;
|
||||
|
||||
type DischargeResult =
|
||||
| {
|
||||
success: true;
|
||||
admission: AdmissionRow;
|
||||
}
|
||||
| {
|
||||
success: false;
|
||||
reason: string;
|
||||
status: number;
|
||||
};
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function readString(source: Record<string, unknown>, key: string): string {
|
||||
const value = source[key];
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
export async function PATCH(request: Request, context: RouteContext): Promise<Response> {
|
||||
const { id } = await context.params;
|
||||
const auth = await requirePermission("admission:manage", {
|
||||
action: "admission.discharge",
|
||||
targetType: "admission",
|
||||
targetId: id,
|
||||
});
|
||||
|
||||
if (!auth.success) {
|
||||
return auth.response;
|
||||
}
|
||||
|
||||
const organizationId = auth.context.organization?.id;
|
||||
if (!organizationId) {
|
||||
return jsonFailure("请选择机构后办理退住", 400);
|
||||
}
|
||||
|
||||
const body = await readJsonBody(request);
|
||||
const notes = isRecord(body) ? readString(body, "notes") : "";
|
||||
|
||||
const database = getDatabase();
|
||||
const result = await database.transaction(async (transaction): Promise<DischargeResult> => {
|
||||
const admissionRows = await transaction
|
||||
.select()
|
||||
.from(admissions)
|
||||
.where(and(eq(admissions.id, id), eq(admissions.organizationId, organizationId)))
|
||||
.limit(1);
|
||||
const admission = admissionRows[0];
|
||||
if (!admission) {
|
||||
return { success: false, reason: "入住记录不存在", status: 404 };
|
||||
}
|
||||
|
||||
if (admission.status !== "active") {
|
||||
return { success: false, reason: "该入住记录已结束", status: 409 };
|
||||
}
|
||||
|
||||
const updatedRows = await transaction
|
||||
.update(admissions)
|
||||
.set({
|
||||
status: "discharged",
|
||||
dischargedAt: new Date(),
|
||||
notes: notes || admission.notes,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(admissions.id, admission.id))
|
||||
.returning();
|
||||
const updated = updatedRows[0];
|
||||
if (!updated) {
|
||||
return { success: false, reason: "退住记录更新失败", status: 500 };
|
||||
}
|
||||
|
||||
await transaction.update(beds).set({ status: "available", updatedAt: new Date() }).where(eq(beds.id, admission.bedId));
|
||||
await transaction.update(elders).set({ status: "discharged", updatedAt: new Date() }).where(eq(elders.id, admission.elderId));
|
||||
|
||||
return { success: true, admission: updated };
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
return jsonFailure(result.reason, result.status);
|
||||
}
|
||||
|
||||
await recordAuditLog({
|
||||
actor: auth.context.account,
|
||||
organizationId,
|
||||
action: "admission.discharge",
|
||||
targetType: "admission",
|
||||
targetId: result.admission.id,
|
||||
result: "success",
|
||||
reason: "办理退住",
|
||||
});
|
||||
|
||||
return jsonSuccess("退住已办理", { admission: result.admission });
|
||||
}
|
||||
@@ -7,6 +7,19 @@ import { getDatabase } from "@/modules/core/server/db";
|
||||
import { admissions, beds, elders } from "@/modules/core/server/schema";
|
||||
import { readData } from "@/modules/core/server/store";
|
||||
|
||||
type AdmissionRow = typeof admissions.$inferSelect;
|
||||
|
||||
type AdmissionMutationResult =
|
||||
| {
|
||||
success: true;
|
||||
admission: AdmissionRow;
|
||||
}
|
||||
| {
|
||||
success: false;
|
||||
reason: string;
|
||||
status: number;
|
||||
};
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
@@ -62,7 +75,7 @@ export async function POST(request: Request): Promise<Response> {
|
||||
}
|
||||
|
||||
const database = getDatabase();
|
||||
const admission = await database.transaction(async (transaction) => {
|
||||
const mutation = await database.transaction(async (transaction): Promise<AdmissionMutationResult> => {
|
||||
const elderRows = await transaction
|
||||
.select()
|
||||
.from(elders)
|
||||
@@ -70,7 +83,7 @@ export async function POST(request: Request): Promise<Response> {
|
||||
.limit(1);
|
||||
const elder = elderRows[0];
|
||||
if (!elder) {
|
||||
throw new Error("老人档案不存在");
|
||||
return { success: false, reason: "老人档案不存在", status: 404 };
|
||||
}
|
||||
|
||||
const bedRows = await transaction
|
||||
@@ -80,16 +93,22 @@ export async function POST(request: Request): Promise<Response> {
|
||||
.limit(1);
|
||||
const bed = bedRows[0];
|
||||
if (!bed) {
|
||||
throw new Error("床位不存在");
|
||||
return { success: false, reason: "床位不存在", status: 404 };
|
||||
}
|
||||
if (bed.status !== "available") {
|
||||
throw new Error("床位不可分配");
|
||||
return { success: false, reason: "床位不可分配", status: 409 };
|
||||
}
|
||||
|
||||
const activeAdmissions = await transaction
|
||||
.select()
|
||||
.from(admissions)
|
||||
.where(and(eq(admissions.elderId, elderId), eq(admissions.status, "active")));
|
||||
.where(
|
||||
and(
|
||||
eq(admissions.elderId, elderId),
|
||||
eq(admissions.organizationId, organizationId),
|
||||
eq(admissions.status, "active"),
|
||||
),
|
||||
);
|
||||
await Promise.all(
|
||||
activeAdmissions.map((activeAdmission) =>
|
||||
transaction
|
||||
@@ -116,23 +135,27 @@ export async function POST(request: Request): Promise<Response> {
|
||||
.returning();
|
||||
const row = rows[0];
|
||||
if (!row) {
|
||||
throw new Error("入住记录创建失败");
|
||||
return { success: false, reason: "入住记录创建失败", status: 500 };
|
||||
}
|
||||
|
||||
await transaction.update(beds).set({ status: "occupied", updatedAt: new Date() }).where(eq(beds.id, bedId));
|
||||
await transaction.update(elders).set({ status: "active", updatedAt: new Date() }).where(eq(elders.id, elderId));
|
||||
return row;
|
||||
return { success: true, admission: row };
|
||||
});
|
||||
|
||||
if (!mutation.success) {
|
||||
return jsonFailure(mutation.reason, mutation.status);
|
||||
}
|
||||
|
||||
await recordAuditLog({
|
||||
actor: auth.context.account,
|
||||
organizationId,
|
||||
action: "admission.create",
|
||||
targetType: "admission",
|
||||
targetId: admission.id,
|
||||
targetId: mutation.admission.id,
|
||||
result: "success",
|
||||
reason: "办理入住/换床",
|
||||
});
|
||||
|
||||
return jsonSuccess("入住记录已创建", { admission }, 201);
|
||||
return jsonSuccess("入住记录已创建", { admission: mutation.admission }, 201);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user