fix: tighten real operations data paths
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user