Projects with several buildings could not be structured: floors and rooms hung directly off the project. Floors can now belong to a building (migration 0009: table buildings, nullable floors.building_id). Rooms and distribution boards keep their floor; their building follows from it. Existing floors stay without a building. Buildings use the location-structure command store with project-building.insert/.update/.delete, inverses and persistent Undo/Redo; deleting a building with assigned floors is rejected. Floor commands carry buildingId since command version 2. Version 1 floor commands from existing history predate buildings and are upgraded with buildingId = null before they are executed and persisted, so forward and inverse keep one version. Project snapshot schema 7 stores buildings and floor.buildingId; versions 1-6 are upgraded without buildings. Restore and portable transfer include buildings, and the transfer remaps building ids. Buildings, floors and rooms move from the project overview to their own page /projects/:id/struktur with three columns (Gebäude -> Etagen -> Räume) that filter each other, and a sidebar entry. Floor selects in the board and room dialogs are grouped by building. Unchanged dialogs close without a command. Details in docs/building-structure.md; see issue #6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
119 lines
4 KiB
TypeScript
119 lines
4 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import {
|
|
createProjectFloorDeleteProjectCommand,
|
|
createProjectFloorInsertProjectCommand,
|
|
createProjectFloorSnapshot,
|
|
createProjectFloorUpdateProjectCommand,
|
|
} from "../../domain/models/project-location-structure-project-command.model.js";
|
|
import {
|
|
createFloorSchema,
|
|
deleteProjectLocationSchema,
|
|
updateFloorSchema,
|
|
} from "../../shared/validation/project-structure.schemas.js";
|
|
import { projectCommandService } from "../composition/project-command-stores.js";
|
|
import { floorRepository } from "../composition/application-repositories.js";
|
|
import { respondWithProjectCommandError } from "./project-command.controller.js";
|
|
|
|
export async function listFloorsByProject(req: Request, res: Response) {
|
|
const { projectId } = req.params;
|
|
if (typeof projectId !== "string") {
|
|
return res.status(400).json({ error: "Invalid projectId" });
|
|
}
|
|
|
|
const result = await floorRepository.listByProject(projectId);
|
|
return res.json(result);
|
|
}
|
|
|
|
export async function createFloor(req: Request, res: Response) {
|
|
const { projectId } = req.params;
|
|
if (typeof projectId !== "string") {
|
|
return res.status(400).json({ error: "Invalid projectId" });
|
|
}
|
|
|
|
const parsed = createFloorSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
const existingFloors = await floorRepository.listByProject(projectId);
|
|
const nextSortOrder = existingFloors.length
|
|
? Math.max(...existingFloors.map((floor) => floor.sortOrder)) + 1
|
|
: 0;
|
|
const floor = createProjectFloorSnapshot(
|
|
projectId,
|
|
parsed.data.name,
|
|
nextSortOrder,
|
|
parsed.data.buildingId ?? null
|
|
);
|
|
try {
|
|
const result = projectCommandService.executeUser({
|
|
projectId,
|
|
expectedRevision: parsed.data.expectedRevision,
|
|
description: "Geschoss anlegen",
|
|
command: createProjectFloorInsertProjectCommand(floor),
|
|
});
|
|
return res.status(201).json({ ...result, floor });
|
|
} catch (error) {
|
|
return respondWithProjectCommandError(error, res);
|
|
}
|
|
}
|
|
|
|
export async function updateFloor(req: Request, res: Response) {
|
|
const { projectId, floorId } = req.params;
|
|
if (typeof projectId !== "string" || typeof floorId !== "string") {
|
|
return res.status(400).json({ error: "Invalid parameters" });
|
|
}
|
|
const parsed = updateFloorSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
const current = await floorRepository.findById(projectId, floorId);
|
|
if (!current) {
|
|
return res.status(404).json({ error: "Floor not found" });
|
|
}
|
|
const target = {
|
|
...current,
|
|
name: parsed.data.name,
|
|
buildingId:
|
|
parsed.data.buildingId === undefined
|
|
? current.buildingId
|
|
: parsed.data.buildingId,
|
|
};
|
|
try {
|
|
const result = projectCommandService.executeUser({
|
|
projectId,
|
|
expectedRevision: parsed.data.expectedRevision,
|
|
description: "Geschoss bearbeiten",
|
|
command: createProjectFloorUpdateProjectCommand(current, target),
|
|
});
|
|
return res.json({ ...result, floor: target });
|
|
} catch (error) {
|
|
return respondWithProjectCommandError(error, res);
|
|
}
|
|
}
|
|
|
|
export async function deleteFloor(req: Request, res: Response) {
|
|
const { projectId, floorId } = req.params;
|
|
if (typeof projectId !== "string" || typeof floorId !== "string") {
|
|
return res.status(400).json({ error: "Invalid parameters" });
|
|
}
|
|
const parsed = deleteProjectLocationSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
const current = await floorRepository.findById(projectId, floorId);
|
|
if (!current) {
|
|
return res.status(404).json({ error: "Floor not found" });
|
|
}
|
|
try {
|
|
const result = projectCommandService.executeUser({
|
|
projectId,
|
|
expectedRevision: parsed.data.expectedRevision,
|
|
description: "Geschoss löschen",
|
|
command: createProjectFloorDeleteProjectCommand(current),
|
|
});
|
|
return res.json(result);
|
|
} catch (error) {
|
|
return respondWithProjectCommandError(error, res);
|
|
}
|
|
}
|