fix: tighten real operations data paths
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { getCurrentAuthContext } from "@/modules/core/server/auth";
|
||||
import {
|
||||
listOperationalAdmissions,
|
||||
listOperationalBeds,
|
||||
listOperationalElders,
|
||||
listOperationalRooms,
|
||||
} from "@/modules/core/server/operations";
|
||||
import { hasPermission } from "@/modules/core/server/permissions";
|
||||
import { readData } from "@/modules/core/server/store";
|
||||
import { BedsWorkspace } from "@/modules/facilities/components/BedsWorkspace";
|
||||
|
||||
export default async function BedsPage(): Promise<React.ReactElement> {
|
||||
@@ -15,14 +20,13 @@ export default async function BedsPage(): Promise<React.ReactElement> {
|
||||
redirect("/app/dashboard");
|
||||
}
|
||||
|
||||
const data = await readData();
|
||||
const organizationId = context.organization?.id;
|
||||
const rooms = organizationId ? data.rooms.filter((room) => room.organizationId === organizationId) : data.rooms;
|
||||
const beds = organizationId ? data.beds.filter((bed) => bed.organizationId === organizationId) : data.beds;
|
||||
const admissions = organizationId
|
||||
? data.admissions.filter((admission) => admission.organizationId === organizationId)
|
||||
: data.admissions;
|
||||
const elders = organizationId ? data.elders.filter((elder) => elder.organizationId === organizationId) : data.elders;
|
||||
const [rooms, beds, admissions, elders] = await Promise.all([
|
||||
listOperationalRooms(organizationId),
|
||||
listOperationalBeds(organizationId),
|
||||
listOperationalAdmissions(organizationId),
|
||||
listOperationalElders(organizationId),
|
||||
]);
|
||||
|
||||
return (
|
||||
<BedsWorkspace
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { getCurrentAuthContext } from "@/modules/core/server/auth";
|
||||
import { readData } from "@/modules/core/server/store";
|
||||
import {
|
||||
listOperationalAdmissions,
|
||||
listOperationalBeds,
|
||||
listOperationalElders,
|
||||
listOperationalIncidents,
|
||||
} from "@/modules/core/server/operations";
|
||||
import type { BedStatus } from "@/modules/core/types";
|
||||
import { DashboardHome, type DashboardMetric } from "@/modules/dashboard/components/DashboardHome";
|
||||
|
||||
@@ -13,16 +18,13 @@ export default async function DashboardPage(): Promise<React.ReactElement> {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
const data = await readData();
|
||||
const organizationId = context.organization?.id;
|
||||
const elders = organizationId ? data.elders.filter((elder) => elder.organizationId === organizationId) : data.elders;
|
||||
const beds = organizationId ? data.beds.filter((bed) => bed.organizationId === organizationId) : data.beds;
|
||||
const admissions = organizationId
|
||||
? data.admissions.filter((admission) => admission.organizationId === organizationId)
|
||||
: data.admissions;
|
||||
const incidents = organizationId
|
||||
? data.incidents.filter((incident) => !incident.organizationId || incident.organizationId === organizationId)
|
||||
: data.incidents;
|
||||
const [elders, beds, admissions, incidents] = await Promise.all([
|
||||
listOperationalElders(organizationId),
|
||||
listOperationalBeds(organizationId),
|
||||
listOperationalAdmissions(organizationId),
|
||||
listOperationalIncidents(organizationId),
|
||||
]);
|
||||
|
||||
const activeElders = elders.filter((elder) => elder.status === "active").length;
|
||||
const pendingElders = elders.filter((elder) => elder.status === "pending").length;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { getCurrentAuthContext } from "@/modules/core/server/auth";
|
||||
import { readData } from "@/modules/core/server/store";
|
||||
import { listOperationalElders } from "@/modules/core/server/operations";
|
||||
import { EldersClient } from "@/modules/elders/components/EldersClient";
|
||||
|
||||
export default async function EldersPage(): Promise<React.ReactElement> {
|
||||
@@ -10,11 +10,7 @@ export default async function EldersPage(): Promise<React.ReactElement> {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
const data = await readData();
|
||||
const organizationId = context.organization?.id;
|
||||
const elders = organizationId
|
||||
? data.elders.filter((elder) => elder.organizationId === organizationId)
|
||||
: data.elders;
|
||||
const elders = await listOperationalElders(context.organization?.id);
|
||||
|
||||
return <EldersClient initialElders={elders} permissions={context.permissions} />;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/ap
|
||||
import { recordAuditLog } from "@/modules/core/server/audit";
|
||||
import { requirePermission } from "@/modules/core/server/auth";
|
||||
import { getDatabase } from "@/modules/core/server/db";
|
||||
import { listOperationalAdmissions } from "@/modules/core/server/operations";
|
||||
import { admissions, beds, elders } from "@/modules/core/server/schema";
|
||||
import { readData } from "@/modules/core/server/store";
|
||||
|
||||
type AdmissionRow = typeof admissions.$inferSelect;
|
||||
|
||||
@@ -39,13 +39,8 @@ export async function GET(): Promise<Response> {
|
||||
return auth.response;
|
||||
}
|
||||
|
||||
const data = await readData();
|
||||
const organizationId = auth.context.organization?.id;
|
||||
return jsonSuccess("入住记录已加载", {
|
||||
admissions: organizationId
|
||||
? data.admissions.filter((admission) => admission.organizationId === organizationId)
|
||||
: data.admissions,
|
||||
});
|
||||
const admissionsData = await listOperationalAdmissions(auth.context.organization?.id);
|
||||
return jsonSuccess("入住记录已加载", { admissions: admissionsData });
|
||||
}
|
||||
|
||||
export async function POST(request: Request): Promise<Response> {
|
||||
|
||||
@@ -4,8 +4,8 @@ import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/ap
|
||||
import { recordAuditLog } from "@/modules/core/server/audit";
|
||||
import { requirePermission } from "@/modules/core/server/auth";
|
||||
import { getDatabase } from "@/modules/core/server/db";
|
||||
import { listOperationalBeds } from "@/modules/core/server/operations";
|
||||
import { beds, rooms } from "@/modules/core/server/schema";
|
||||
import { readData } from "@/modules/core/server/store";
|
||||
import type { BedStatus } from "@/modules/core/types";
|
||||
|
||||
const BED_STATUS_VALUES: BedStatus[] = ["available", "occupied", "maintenance", "disabled"];
|
||||
@@ -19,8 +19,12 @@ function readString(source: Record<string, unknown>, key: string): string {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
function readBedStatus(value: string): BedStatus {
|
||||
return BED_STATUS_VALUES.find((status) => status === value) ?? "available";
|
||||
function readOptionalBedStatus(value: string): BedStatus | null | undefined {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return BED_STATUS_VALUES.find((status) => status === value) ?? null;
|
||||
}
|
||||
|
||||
export async function GET(): Promise<Response> {
|
||||
@@ -33,11 +37,8 @@ export async function GET(): Promise<Response> {
|
||||
return auth.response;
|
||||
}
|
||||
|
||||
const data = await readData();
|
||||
const organizationId = auth.context.organization?.id;
|
||||
return jsonSuccess("床位列表已加载", {
|
||||
beds: organizationId ? data.beds.filter((bed) => bed.organizationId === organizationId) : data.beds,
|
||||
});
|
||||
const bedsData = await listOperationalBeds(auth.context.organization?.id);
|
||||
return jsonSuccess("床位列表已加载", { beds: bedsData });
|
||||
}
|
||||
|
||||
export async function POST(request: Request): Promise<Response> {
|
||||
@@ -65,6 +66,10 @@ export async function POST(request: Request): Promise<Response> {
|
||||
if (!roomId || !code) {
|
||||
return jsonFailure("房间和床位编号不能为空");
|
||||
}
|
||||
const status = readOptionalBedStatus(readString(body, "status"));
|
||||
if (status === null) {
|
||||
return jsonFailure("床位状态无效");
|
||||
}
|
||||
|
||||
const database = getDatabase();
|
||||
const roomRows = await database
|
||||
@@ -83,7 +88,7 @@ export async function POST(request: Request): Promise<Response> {
|
||||
organizationId,
|
||||
roomId,
|
||||
code,
|
||||
status: readBedStatus(readString(body, "status")),
|
||||
status: status ?? "available",
|
||||
notes: readString(body, "notes"),
|
||||
})
|
||||
.returning();
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
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 { campuses, buildings, floors, rooms } from "@/modules/core/server/schema";
|
||||
import { readData } from "@/modules/core/server/store";
|
||||
import { listOperationalRooms } from "@/modules/core/server/operations";
|
||||
import { floors, rooms } from "@/modules/core/server/schema";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
@@ -18,10 +16,14 @@ function readString(source: Record<string, unknown>, key: string): string {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
function readPositiveNumber(source: Record<string, unknown>, key: string, fallback: number): number {
|
||||
function readOptionalPositiveNumber(source: Record<string, unknown>, key: string): number | null | undefined {
|
||||
const value = source[key];
|
||||
if (value === undefined || value === null || value === "") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const numeric = typeof value === "number" ? value : Number(value);
|
||||
return Number.isInteger(numeric) && numeric > 0 ? numeric : fallback;
|
||||
return Number.isInteger(numeric) && numeric > 0 ? numeric : null;
|
||||
}
|
||||
|
||||
export async function GET(): Promise<Response> {
|
||||
@@ -34,11 +36,8 @@ export async function GET(): Promise<Response> {
|
||||
return auth.response;
|
||||
}
|
||||
|
||||
const data = await readData();
|
||||
const organizationId = auth.context.organization?.id;
|
||||
return jsonSuccess("房间列表已加载", {
|
||||
rooms: organizationId ? data.rooms.filter((room) => room.organizationId === organizationId) : data.rooms,
|
||||
});
|
||||
const roomsData = await listOperationalRooms(auth.context.organization?.id);
|
||||
return jsonSuccess("房间列表已加载", { rooms: roomsData });
|
||||
}
|
||||
|
||||
export async function POST(request: Request): Promise<Response> {
|
||||
@@ -63,63 +62,37 @@ export async function POST(request: Request): Promise<Response> {
|
||||
|
||||
const roomName = readString(body, "name");
|
||||
const roomCode = readString(body, "code");
|
||||
if (!roomName || !roomCode) {
|
||||
return jsonFailure("房间名称和编号不能为空");
|
||||
const floorId = readString(body, "floorId");
|
||||
if (!roomName || !roomCode || !floorId) {
|
||||
return jsonFailure("房间名称、编号和楼层不能为空");
|
||||
}
|
||||
const capacityInput = readOptionalPositiveNumber(body, "capacity");
|
||||
if (capacityInput === null) {
|
||||
return jsonFailure("房间容量需为正整数");
|
||||
}
|
||||
const capacity = capacityInput ?? 1;
|
||||
|
||||
const database = getDatabase();
|
||||
const createdRows = await database.transaction(async (transaction) => {
|
||||
const campusRows = await transaction.select().from(campuses).where(eq(campuses.organizationId, organizationId)).limit(1);
|
||||
const campus =
|
||||
campusRows[0] ??
|
||||
(
|
||||
await transaction
|
||||
.insert(campuses)
|
||||
.values({ organizationId, name: "默认院区", address: "" })
|
||||
.returning()
|
||||
)[0];
|
||||
if (!campus) {
|
||||
throw new Error("院区初始化失败");
|
||||
}
|
||||
const floorRows = await database
|
||||
.select({ id: floors.id })
|
||||
.from(floors)
|
||||
.where(and(eq(floors.id, floorId), eq(floors.organizationId, organizationId)))
|
||||
.limit(1);
|
||||
const floor = floorRows[0];
|
||||
if (!floor) {
|
||||
return jsonFailure("楼层不存在", 404);
|
||||
}
|
||||
|
||||
const buildingRows = await transaction.select().from(buildings).where(eq(buildings.campusId, campus.id)).limit(1);
|
||||
const building =
|
||||
buildingRows[0] ??
|
||||
(
|
||||
await transaction
|
||||
.insert(buildings)
|
||||
.values({ organizationId, campusId: campus.id, name: "默认楼栋" })
|
||||
.returning()
|
||||
)[0];
|
||||
if (!building) {
|
||||
throw new Error("楼栋初始化失败");
|
||||
}
|
||||
|
||||
const floorRows = await transaction.select().from(floors).where(eq(floors.buildingId, building.id)).limit(1);
|
||||
const floor =
|
||||
floorRows[0] ??
|
||||
(
|
||||
await transaction
|
||||
.insert(floors)
|
||||
.values({ organizationId, buildingId: building.id, name: "默认楼层", level: 1 })
|
||||
.returning()
|
||||
)[0];
|
||||
if (!floor) {
|
||||
throw new Error("楼层初始化失败");
|
||||
}
|
||||
|
||||
return await transaction
|
||||
.insert(rooms)
|
||||
.values({
|
||||
id: randomUUID(),
|
||||
organizationId,
|
||||
floorId: floor.id,
|
||||
name: roomName,
|
||||
code: roomCode,
|
||||
capacity: readPositiveNumber(body, "capacity", 1),
|
||||
})
|
||||
.returning();
|
||||
});
|
||||
const createdRows = await database
|
||||
.insert(rooms)
|
||||
.values({
|
||||
organizationId,
|
||||
floorId: floor.id,
|
||||
name: roomName,
|
||||
code: roomCode,
|
||||
capacity,
|
||||
})
|
||||
.returning();
|
||||
|
||||
const room = createdRows[0];
|
||||
if (!room) {
|
||||
|
||||
@@ -64,18 +64,9 @@ export async function PATCH(request: Request, context: RouteContext): Promise<Re
|
||||
throw new Error("申请已处理");
|
||||
}
|
||||
|
||||
let roleId = readString(body, "roleId");
|
||||
const roleId = readString(body, "roleId");
|
||||
if (decision === "approved" && !roleId) {
|
||||
const defaultRoleRows = await transaction
|
||||
.select()
|
||||
.from(roles)
|
||||
.where(and(eq(roles.organizationId, joinRequest.organizationId), eq(roles.key, "caregiver")))
|
||||
.limit(1);
|
||||
const defaultRole = defaultRoleRows[0];
|
||||
if (!defaultRole) {
|
||||
throw new Error("默认角色不存在");
|
||||
}
|
||||
roleId = defaultRole.id;
|
||||
throw new Error("请选择审批角色");
|
||||
}
|
||||
|
||||
if (decision === "approved") {
|
||||
|
||||
Reference in New Issue
Block a user