forked from jappel/leistungsbilanz-ts
Add buildings above floors and a building structure page
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>
This commit is contained in:
parent
7692699ee2
commit
cd89c4a499
42 changed files with 5883 additions and 373 deletions
100
tests/building-structure-frontend-api.test.ts
Normal file
100
tests/building-structure-frontend-api.test.ts
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import {
|
||||
createBuilding,
|
||||
createFloor,
|
||||
deleteBuilding,
|
||||
listBuildings,
|
||||
updateBuilding,
|
||||
updateFloor,
|
||||
} from "../src/frontend/utils/api.js";
|
||||
|
||||
describe("building structure frontend API", () => {
|
||||
it("sends building commands and optional floor building assignments", async () => {
|
||||
const requests: Array<{ url: string; method: string; body: unknown }> = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (input, init) => {
|
||||
requests.push({
|
||||
url: String(input),
|
||||
method: init?.method ?? "GET",
|
||||
body: init?.body === undefined ? undefined : JSON.parse(String(init.body)),
|
||||
});
|
||||
return new Response(JSON.stringify([]), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
};
|
||||
try {
|
||||
await listBuildings("project-1");
|
||||
await createBuilding("project-1", { name: "Haus A" }, 3);
|
||||
await updateBuilding("project-1", "building-1", { name: "Haus B" }, 4);
|
||||
await deleteBuilding("project-1", "building-1", 5);
|
||||
await createFloor(
|
||||
"project-1",
|
||||
{ name: "EG", buildingId: "building-1" },
|
||||
6
|
||||
);
|
||||
await createFloor("project-1", { name: "Dach", buildingId: null }, 7);
|
||||
await createFloor("project-1", { name: "UG", buildingId: undefined }, 8);
|
||||
await updateFloor(
|
||||
"project-1",
|
||||
"floor-1",
|
||||
{ name: "1. OG", buildingId: null },
|
||||
9
|
||||
);
|
||||
await updateFloor("project-1", "floor-1", { name: "2. OG" }, 10);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
|
||||
assert.deepEqual(requests, [
|
||||
{
|
||||
url: "/api/projects/project-1/buildings",
|
||||
method: "GET",
|
||||
body: undefined,
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/buildings",
|
||||
method: "POST",
|
||||
body: { name: "Haus A", expectedRevision: 3 },
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/buildings/building-1",
|
||||
method: "PUT",
|
||||
body: { name: "Haus B", expectedRevision: 4 },
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/buildings/building-1",
|
||||
method: "DELETE",
|
||||
body: { expectedRevision: 5 },
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/floors",
|
||||
method: "POST",
|
||||
body: { name: "EG", buildingId: "building-1", expectedRevision: 6 },
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/floors",
|
||||
method: "POST",
|
||||
body: { name: "Dach", buildingId: null, expectedRevision: 7 },
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/floors",
|
||||
method: "POST",
|
||||
body: { name: "UG", expectedRevision: 8 },
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/floors/floor-1",
|
||||
method: "PUT",
|
||||
body: { name: "1. OG", buildingId: null, expectedRevision: 9 },
|
||||
},
|
||||
{
|
||||
url: "/api/projects/project-1/floors/floor-1",
|
||||
method: "PUT",
|
||||
body: { name: "2. OG", expectedRevision: 10 },
|
||||
},
|
||||
]);
|
||||
const updateWithoutBuilding = requests[8].body as Record<string, unknown>;
|
||||
assert.equal("buildingId" in updateWithoutBuilding, false);
|
||||
});
|
||||
});
|
||||
284
tests/building-structure-view.test.ts
Normal file
284
tests/building-structure-view.test.ts
Normal file
|
|
@ -0,0 +1,284 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import type {
|
||||
BuildingDto,
|
||||
FloorDto,
|
||||
RoomDto,
|
||||
} from "../src/frontend/types.js";
|
||||
import {
|
||||
ALL_SELECTION,
|
||||
UNASSIGNED_SELECTION,
|
||||
countFloorsByBuilding,
|
||||
countRoomsByFloor,
|
||||
defaultBuildingIdForNewFloor,
|
||||
defaultFloorIdForNewRoom,
|
||||
describeRoomLocation,
|
||||
filterFloorsByBuilding,
|
||||
filterRooms,
|
||||
groupFloorsByBuilding,
|
||||
hasFloorsWithoutBuilding,
|
||||
hasRoomsWithoutFloor,
|
||||
normalizeBuildingSelection,
|
||||
normalizeFloorSelection,
|
||||
showsRoomsWithoutFloorEntry,
|
||||
} from "../src/frontend/utils/building-structure.js";
|
||||
|
||||
function building(id: string, name: string, sortOrder: number): BuildingDto {
|
||||
return { id, projectId: "project-1", name, sortOrder };
|
||||
}
|
||||
|
||||
function floor(
|
||||
id: string,
|
||||
name: string,
|
||||
buildingId: string | null,
|
||||
sortOrder = 0
|
||||
): FloorDto {
|
||||
return { id, projectId: "project-1", name, sortOrder, buildingId };
|
||||
}
|
||||
|
||||
function room(id: string, floorId: string | null, roomNumber = id): RoomDto {
|
||||
return {
|
||||
id,
|
||||
projectId: "project-1",
|
||||
floorId,
|
||||
roomNumber,
|
||||
roomName: `Raum ${roomNumber}`,
|
||||
};
|
||||
}
|
||||
|
||||
const buildings = [building("b-a", "Haus A", 0), building("b-b", "Haus B", 1)];
|
||||
const floors = [
|
||||
floor("f-free", "Außenanlage", null, 0),
|
||||
floor("f-a-eg", "EG", "b-a", 1),
|
||||
floor("f-b-eg", "EG", "b-b", 2),
|
||||
floor("f-a-og", "1. OG", "b-a", 3),
|
||||
floor("f-orphan", "Keller", "b-deleted", 4),
|
||||
];
|
||||
const rooms = [
|
||||
room("r-a-eg", "f-a-eg"),
|
||||
room("r-a-og", "f-a-og"),
|
||||
room("r-b-eg", "f-b-eg"),
|
||||
room("r-free", "f-free"),
|
||||
room("r-orphan-floor", "f-orphan"),
|
||||
room("r-none", null),
|
||||
room("r-missing-floor", "f-deleted"),
|
||||
];
|
||||
|
||||
const ids = (entries: ReadonlyArray<{ id: string }>) =>
|
||||
entries.map((entry) => entry.id);
|
||||
|
||||
describe("building structure view", () => {
|
||||
it("groups floors in building order and puts unassigned floors last", () => {
|
||||
const groups = groupFloorsByBuilding(floors, buildings);
|
||||
assert.deepEqual(
|
||||
groups.map((group) => ({
|
||||
buildingId: group.buildingId,
|
||||
label: group.label,
|
||||
floors: ids(group.floors),
|
||||
})),
|
||||
[
|
||||
{ buildingId: "b-a", label: "Haus A", floors: ["f-a-eg", "f-a-og"] },
|
||||
{ buildingId: "b-b", label: "Haus B", floors: ["f-b-eg"] },
|
||||
{
|
||||
buildingId: null,
|
||||
label: "Ohne Gebäude",
|
||||
floors: ["f-free", "f-orphan"],
|
||||
},
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
it("omits empty groups", () => {
|
||||
const groups = groupFloorsByBuilding(
|
||||
[floor("f-1", "EG", "b-b")],
|
||||
buildings
|
||||
);
|
||||
assert.deepEqual(
|
||||
groups.map((group) => group.buildingId),
|
||||
["b-b"]
|
||||
);
|
||||
assert.deepEqual(groupFloorsByBuilding([], buildings), []);
|
||||
});
|
||||
|
||||
it("filters floors by building selection", () => {
|
||||
assert.deepEqual(
|
||||
ids(filterFloorsByBuilding(floors, buildings, ALL_SELECTION)),
|
||||
ids(floors)
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(filterFloorsByBuilding(floors, buildings, "b-a")),
|
||||
["f-a-eg", "f-a-og"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(filterFloorsByBuilding(floors, buildings, UNASSIGNED_SELECTION)),
|
||||
["f-free", "f-orphan"]
|
||||
);
|
||||
});
|
||||
|
||||
it("detects floors without building and rooms without floor", () => {
|
||||
assert.equal(hasFloorsWithoutBuilding(floors, buildings), true);
|
||||
assert.equal(
|
||||
hasFloorsWithoutBuilding([floor("f-1", "EG", "b-a")], buildings),
|
||||
false
|
||||
);
|
||||
assert.equal(hasRoomsWithoutFloor(rooms, floors), true);
|
||||
assert.equal(hasRoomsWithoutFloor([room("r-1", "f-a-eg")], floors), false);
|
||||
});
|
||||
|
||||
it("filters rooms for building and floor selections", () => {
|
||||
assert.deepEqual(
|
||||
ids(filterRooms(rooms, floors, buildings, ALL_SELECTION, ALL_SELECTION)),
|
||||
ids(rooms)
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(filterRooms(rooms, floors, buildings, "b-a", ALL_SELECTION)),
|
||||
["r-a-eg", "r-a-og"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(filterRooms(rooms, floors, buildings, "b-a", "f-a-og")),
|
||||
["r-a-og"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(filterRooms(rooms, floors, buildings, ALL_SELECTION, "f-b-eg")),
|
||||
["r-b-eg"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(
|
||||
filterRooms(
|
||||
rooms,
|
||||
floors,
|
||||
buildings,
|
||||
UNASSIGNED_SELECTION,
|
||||
ALL_SELECTION
|
||||
)
|
||||
),
|
||||
["r-free", "r-orphan-floor", "r-none", "r-missing-floor"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(
|
||||
filterRooms(
|
||||
rooms,
|
||||
floors,
|
||||
buildings,
|
||||
ALL_SELECTION,
|
||||
UNASSIGNED_SELECTION
|
||||
)
|
||||
),
|
||||
["r-none", "r-missing-floor"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
ids(filterRooms(rooms, floors, buildings, "b-a", UNASSIGNED_SELECTION)),
|
||||
[]
|
||||
);
|
||||
});
|
||||
|
||||
it("offers 'Ohne Etage' only where rooms without floor can belong", () => {
|
||||
assert.equal(showsRoomsWithoutFloorEntry(rooms, floors, ALL_SELECTION), true);
|
||||
assert.equal(
|
||||
showsRoomsWithoutFloorEntry(rooms, floors, UNASSIGNED_SELECTION),
|
||||
true
|
||||
);
|
||||
assert.equal(showsRoomsWithoutFloorEntry(rooms, floors, "b-a"), false);
|
||||
assert.equal(
|
||||
showsRoomsWithoutFloorEntry([room("r-1", "f-a-eg")], floors, ALL_SELECTION),
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to 'Alle' for vanished or hidden selections", () => {
|
||||
assert.equal(normalizeBuildingSelection("b-a", floors, buildings), "b-a");
|
||||
assert.equal(
|
||||
normalizeBuildingSelection("b-deleted", floors, buildings),
|
||||
ALL_SELECTION
|
||||
);
|
||||
assert.equal(
|
||||
normalizeBuildingSelection(UNASSIGNED_SELECTION, floors, buildings),
|
||||
UNASSIGNED_SELECTION
|
||||
);
|
||||
assert.equal(
|
||||
normalizeBuildingSelection(
|
||||
UNASSIGNED_SELECTION,
|
||||
[floor("f-1", "EG", "b-a")],
|
||||
buildings
|
||||
),
|
||||
ALL_SELECTION
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
normalizeFloorSelection("f-a-og", rooms, floors, buildings, "b-a"),
|
||||
"f-a-og"
|
||||
);
|
||||
assert.equal(
|
||||
normalizeFloorSelection("f-b-eg", rooms, floors, buildings, "b-a"),
|
||||
ALL_SELECTION
|
||||
);
|
||||
assert.equal(
|
||||
normalizeFloorSelection("f-deleted", rooms, floors, buildings, ALL_SELECTION),
|
||||
ALL_SELECTION
|
||||
);
|
||||
assert.equal(
|
||||
normalizeFloorSelection(
|
||||
UNASSIGNED_SELECTION,
|
||||
rooms,
|
||||
floors,
|
||||
buildings,
|
||||
ALL_SELECTION
|
||||
),
|
||||
UNASSIGNED_SELECTION
|
||||
);
|
||||
assert.equal(
|
||||
normalizeFloorSelection(UNASSIGNED_SELECTION, rooms, floors, buildings, "b-a"),
|
||||
ALL_SELECTION
|
||||
);
|
||||
});
|
||||
|
||||
it("preselects the selected building and floor for new entries", () => {
|
||||
assert.equal(defaultBuildingIdForNewFloor("b-a"), "b-a");
|
||||
assert.equal(defaultBuildingIdForNewFloor(ALL_SELECTION), "");
|
||||
assert.equal(defaultBuildingIdForNewFloor(UNASSIGNED_SELECTION), "");
|
||||
assert.equal(defaultFloorIdForNewRoom("f-a-eg"), "f-a-eg");
|
||||
assert.equal(defaultFloorIdForNewRoom(ALL_SELECTION), "");
|
||||
assert.equal(defaultFloorIdForNewRoom(UNASSIGNED_SELECTION), "");
|
||||
});
|
||||
|
||||
it("counts floors per building and rooms per floor", () => {
|
||||
assert.deepEqual(
|
||||
[...countFloorsByBuilding(floors, buildings)],
|
||||
[
|
||||
[null, 2],
|
||||
["b-a", 2],
|
||||
["b-b", 1],
|
||||
]
|
||||
);
|
||||
assert.deepEqual(
|
||||
[...countRoomsByFloor(rooms, floors)],
|
||||
[
|
||||
["f-a-eg", 1],
|
||||
["f-a-og", 1],
|
||||
["f-b-eg", 1],
|
||||
["f-free", 1],
|
||||
["f-orphan", 1],
|
||||
[null, 2],
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
it("describes a room location with floor and building", () => {
|
||||
assert.equal(
|
||||
describeRoomLocation(room("r", "f-a-og"), floors, buildings),
|
||||
"1. OG · Haus A"
|
||||
);
|
||||
assert.equal(
|
||||
describeRoomLocation(room("r", "f-free"), floors, buildings),
|
||||
"Außenanlage"
|
||||
);
|
||||
assert.equal(
|
||||
describeRoomLocation(room("r", "f-orphan"), floors, buildings),
|
||||
"Keller"
|
||||
);
|
||||
assert.equal(
|
||||
describeRoomLocation(room("r", null), floors, buildings),
|
||||
"Ohne Etage"
|
||||
);
|
||||
});
|
||||
});
|
||||
305
tests/project-building-structure.test.ts
Normal file
305
tests/project-building-structure.test.ts
Normal file
|
|
@ -0,0 +1,305 @@
|
|||
import path from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import {
|
||||
createDatabaseContext,
|
||||
type DatabaseContext,
|
||||
} from "../src/db/database-context.js";
|
||||
import { ProjectHistoryRepository } from "../src/db/repositories/project-history.repository.js";
|
||||
import { ProjectLocationStructureProjectCommandRepository } from "../src/db/repositories/project-location-structure-project-command.repository.js";
|
||||
import { buildings } from "../src/db/schema/buildings.js";
|
||||
import { floors } from "../src/db/schema/floors.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
import {
|
||||
assertProjectFloorInsertProjectCommand,
|
||||
createProjectBuildingDeleteProjectCommand,
|
||||
createProjectBuildingInsertProjectCommand,
|
||||
createProjectBuildingSnapshot,
|
||||
createProjectBuildingUpdateProjectCommand,
|
||||
createProjectFloorInsertProjectCommand,
|
||||
createProjectFloorSnapshot,
|
||||
createProjectFloorUpdateProjectCommand,
|
||||
projectFloorCommandSchemaVersion,
|
||||
type ProjectLocationStructureProjectCommand,
|
||||
} from "../src/domain/models/project-location-structure-project-command.model.js";
|
||||
import {
|
||||
createBuildingSchema,
|
||||
createFloorSchema,
|
||||
updateFloorSchema,
|
||||
} from "../src/shared/validation/project-structure.schemas.js";
|
||||
|
||||
function createTestDatabase(): DatabaseContext {
|
||||
const context = createDatabaseContext(":memory:");
|
||||
migrate(context.db, {
|
||||
migrationsFolder: path.resolve("src", "db", "migrations"),
|
||||
});
|
||||
context.db
|
||||
.insert(projects)
|
||||
.values([
|
||||
{ id: "project-1", name: "Test project" },
|
||||
{ id: "project-2", name: "Foreign project" },
|
||||
])
|
||||
.run();
|
||||
return context;
|
||||
}
|
||||
|
||||
/** Executes a user command and returns a runner for its undo and redo. */
|
||||
function createRunner(context: DatabaseContext) {
|
||||
const repository = new ProjectLocationStructureProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
const history = new ProjectHistoryRepository(context.db);
|
||||
let revision = 0;
|
||||
const run = (
|
||||
command: ProjectLocationStructureProjectCommand,
|
||||
source: "user" | "undo" | "redo" = "user"
|
||||
) => {
|
||||
const step =
|
||||
source === "user" ? null : history.getNextCommand("project-1", source);
|
||||
if (source !== "user") {
|
||||
assert.ok(step);
|
||||
}
|
||||
const result = repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: revision,
|
||||
source,
|
||||
historyTargetChangeSetId: step?.changeSetId,
|
||||
command,
|
||||
});
|
||||
revision += 1;
|
||||
return result;
|
||||
};
|
||||
return { repository, run };
|
||||
}
|
||||
|
||||
function getBuilding(context: DatabaseContext, id: string) {
|
||||
return context.db.select().from(buildings).where(eq(buildings.id, id)).get();
|
||||
}
|
||||
|
||||
function getFloor(context: DatabaseContext, id: string) {
|
||||
return context.db.select().from(floors).where(eq(floors.id, id)).get();
|
||||
}
|
||||
|
||||
describe("project building structure", () => {
|
||||
it("creates, renames and deletes buildings with persisted undo and redo", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const { run } = createRunner(context);
|
||||
const building = createProjectBuildingSnapshot("project-1", " Haus A ", 0);
|
||||
assert.equal(building.name, "Haus A");
|
||||
|
||||
const insert = createProjectBuildingInsertProjectCommand(building);
|
||||
const inserted = run(insert);
|
||||
assert.deepEqual(getBuilding(context, building.id), building);
|
||||
|
||||
const renamed = { ...building, name: "Haus B" };
|
||||
const update = createProjectBuildingUpdateProjectCommand(building, renamed);
|
||||
const updated = run(update);
|
||||
assert.equal(getBuilding(context, building.id)?.name, "Haus B");
|
||||
|
||||
run(updated.inverse, "undo");
|
||||
assert.deepEqual(getBuilding(context, building.id), building);
|
||||
run(inserted.inverse, "undo");
|
||||
assert.equal(getBuilding(context, building.id), undefined);
|
||||
run(insert, "redo");
|
||||
run(update, "redo");
|
||||
assert.deepEqual(getBuilding(context, building.id), renamed);
|
||||
|
||||
const deleted = run(createProjectBuildingDeleteProjectCommand(renamed));
|
||||
assert.equal(getBuilding(context, building.id), undefined);
|
||||
run(deleted.inverse, "undo");
|
||||
assert.deepEqual(getBuilding(context, building.id), renamed);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("assigns floors to buildings and blocks deleting a building in use", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const { run } = createRunner(context);
|
||||
const houseA = createProjectBuildingSnapshot("project-1", "Haus A", 0);
|
||||
const houseB = createProjectBuildingSnapshot("project-1", "Haus B", 1);
|
||||
run(createProjectBuildingInsertProjectCommand(houseA));
|
||||
run(createProjectBuildingInsertProjectCommand(houseB));
|
||||
|
||||
const floor = createProjectFloorSnapshot("project-1", "EG", 0, houseA.id);
|
||||
const floorInsert = createProjectFloorInsertProjectCommand(floor);
|
||||
assert.equal(floorInsert.schemaVersion, projectFloorCommandSchemaVersion);
|
||||
run(floorInsert);
|
||||
assert.deepEqual(getFloor(context, floor.id), floor);
|
||||
|
||||
const moved = { ...floor, buildingId: houseB.id };
|
||||
const moveCommand = createProjectFloorUpdateProjectCommand(floor, moved);
|
||||
const movedResult = run(moveCommand);
|
||||
assert.equal(getFloor(context, floor.id)?.buildingId, houseB.id);
|
||||
|
||||
assert.throws(
|
||||
() => run(createProjectBuildingDeleteProjectCommand(houseB)),
|
||||
/solange Etagen zugeordnet/
|
||||
);
|
||||
assert.ok(getBuilding(context, houseB.id));
|
||||
|
||||
run(movedResult.inverse, "undo");
|
||||
assert.equal(getFloor(context, floor.id)?.buildingId, houseA.id);
|
||||
run(moveCommand, "redo");
|
||||
assert.equal(getFloor(context, floor.id)?.buildingId, houseB.id);
|
||||
|
||||
const unassigned = { ...moved, buildingId: null };
|
||||
run(createProjectFloorUpdateProjectCommand(moved, unassigned));
|
||||
assert.equal(getFloor(context, floor.id)?.buildingId, null);
|
||||
run(createProjectBuildingDeleteProjectCommand(houseB));
|
||||
assert.equal(getBuilding(context, houseB.id), undefined);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects floors that reference a missing or foreign building", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const { run } = createRunner(context);
|
||||
context.db
|
||||
.insert(buildings)
|
||||
.values({ id: "foreign", projectId: "project-2", name: "Fremd", sortOrder: 0 })
|
||||
.run();
|
||||
assert.throws(
|
||||
() =>
|
||||
run(
|
||||
createProjectFloorInsertProjectCommand(
|
||||
createProjectFloorSnapshot("project-1", "EG", 0, "foreign")
|
||||
)
|
||||
),
|
||||
/foreign building/
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
run(
|
||||
createProjectFloorInsertProjectCommand(
|
||||
createProjectFloorSnapshot("project-1", "EG", 0, "missing")
|
||||
)
|
||||
),
|
||||
/foreign building/
|
||||
);
|
||||
assert.equal(context.db.select().from(floors).all().length, 0);
|
||||
|
||||
const floor = createProjectFloorSnapshot("project-1", "EG", 0);
|
||||
run(createProjectFloorInsertProjectCommand(floor));
|
||||
assert.throws(
|
||||
() =>
|
||||
run(
|
||||
createProjectFloorUpdateProjectCommand(floor, {
|
||||
...floor,
|
||||
buildingId: "foreign",
|
||||
})
|
||||
),
|
||||
/foreign building/
|
||||
);
|
||||
assert.throws(
|
||||
() => createProjectFloorUpdateProjectCommand(floor, { ...floor }),
|
||||
/only the name or the building/
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
createProjectFloorUpdateProjectCommand(floor, {
|
||||
...floor,
|
||||
sortOrder: 5,
|
||||
name: "Erdgeschoss",
|
||||
}),
|
||||
/only the name or the building/
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("executes version 1 floor commands recorded before buildings existed", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const { run } = createRunner(context);
|
||||
const legacyFloor = {
|
||||
id: "floor-legacy",
|
||||
projectId: "project-1",
|
||||
name: "EG",
|
||||
sortOrder: 0,
|
||||
};
|
||||
const legacyInsert = {
|
||||
schemaVersion: 1 as const,
|
||||
type: "project-floor.insert" as const,
|
||||
payload: { floor: legacyFloor },
|
||||
};
|
||||
assertProjectFloorInsertProjectCommand(legacyInsert);
|
||||
const inserted = run(legacyInsert);
|
||||
assert.deepEqual(getFloor(context, legacyFloor.id), {
|
||||
...legacyFloor,
|
||||
buildingId: null,
|
||||
});
|
||||
// The inverse is always written in the current version.
|
||||
assert.equal(inserted.inverse.schemaVersion, projectFloorCommandSchemaVersion);
|
||||
|
||||
const legacyUpdate = {
|
||||
schemaVersion: 1 as const,
|
||||
type: "project-floor.update" as const,
|
||||
payload: {
|
||||
expected: legacyFloor,
|
||||
target: { ...legacyFloor, name: "Erdgeschoss" },
|
||||
},
|
||||
};
|
||||
const updated = run(legacyUpdate);
|
||||
assert.equal(getFloor(context, legacyFloor.id)?.name, "Erdgeschoss");
|
||||
run(updated.inverse, "undo");
|
||||
assert.equal(getFloor(context, legacyFloor.id)?.name, "EG");
|
||||
|
||||
// A version 1 snapshot must not carry buildingId, a version 2 one must.
|
||||
assert.throws(() =>
|
||||
assertProjectFloorInsertProjectCommand({
|
||||
schemaVersion: 1,
|
||||
type: "project-floor.insert",
|
||||
payload: { floor: { ...legacyFloor, buildingId: null } },
|
||||
})
|
||||
);
|
||||
assert.throws(() =>
|
||||
assertProjectFloorInsertProjectCommand({
|
||||
schemaVersion: 2,
|
||||
type: "project-floor.insert",
|
||||
payload: { floor: legacyFloor },
|
||||
})
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("validates building and floor requests", () => {
|
||||
assert.equal(
|
||||
createBuildingSchema.safeParse({ expectedRevision: 0, name: "Haus A" }).success,
|
||||
true
|
||||
);
|
||||
assert.equal(
|
||||
createBuildingSchema.safeParse({ expectedRevision: 0, name: " " }).success,
|
||||
false
|
||||
);
|
||||
assert.equal(
|
||||
createBuildingSchema.safeParse({
|
||||
expectedRevision: 0,
|
||||
name: "Haus A",
|
||||
sortOrder: 1,
|
||||
}).success,
|
||||
false
|
||||
);
|
||||
for (const buildingId of [undefined, null, "building-1"]) {
|
||||
assert.equal(
|
||||
createFloorSchema.safeParse({ expectedRevision: 0, name: "EG", buildingId })
|
||||
.success,
|
||||
true
|
||||
);
|
||||
}
|
||||
assert.equal(
|
||||
updateFloorSchema.safeParse({ expectedRevision: 0, name: "EG", buildingId: "" })
|
||||
.success,
|
||||
false
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -41,6 +41,7 @@ import { projectChangeSets } from "../src/db/schema/project-change-sets.js";
|
|||
import { projectDevices } from "../src/db/schema/project-devices.js";
|
||||
import { projectRevisions } from "../src/db/schema/project-revisions.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
import { buildings } from "../src/db/schema/buildings.js";
|
||||
import { floors } from "../src/db/schema/floors.js";
|
||||
import { rooms } from "../src/db/schema/rooms.js";
|
||||
import { externalCsvConfigurations } from "../src/db/schema/external-csv-configurations.js";
|
||||
|
|
@ -78,6 +79,8 @@ import {
|
|||
createDistributionBoardComponentUpdateProjectCommand,
|
||||
} from "../src/domain/models/distribution-board-component-structure-project-command.model.js";
|
||||
import {
|
||||
createProjectBuildingInsertProjectCommand,
|
||||
createProjectBuildingSnapshot,
|
||||
createProjectFloorInsertProjectCommand,
|
||||
createProjectFloorSnapshot,
|
||||
createProjectRoomInsertProjectCommand,
|
||||
|
|
@ -1610,6 +1613,43 @@ describe("project command service", () => {
|
|||
}
|
||||
});
|
||||
|
||||
it("dispatches building commands and floor assignments with persisted inverses", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const service = createService(context);
|
||||
const building = createProjectBuildingSnapshot("project-1", "Haus A", 0);
|
||||
service.executeUser({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
command: createProjectBuildingInsertProjectCommand(building),
|
||||
});
|
||||
const floor = createProjectFloorSnapshot("project-1", "EG", 0, building.id);
|
||||
const created = service.executeUser({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
command: createProjectFloorInsertProjectCommand(floor),
|
||||
});
|
||||
assert.equal(created.history.currentRevision, 2);
|
||||
assert.equal(
|
||||
context.db.select().from(floors).where(eq(floors.id, floor.id)).get()
|
||||
?.buildingId,
|
||||
building.id
|
||||
);
|
||||
|
||||
createService(context).undo({ projectId: "project-1", expectedRevision: 2 });
|
||||
createService(context).undo({ projectId: "project-1", expectedRevision: 3 });
|
||||
assert.equal(context.db.select().from(buildings).all().length, 0);
|
||||
createService(context).redo({ projectId: "project-1", expectedRevision: 4 });
|
||||
createService(context).redo({ projectId: "project-1", expectedRevision: 5 });
|
||||
assert.deepEqual(
|
||||
context.db.select().from(floors).where(eq(floors.id, floor.id)).get(),
|
||||
floor
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("dispatches floor and room setup with their persisted inverses", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import { circuits } from "../src/db/schema/circuits.js";
|
|||
import { distributionBoardComponentProtectionDevices } from "../src/db/schema/distribution-board-component-protection-devices.js";
|
||||
import { distributionBoardComponents } from "../src/db/schema/distribution-board-components.js";
|
||||
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
|
||||
import { buildings } from "../src/db/schema/buildings.js";
|
||||
import { floors } from "../src/db/schema/floors.js";
|
||||
import { projectDevices } from "../src/db/schema/project-devices.js";
|
||||
import { projectSnapshots } from "../src/db/schema/project-snapshots.js";
|
||||
|
|
@ -59,6 +60,15 @@ function createTestDatabase(): DatabaseContext {
|
|||
.where(eq(circuitSections.circuitListId, list.id))
|
||||
.get();
|
||||
assert.ok(section);
|
||||
context.db
|
||||
.insert(buildings)
|
||||
.values({
|
||||
id: "building-1",
|
||||
projectId: "project-1",
|
||||
name: "Haus A",
|
||||
sortOrder: 0,
|
||||
})
|
||||
.run();
|
||||
context.db
|
||||
.insert(floors)
|
||||
.values({
|
||||
|
|
@ -66,6 +76,7 @@ function createTestDatabase(): DatabaseContext {
|
|||
projectId: "project-1",
|
||||
name: "EG",
|
||||
sortOrder: 10,
|
||||
buildingId: "building-1",
|
||||
})
|
||||
.run();
|
||||
context.db
|
||||
|
|
@ -248,7 +259,9 @@ describe("project snapshot repository", () => {
|
|||
"device-1"
|
||||
);
|
||||
assert.equal(state.projectDevices.length, 1);
|
||||
assert.equal(state.buildings.length, 1);
|
||||
assert.equal(state.floors.length, 1);
|
||||
assert.equal(state.floors[0].buildingId, "building-1");
|
||||
assert.equal(state.rooms.length, 1);
|
||||
assert.deepEqual(repository.listByProject("project-1"), [
|
||||
created,
|
||||
|
|
@ -400,8 +413,14 @@ describe("project snapshot repository", () => {
|
|||
schemaVersion: number;
|
||||
project: Record<string, unknown>;
|
||||
distributionBoards: Array<Record<string, unknown>>;
|
||||
buildings?: unknown;
|
||||
floors: Array<Record<string, unknown>>;
|
||||
};
|
||||
payload.schemaVersion = 5;
|
||||
delete payload.buildings;
|
||||
for (const floor of payload.floors) {
|
||||
delete floor.buildingId;
|
||||
}
|
||||
for (const field of ["siteAddress", "workStage", "projectLead", "projectEditor"]) {
|
||||
delete payload.project[field];
|
||||
}
|
||||
|
|
@ -434,6 +453,9 @@ describe("project snapshot repository", () => {
|
|||
(board) => board.boardNumber === null && board.fedFrom === null
|
||||
)
|
||||
);
|
||||
// Version 5 predates buildings: the upgraded state has none.
|
||||
assert.deepEqual(targetState.buildings, []);
|
||||
assert.ok(targetState.floors.every((floor) => floor.buildingId === null));
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
|
|
@ -497,6 +519,11 @@ describe("portable project transfer", () => {
|
|||
copied.state.distributionBoards[0].floorId,
|
||||
copied.state.floors[0].id
|
||||
);
|
||||
assert.notEqual(copied.state.buildings[0].id, "building-1");
|
||||
assert.equal(
|
||||
copied.state.floors[0].buildingId,
|
||||
copied.state.buildings[0].id
|
||||
);
|
||||
assert.equal(
|
||||
copied.state.distributionBoards[0].supplyType,
|
||||
"SV"
|
||||
|
|
|
|||
|
|
@ -8,10 +8,22 @@ import {
|
|||
} from "../src/domain/models/project-state-snapshot.model.js";
|
||||
import { createNamedProjectSnapshotSchema } from "../src/shared/validation/project-snapshot.schemas.js";
|
||||
|
||||
/** Removes the fields introduced by snapshot schema version 6. */
|
||||
function withoutVersionSixFields<
|
||||
T extends { project: object; distributionBoards: object[] },
|
||||
/** Removes the fields introduced by snapshot schema version 7. */
|
||||
function withoutVersionSevenFields<
|
||||
T extends { floors: object[] },
|
||||
>(snapshot: T): T {
|
||||
delete (snapshot as Record<string, unknown>).buildings;
|
||||
for (const floor of snapshot.floors) {
|
||||
delete (floor as Record<string, unknown>).buildingId;
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
/** Removes the fields introduced by snapshot schema versions 6 and 7. */
|
||||
function withoutVersionSixFields<
|
||||
T extends { project: object; distributionBoards: object[]; floors: object[] },
|
||||
>(snapshot: T): T {
|
||||
withoutVersionSevenFields(snapshot);
|
||||
const project = snapshot.project as Record<string, unknown>;
|
||||
for (const field of ["siteAddress", "workStage", "projectLead", "projectEditor"]) {
|
||||
delete project[field];
|
||||
|
|
@ -66,7 +78,19 @@ function minimalSnapshot() {
|
|||
distributionBoardComponentProtectionDevices: [],
|
||||
circuits: [],
|
||||
projectDevices: [],
|
||||
floors: [],
|
||||
buildings: [] as Array<{
|
||||
id: string;
|
||||
projectId: string;
|
||||
name: string;
|
||||
sortOrder: number;
|
||||
}>,
|
||||
floors: [] as Array<{
|
||||
id: string;
|
||||
projectId: string;
|
||||
name: string;
|
||||
sortOrder: number;
|
||||
buildingId: string | null;
|
||||
}>,
|
||||
rooms: [],
|
||||
};
|
||||
}
|
||||
|
|
@ -306,7 +330,10 @@ describe("project state snapshot model", () => {
|
|||
delete (outdated.project as Record<string, unknown>).description;
|
||||
assert.throws(() => parseProjectStateSnapshot(outdated));
|
||||
assert.throws(() =>
|
||||
parseProjectStateSnapshot({ ...minimalSnapshot(), schemaVersion: 7 })
|
||||
parseProjectStateSnapshot({
|
||||
...minimalSnapshot(),
|
||||
schemaVersion: projectStateSnapshotSchemaVersion + 1,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -355,12 +382,99 @@ describe("project state snapshot model", () => {
|
|||
projectId: "project-2",
|
||||
name: "EG",
|
||||
sortOrder: 10,
|
||||
buildingId: null,
|
||||
});
|
||||
assert.throws(
|
||||
() => parseProjectStateSnapshot(wrongOwner),
|
||||
/floor belongs to a different project/
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps buildings and validates floor building references", () => {
|
||||
const snapshot = minimalSnapshot();
|
||||
snapshot.buildings.push({
|
||||
id: "building-1",
|
||||
projectId: "project-1",
|
||||
name: "Haus A",
|
||||
sortOrder: 0,
|
||||
});
|
||||
snapshot.floors.push(
|
||||
{
|
||||
id: "floor-1",
|
||||
projectId: "project-1",
|
||||
name: "EG",
|
||||
sortOrder: 0,
|
||||
buildingId: "building-1",
|
||||
},
|
||||
{
|
||||
id: "floor-2",
|
||||
projectId: "project-1",
|
||||
name: "Keller",
|
||||
sortOrder: 1,
|
||||
buildingId: null,
|
||||
}
|
||||
);
|
||||
const parsed = deserializeProjectStateSnapshot(
|
||||
serializeProjectStateSnapshot(parseProjectStateSnapshot(snapshot))
|
||||
);
|
||||
assert.deepEqual(parsed.buildings, snapshot.buildings);
|
||||
assert.deepEqual(
|
||||
parsed.floors.map((floor) => floor.buildingId),
|
||||
["building-1", null]
|
||||
);
|
||||
|
||||
const unknownBuilding = structuredClone(snapshot);
|
||||
unknownBuilding.floors[0]!.buildingId = "building-x";
|
||||
assert.throws(
|
||||
() => parseProjectStateSnapshot(unknownBuilding),
|
||||
/floor building/
|
||||
);
|
||||
|
||||
const foreignBuilding = structuredClone(snapshot);
|
||||
foreignBuilding.buildings[0]!.projectId = "project-2";
|
||||
assert.throws(
|
||||
() => parseProjectStateSnapshot(foreignBuilding),
|
||||
/building belongs to a different project/
|
||||
);
|
||||
|
||||
const floorWithoutBuildingField = structuredClone(snapshot);
|
||||
delete (floorWithoutBuildingField.floors[1] as Record<string, unknown>)
|
||||
.buildingId;
|
||||
assert.throws(() => parseProjectStateSnapshot(floorWithoutBuildingField));
|
||||
});
|
||||
|
||||
it("upgrades snapshot version 6 without buildings", () => {
|
||||
const snapshot = minimalSnapshot();
|
||||
snapshot.floors.push({
|
||||
id: "floor-1",
|
||||
projectId: "project-1",
|
||||
name: "EG",
|
||||
sortOrder: 0,
|
||||
buildingId: null,
|
||||
});
|
||||
const previous = withoutVersionSevenFields(structuredClone(snapshot));
|
||||
previous.schemaVersion = 6 as typeof previous.schemaVersion;
|
||||
|
||||
const upgraded = parseProjectStateSnapshot(previous);
|
||||
|
||||
assert.equal(upgraded.schemaVersion, projectStateSnapshotSchemaVersion);
|
||||
assert.deepEqual(upgraded.buildings, []);
|
||||
assert.deepEqual(upgraded.floors, [
|
||||
{
|
||||
id: "floor-1",
|
||||
projectId: "project-1",
|
||||
name: "EG",
|
||||
sortOrder: 0,
|
||||
buildingId: null,
|
||||
},
|
||||
]);
|
||||
// Version 6 cover values survive the upgrade.
|
||||
assert.equal(upgraded.project.workStage, "LPH 5");
|
||||
|
||||
const mislabeled = structuredClone(snapshot);
|
||||
mislabeled.schemaVersion = 6 as typeof mislabeled.schemaVersion;
|
||||
assert.throws(() => parseProjectStateSnapshot(mislabeled));
|
||||
});
|
||||
});
|
||||
|
||||
describe("named project snapshot request", () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue