feat: complete admission workspace
This commit is contained in:
@@ -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