Add logical distribution board feed links
This commit is contained in:
parent
b50c00c0ac
commit
b893c72534
23 changed files with 3732 additions and 45 deletions
4
src/db/migrations/0012_good_gateway.sql
Normal file
4
src/db/migrations/0012_good_gateway.sql
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
ALTER TABLE `distribution_boards` ADD `feeding_distribution_board_id` text REFERENCES distribution_boards(id);--> statement-breakpoint
|
||||
ALTER TABLE `distribution_boards` ADD `feeding_circuit_id` text REFERENCES circuits(id);--> statement-breakpoint
|
||||
CREATE INDEX `distribution_boards_feeding_board_idx` ON `distribution_boards` (`feeding_distribution_board_id`);--> statement-breakpoint
|
||||
CREATE INDEX `distribution_boards_feeding_circuit_idx` ON `distribution_boards` (`feeding_circuit_id`);
|
||||
2986
src/db/migrations/meta/0012_snapshot.json
Normal file
2986
src/db/migrations/meta/0012_snapshot.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -85,6 +85,13 @@
|
|||
"when": 1790531351492,
|
||||
"tag": "0011_glossy_norman_osborn",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 12,
|
||||
"version": "6",
|
||||
"when": 1790540142918,
|
||||
"tag": "0012_good_gateway",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -325,6 +325,16 @@ export class DistributionBoardStructureProjectCommandRepository
|
|||
patch.supplyType
|
||||
);
|
||||
}
|
||||
this.assertFeedingReference(database, input.projectId, current.id, {
|
||||
feedingDistributionBoardId:
|
||||
patch.feedingDistributionBoardId === undefined
|
||||
? current.feedingDistributionBoardId
|
||||
: patch.feedingDistributionBoardId,
|
||||
feedingCircuitId:
|
||||
patch.feedingCircuitId === undefined
|
||||
? current.feedingCircuitId
|
||||
: patch.feedingCircuitId,
|
||||
});
|
||||
const inversePatch = Object.fromEntries(
|
||||
input.command.payload.changes.map((change) => [
|
||||
change.field,
|
||||
|
|
@ -333,7 +343,8 @@ export class DistributionBoardStructureProjectCommandRepository
|
|||
) as DistributionBoardUpdatePatch;
|
||||
const inverse = createDistributionBoardUpdateProjectCommand(
|
||||
current.id,
|
||||
inversePatch
|
||||
inversePatch,
|
||||
input.command.schemaVersion
|
||||
);
|
||||
const updated = database
|
||||
.update(distributionBoards)
|
||||
|
|
@ -352,6 +363,77 @@ export class DistributionBoardStructureProjectCommandRepository
|
|||
}
|
||||
return inverse;
|
||||
}
|
||||
|
||||
private assertFeedingReference(
|
||||
database: AppDatabase,
|
||||
projectId: string,
|
||||
distributionBoardId: string,
|
||||
reference: {
|
||||
feedingDistributionBoardId: string | null;
|
||||
feedingCircuitId: string | null;
|
||||
}
|
||||
) {
|
||||
const { feedingDistributionBoardId, feedingCircuitId } = reference;
|
||||
if (feedingDistributionBoardId === null) {
|
||||
if (feedingCircuitId !== null) {
|
||||
throw new Error(
|
||||
"Ein speisender Stromkreis erfordert eine speisende Verteilung."
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (feedingDistributionBoardId === distributionBoardId) {
|
||||
throw new Error("Eine Verteilung kann sich nicht selbst speisen.");
|
||||
}
|
||||
const projectBoards = database
|
||||
.select({
|
||||
id: distributionBoards.id,
|
||||
feedingDistributionBoardId:
|
||||
distributionBoards.feedingDistributionBoardId,
|
||||
})
|
||||
.from(distributionBoards)
|
||||
.where(eq(distributionBoards.projectId, projectId))
|
||||
.all();
|
||||
const boardById = new Map(projectBoards.map((board) => [board.id, board]));
|
||||
if (!boardById.has(feedingDistributionBoardId)) {
|
||||
throw new Error(
|
||||
"Die speisende Verteilung gehört nicht zum Projekt."
|
||||
);
|
||||
}
|
||||
const visited = new Set<string>([distributionBoardId]);
|
||||
let currentId: string | null = feedingDistributionBoardId;
|
||||
while (currentId !== null) {
|
||||
if (visited.has(currentId)) {
|
||||
throw new Error("Die Einspeisungsverknüpfung würde einen Zyklus erzeugen.");
|
||||
}
|
||||
visited.add(currentId);
|
||||
currentId = boardById.get(currentId)?.feedingDistributionBoardId ?? null;
|
||||
}
|
||||
if (feedingCircuitId === null) {
|
||||
return;
|
||||
}
|
||||
const feedingCircuit = database
|
||||
.select({
|
||||
projectId: circuitLists.projectId,
|
||||
distributionBoardId: circuitLists.distributionBoardId,
|
||||
})
|
||||
.from(circuits)
|
||||
.innerJoin(
|
||||
circuitLists,
|
||||
eq(circuitLists.id, circuits.circuitListId)
|
||||
)
|
||||
.where(eq(circuits.id, feedingCircuitId))
|
||||
.get();
|
||||
if (
|
||||
!feedingCircuit ||
|
||||
feedingCircuit.projectId !== projectId ||
|
||||
feedingCircuit.distributionBoardId !== feedingDistributionBoardId
|
||||
) {
|
||||
throw new Error(
|
||||
"Der speisende Stromkreis gehört nicht zur gewählten Verteilung."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function assertSupplyTypeEnabled(
|
||||
|
|
@ -392,11 +474,20 @@ function structureMatches(
|
|||
location,
|
||||
fedFrom,
|
||||
remark,
|
||||
feedingDistributionBoardId,
|
||||
feedingCircuitId,
|
||||
...actualDistributionBoard
|
||||
} = actual.board;
|
||||
if (
|
||||
simultaneityFactor !== 1 ||
|
||||
[boardNumber, location, fedFrom, remark].some((value) => value !== null) ||
|
||||
[
|
||||
boardNumber,
|
||||
location,
|
||||
fedFrom,
|
||||
remark,
|
||||
feedingDistributionBoardId,
|
||||
feedingCircuitId,
|
||||
].some((value) => value !== null) ||
|
||||
!sameRecord(
|
||||
expected.distributionBoard,
|
||||
actualDistributionBoard
|
||||
|
|
|
|||
|
|
@ -300,6 +300,69 @@ export class DistributionBoardSubtreeProjectCommandRepository
|
|||
);
|
||||
}
|
||||
}
|
||||
const feedingDistributionBoardId =
|
||||
snapshot.distributionBoard.feedingDistributionBoardId;
|
||||
const feedingCircuitId = snapshot.distributionBoard.feedingCircuitId;
|
||||
if (feedingDistributionBoardId === null) {
|
||||
if (feedingCircuitId !== null) {
|
||||
throw new Error(
|
||||
"Ein speisender Stromkreis erfordert eine speisende Verteilung."
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (feedingDistributionBoardId === snapshot.distributionBoard.id) {
|
||||
throw new Error("Eine Verteilung kann sich nicht selbst speisen.");
|
||||
}
|
||||
const projectBoards = database
|
||||
.select({
|
||||
id: distributionBoards.id,
|
||||
feedingDistributionBoardId:
|
||||
distributionBoards.feedingDistributionBoardId,
|
||||
})
|
||||
.from(distributionBoards)
|
||||
.where(eq(distributionBoards.projectId, projectId))
|
||||
.all();
|
||||
const boardById = new Map(projectBoards.map((board) => [board.id, board]));
|
||||
if (!boardById.has(feedingDistributionBoardId)) {
|
||||
throw new Error(
|
||||
"Die speisende Verteilung gehört nicht zum Projekt."
|
||||
);
|
||||
}
|
||||
const visited = new Set<string>([snapshot.distributionBoard.id]);
|
||||
let currentId: string | null = feedingDistributionBoardId;
|
||||
while (currentId !== null) {
|
||||
if (visited.has(currentId)) {
|
||||
throw new Error(
|
||||
"Die Einspeisungsverknüpfung würde einen Zyklus erzeugen."
|
||||
);
|
||||
}
|
||||
visited.add(currentId);
|
||||
currentId = boardById.get(currentId)?.feedingDistributionBoardId ?? null;
|
||||
}
|
||||
if (feedingCircuitId !== null) {
|
||||
const feedingCircuit = database
|
||||
.select({
|
||||
projectId: circuitLists.projectId,
|
||||
distributionBoardId: circuitLists.distributionBoardId,
|
||||
})
|
||||
.from(circuits)
|
||||
.innerJoin(
|
||||
circuitLists,
|
||||
eq(circuitLists.id, circuits.circuitListId)
|
||||
)
|
||||
.where(eq(circuits.id, feedingCircuitId))
|
||||
.get();
|
||||
if (
|
||||
!feedingCircuit ||
|
||||
feedingCircuit.projectId !== projectId ||
|
||||
feedingCircuit.distributionBoardId !== feedingDistributionBoardId
|
||||
) {
|
||||
throw new Error(
|
||||
"Der speisende Stromkreis gehört nicht zur gewählten Verteilung."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
const linkedProjectDeviceIds = uniqueStrings(
|
||||
snapshot.circuits.flatMap((circuit) =>
|
||||
circuit.deviceRows.flatMap((row) =>
|
||||
|
|
|
|||
|
|
@ -270,6 +270,14 @@ function replaceProjectState(
|
|||
database: AppDatabase,
|
||||
state: ProjectStateSnapshot
|
||||
) {
|
||||
database
|
||||
.update(distributionBoards)
|
||||
.set({
|
||||
feedingDistributionBoardId: null,
|
||||
feedingCircuitId: null,
|
||||
})
|
||||
.where(eq(distributionBoards.projectId, state.project.id))
|
||||
.run();
|
||||
database
|
||||
.delete(externalModelSources)
|
||||
.where(eq(externalModelSources.projectId, state.project.id))
|
||||
|
|
@ -330,7 +338,15 @@ function replaceProjectState(
|
|||
}
|
||||
insertMany(database, rooms, state.rooms);
|
||||
insertMany(database, projectDevices, state.projectDevices);
|
||||
insertMany(database, distributionBoards, state.distributionBoards);
|
||||
insertMany(
|
||||
database,
|
||||
distributionBoards,
|
||||
state.distributionBoards.map((board) => ({
|
||||
...board,
|
||||
feedingDistributionBoardId: null,
|
||||
feedingCircuitId: null,
|
||||
}))
|
||||
);
|
||||
insertMany(database, circuitLists, state.circuitLists);
|
||||
insertMany(database, circuitSections, state.circuitSections);
|
||||
insertMany(
|
||||
|
|
@ -341,6 +357,7 @@ function replaceProjectState(
|
|||
isReserve: circuit.isReserve ? 1 : 0,
|
||||
}))
|
||||
);
|
||||
restoreDistributionBoardFeedingReferences(database, state);
|
||||
insertMany(
|
||||
database,
|
||||
distributionBoardComponents,
|
||||
|
|
@ -378,6 +395,31 @@ function replaceProjectState(
|
|||
insertMany(database, externalModelObjects, state.externalModel.objects);
|
||||
}
|
||||
|
||||
function restoreDistributionBoardFeedingReferences(
|
||||
database: AppDatabase,
|
||||
state: ProjectStateSnapshot
|
||||
) {
|
||||
for (const board of state.distributionBoards) {
|
||||
if (
|
||||
board.feedingDistributionBoardId === null &&
|
||||
board.feedingCircuitId === null
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const updated = database
|
||||
.update(distributionBoards)
|
||||
.set({
|
||||
feedingDistributionBoardId: board.feedingDistributionBoardId,
|
||||
feedingCircuitId: board.feedingCircuitId,
|
||||
})
|
||||
.where(eq(distributionBoards.id, board.id))
|
||||
.run();
|
||||
if (updated.changes !== 1) {
|
||||
throw new Error("Distribution-board feeding reference could not be restored.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function insertMany<TTable extends Parameters<AppDatabase["insert"]>[0]>(
|
||||
database: AppDatabase,
|
||||
table: TTable,
|
||||
|
|
|
|||
|
|
@ -173,7 +173,15 @@ function insertProjectState(
|
|||
insertMany(database, floors, state.floors);
|
||||
insertMany(database, rooms, state.rooms);
|
||||
insertMany(database, projectDevices, state.projectDevices);
|
||||
insertMany(database, distributionBoards, state.distributionBoards);
|
||||
insertMany(
|
||||
database,
|
||||
distributionBoards,
|
||||
state.distributionBoards.map((board) => ({
|
||||
...board,
|
||||
feedingDistributionBoardId: null,
|
||||
feedingCircuitId: null,
|
||||
}))
|
||||
);
|
||||
insertMany(database, circuitLists, state.circuitLists);
|
||||
insertMany(database, circuitSections, state.circuitSections);
|
||||
insertMany(
|
||||
|
|
@ -184,6 +192,25 @@ function insertProjectState(
|
|||
isReserve: circuit.isReserve ? 1 : 0,
|
||||
}))
|
||||
);
|
||||
for (const board of state.distributionBoards) {
|
||||
if (
|
||||
board.feedingDistributionBoardId === null &&
|
||||
board.feedingCircuitId === null
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const updated = database
|
||||
.update(distributionBoards)
|
||||
.set({
|
||||
feedingDistributionBoardId: board.feedingDistributionBoardId,
|
||||
feedingCircuitId: board.feedingCircuitId,
|
||||
})
|
||||
.where(eq(distributionBoards.id, board.id))
|
||||
.run();
|
||||
if (updated.changes !== 1) {
|
||||
throw new Error("Distribution-board feeding reference could not be imported.");
|
||||
}
|
||||
}
|
||||
insertMany(
|
||||
database,
|
||||
distributionBoardComponents,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { sqliteTable, text, unique } from "drizzle-orm/sqlite-core";
|
||||
import { sqliteTable, text, unique, type AnySQLiteColumn } from "drizzle-orm/sqlite-core";
|
||||
import { distributionBoards } from "./distribution-boards.js";
|
||||
import { projects } from "./projects.js";
|
||||
|
||||
|
|
@ -11,7 +11,7 @@ export const circuitLists = sqliteTable(
|
|||
.references(() => projects.id, { onDelete: "cascade" }),
|
||||
distributionBoardId: text("distribution_board_id")
|
||||
.notNull()
|
||||
.references(() => distributionBoards.id, { onDelete: "cascade" }),
|
||||
.references((): AnySQLiteColumn => distributionBoards.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
},
|
||||
(table) => [unique("circuit_lists_distribution_board_id_unique").on(table.distributionBoardId)]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { index, integer, real, sqliteTable, text, unique } from "drizzle-orm/sqlite-core";
|
||||
import { index, integer, real, sqliteTable, text, unique, type AnySQLiteColumn } from "drizzle-orm/sqlite-core";
|
||||
import { circuitLists } from "./circuit-lists.js";
|
||||
import { circuitSections } from "./circuit-sections.js";
|
||||
|
||||
|
|
@ -8,7 +8,7 @@ export const circuits = sqliteTable(
|
|||
id: text("id").primaryKey(),
|
||||
circuitListId: text("circuit_list_id")
|
||||
.notNull()
|
||||
.references(() => circuitLists.id, { onDelete: "cascade" }),
|
||||
.references((): AnySQLiteColumn => circuitLists.id, { onDelete: "cascade" }),
|
||||
sectionId: text("section_id")
|
||||
.notNull()
|
||||
.references(() => circuitSections.id, { onDelete: "cascade" }),
|
||||
|
|
|
|||
|
|
@ -1,24 +1,44 @@
|
|||
import { real, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
import { index, real, sqliteTable, text, type AnySQLiteColumn } from "drizzle-orm/sqlite-core";
|
||||
import { circuits } from "./circuits.js";
|
||||
import { floors } from "./floors.js";
|
||||
import { projects } from "./projects.js";
|
||||
import type { DistributionBoardSupplyType } from "../../shared/constants/distribution-board.js";
|
||||
|
||||
export const distributionBoards = sqliteTable("distribution_boards", {
|
||||
id: text("id").primaryKey(),
|
||||
projectId: text("project_id")
|
||||
.notNull()
|
||||
.references(() => projects.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
floorId: text("floor_id").references(() => floors.id, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
supplyType: text("supply_type").$type<DistributionBoardSupplyType>(),
|
||||
simultaneityFactor: real("simultaneity_factor")
|
||||
.notNull()
|
||||
.default(1),
|
||||
boardNumber: text("board_number"),
|
||||
location: text("location"),
|
||||
fedFrom: text("fed_from"),
|
||||
remark: text("remark"),
|
||||
});
|
||||
export const distributionBoards = sqliteTable(
|
||||
"distribution_boards",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
projectId: text("project_id")
|
||||
.notNull()
|
||||
.references(() => projects.id, { onDelete: "cascade" }),
|
||||
name: text("name").notNull(),
|
||||
floorId: text("floor_id").references(() => floors.id, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
supplyType: text("supply_type").$type<DistributionBoardSupplyType>(),
|
||||
simultaneityFactor: real("simultaneity_factor")
|
||||
.notNull()
|
||||
.default(1),
|
||||
boardNumber: text("board_number"),
|
||||
location: text("location"),
|
||||
fedFrom: text("fed_from"),
|
||||
remark: text("remark"),
|
||||
feedingDistributionBoardId: text("feeding_distribution_board_id").references(
|
||||
(): AnySQLiteColumn => distributionBoards.id,
|
||||
{ onDelete: "restrict" }
|
||||
),
|
||||
feedingCircuitId: text("feeding_circuit_id").references(
|
||||
(): AnySQLiteColumn => circuits.id,
|
||||
{ onDelete: "restrict" }
|
||||
),
|
||||
},
|
||||
(table) => [
|
||||
index("distribution_boards_feeding_board_idx").on(
|
||||
table.feedingDistributionBoardId
|
||||
),
|
||||
index("distribution_boards_feeding_circuit_idx").on(
|
||||
table.feedingCircuitId
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -9,16 +9,18 @@ import type { SerializedProjectCommand } from "./project-command.model.js";
|
|||
|
||||
export const distributionBoardUpdateCommandType =
|
||||
"distribution-board.update" as const;
|
||||
// The info-card fields were added additively to schema version 1: every
|
||||
// previously stored command remains valid, and the inverse is still derived
|
||||
// from the persisted row for exactly the changed fields.
|
||||
export const distributionBoardUpdateCommandSchemaVersion = 1 as const;
|
||||
// Version 2 adds logical feeding references. Version-1 commands remain
|
||||
// executable and can only change the fields they originally knew.
|
||||
export const distributionBoardUpdateCommandSchemaVersion = 2 as const;
|
||||
const legacyDistributionBoardUpdateCommandSchemaVersion = 1 as const;
|
||||
|
||||
export interface DistributionBoardUpdateValues
|
||||
extends Record<DistributionBoardInfoField, string | null> {
|
||||
floorId: string | null;
|
||||
supplyType: DistributionBoardSupplyType | null;
|
||||
simultaneityFactor: number;
|
||||
feedingDistributionBoardId: string | null;
|
||||
feedingCircuitId: string | null;
|
||||
}
|
||||
|
||||
export type DistributionBoardUpdateField =
|
||||
|
|
@ -41,16 +43,20 @@ export interface DistributionBoardUpdateCommandPayload {
|
|||
|
||||
export interface DistributionBoardUpdateProjectCommand
|
||||
extends SerializedProjectCommand<DistributionBoardUpdateCommandPayload> {
|
||||
schemaVersion: typeof distributionBoardUpdateCommandSchemaVersion;
|
||||
schemaVersion:
|
||||
| typeof legacyDistributionBoardUpdateCommandSchemaVersion
|
||||
| typeof distributionBoardUpdateCommandSchemaVersion;
|
||||
type: typeof distributionBoardUpdateCommandType;
|
||||
}
|
||||
|
||||
export function createDistributionBoardUpdateProjectCommand(
|
||||
distributionBoardId: string,
|
||||
patch: DistributionBoardUpdatePatch
|
||||
patch: DistributionBoardUpdatePatch,
|
||||
schemaVersion: DistributionBoardUpdateProjectCommand["schemaVersion"] =
|
||||
distributionBoardUpdateCommandSchemaVersion
|
||||
): DistributionBoardUpdateProjectCommand {
|
||||
const command: DistributionBoardUpdateProjectCommand = {
|
||||
schemaVersion: distributionBoardUpdateCommandSchemaVersion,
|
||||
schemaVersion,
|
||||
type: distributionBoardUpdateCommandType,
|
||||
payload: {
|
||||
distributionBoardId,
|
||||
|
|
@ -68,7 +74,8 @@ export function assertDistributionBoardUpdateProjectCommand(
|
|||
command: SerializedProjectCommand<unknown>
|
||||
): asserts command is DistributionBoardUpdateProjectCommand {
|
||||
if (
|
||||
command.schemaVersion !== distributionBoardUpdateCommandSchemaVersion ||
|
||||
(command.schemaVersion !== legacyDistributionBoardUpdateCommandSchemaVersion &&
|
||||
command.schemaVersion !== distributionBoardUpdateCommandSchemaVersion) ||
|
||||
command.type !== distributionBoardUpdateCommandType ||
|
||||
!isPlainObject(command.payload)
|
||||
) {
|
||||
|
|
@ -93,7 +100,12 @@ export function assertDistributionBoardUpdateProjectCommand(
|
|||
(change.field !== "floorId" &&
|
||||
change.field !== "supplyType" &&
|
||||
change.field !== "simultaneityFactor" &&
|
||||
change.field !== "feedingDistributionBoardId" &&
|
||||
change.field !== "feedingCircuitId" &&
|
||||
!isInfoField(change.field)) ||
|
||||
(command.schemaVersion === legacyDistributionBoardUpdateCommandSchemaVersion &&
|
||||
(change.field === "feedingDistributionBoardId" ||
|
||||
change.field === "feedingCircuitId")) ||
|
||||
seen.has(change.field)
|
||||
) {
|
||||
throw new Error(
|
||||
|
|
@ -105,7 +117,9 @@ export function assertDistributionBoardUpdateProjectCommand(
|
|||
} else if (change.field === "floorId") {
|
||||
if (
|
||||
change.value !== null &&
|
||||
(typeof change.value !== "string" || !change.value.trim())
|
||||
(typeof change.value !== "string" ||
|
||||
!change.value.trim() ||
|
||||
change.value !== change.value.trim())
|
||||
) {
|
||||
throw new Error("floorId must be a non-empty string or null.");
|
||||
}
|
||||
|
|
@ -118,6 +132,16 @@ export function assertDistributionBoardUpdateProjectCommand(
|
|||
) {
|
||||
throw new Error("supplyType is invalid.");
|
||||
}
|
||||
} else if (
|
||||
change.field === "feedingDistributionBoardId" ||
|
||||
change.field === "feedingCircuitId"
|
||||
) {
|
||||
if (
|
||||
change.value !== null &&
|
||||
(typeof change.value !== "string" || !change.value.trim())
|
||||
) {
|
||||
throw new Error(`${change.field} must be a non-empty string or null.`);
|
||||
}
|
||||
} else if (
|
||||
typeof change.value !== "number" ||
|
||||
!Number.isFinite(change.value) ||
|
||||
|
|
|
|||
|
|
@ -74,6 +74,8 @@ export interface DistributionBoardSubtreeSnapshot {
|
|||
floorId: string | null;
|
||||
supplyType: DistributionBoardSupplyType | null;
|
||||
simultaneityFactor: number;
|
||||
feedingDistributionBoardId: string | null;
|
||||
feedingCircuitId: string | null;
|
||||
} & Record<DistributionBoardInfoField, string | null>;
|
||||
circuitList: {
|
||||
id: string;
|
||||
|
|
@ -184,6 +186,9 @@ export function normalizeDistributionBoardSubtreeSnapshot(
|
|||
for (const field of distributionBoardInfoFields) {
|
||||
board[field] = board[field] ?? null;
|
||||
}
|
||||
board.feedingDistributionBoardId =
|
||||
board.feedingDistributionBoardId ?? null;
|
||||
board.feedingCircuitId = board.feedingCircuitId ?? null;
|
||||
return { ...snapshot, distributionBoard: board };
|
||||
}
|
||||
|
||||
|
|
@ -227,6 +232,18 @@ export function assertDistributionBoardSubtreeSnapshot(
|
|||
assertNullableString(board[field], `distributionBoard.${field}`);
|
||||
}
|
||||
}
|
||||
if (board.feedingDistributionBoardId !== undefined) {
|
||||
assertNullableString(
|
||||
board.feedingDistributionBoardId,
|
||||
"distributionBoard.feedingDistributionBoardId"
|
||||
);
|
||||
}
|
||||
if (board.feedingCircuitId !== undefined) {
|
||||
assertNullableString(
|
||||
board.feedingCircuitId,
|
||||
"distributionBoard.feedingCircuitId"
|
||||
);
|
||||
}
|
||||
for (const field of [
|
||||
"id",
|
||||
"projectId",
|
||||
|
|
|
|||
|
|
@ -35,7 +35,8 @@ import {
|
|||
type ExternalModelStateSnapshot,
|
||||
} from "../../external-model/domain/external-model-contracts.js";
|
||||
|
||||
export const projectStateSnapshotSchemaVersion = 8 as const;
|
||||
export const projectStateSnapshotSchemaVersion = 9 as const;
|
||||
const roomAreaProjectStateSnapshotSchemaVersion = 8 as const;
|
||||
const buildingProjectStateSnapshotSchemaVersion = 7 as const;
|
||||
const coverProjectStateSnapshotSchemaVersion = 6 as const;
|
||||
const boardInfoProjectStateSnapshotSchemaVersion = 5 as const;
|
||||
|
|
@ -112,7 +113,7 @@ const distributionBoardInfoValueSchema = (field: DistributionBoardInfoField) =>
|
|||
.max(distributionBoardInfoFieldMaxLengths[field])
|
||||
.nullable();
|
||||
|
||||
const distributionBoardSchema = legacyDistributionBoardSchema
|
||||
const distributionBoardWithoutFeedingSchema = legacyDistributionBoardSchema
|
||||
.extend({
|
||||
boardNumber: distributionBoardInfoValueSchema("boardNumber"),
|
||||
location: distributionBoardInfoValueSchema("location"),
|
||||
|
|
@ -121,6 +122,13 @@ const distributionBoardSchema = legacyDistributionBoardSchema
|
|||
})
|
||||
.strict();
|
||||
|
||||
const distributionBoardSchema = distributionBoardWithoutFeedingSchema
|
||||
.extend({
|
||||
feedingDistributionBoardId: idSchema.nullable(),
|
||||
feedingCircuitId: idSchema.nullable(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
function withEmptyDistributionBoardInfo(
|
||||
boards: Array<z.infer<typeof legacyDistributionBoardSchema>>
|
||||
) {
|
||||
|
|
@ -133,6 +141,16 @@ function withEmptyDistributionBoardInfo(
|
|||
}));
|
||||
}
|
||||
|
||||
function withEmptyDistributionBoardFeeding(
|
||||
boards: Array<z.infer<typeof distributionBoardWithoutFeedingSchema>>
|
||||
) {
|
||||
return boards.map((board) => ({
|
||||
...board,
|
||||
feedingDistributionBoardId: null,
|
||||
feedingCircuitId: null,
|
||||
}));
|
||||
}
|
||||
|
||||
const circuitListSchema = z
|
||||
.object({
|
||||
id: idSchema,
|
||||
|
|
@ -553,11 +571,22 @@ export const projectStateSnapshotSchema = z
|
|||
})
|
||||
.strict();
|
||||
|
||||
const roomAreaProjectStateSnapshotSchema = z
|
||||
.object({
|
||||
schemaVersion: z.literal(roomAreaProjectStateSnapshotSchemaVersion),
|
||||
project: projectSchema,
|
||||
distributionBoards: z.array(distributionBoardWithoutFeedingSchema),
|
||||
externalCsvConfiguration: externalCsvConfigurationSnapshotSchema.nullable(),
|
||||
externalModel: externalModelStateSchema,
|
||||
...projectStateSnapshotContents,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const buildingProjectStateSnapshotSchema = z
|
||||
.object({
|
||||
schemaVersion: z.literal(buildingProjectStateSnapshotSchemaVersion),
|
||||
project: projectSchema,
|
||||
distributionBoards: z.array(distributionBoardSchema),
|
||||
distributionBoards: z.array(distributionBoardWithoutFeedingSchema),
|
||||
externalCsvConfiguration: externalCsvConfigurationSnapshotSchema.nullable(),
|
||||
externalModel: externalModelStateSchema,
|
||||
...buildingProjectStateSnapshotContents,
|
||||
|
|
@ -568,7 +597,7 @@ const coverProjectStateSnapshotSchema = z
|
|||
.object({
|
||||
schemaVersion: z.literal(coverProjectStateSnapshotSchemaVersion),
|
||||
project: projectSchema,
|
||||
distributionBoards: z.array(distributionBoardSchema),
|
||||
distributionBoards: z.array(distributionBoardWithoutFeedingSchema),
|
||||
externalCsvConfiguration: externalCsvConfigurationSnapshotSchema.nullable(),
|
||||
externalModel: externalModelStateSchema,
|
||||
...previousProjectStateSnapshotContents,
|
||||
|
|
@ -655,6 +684,27 @@ export function parseProjectStateSnapshot(
|
|||
}
|
||||
|
||||
function upgradePreviousProjectStateSnapshot(value: unknown): unknown {
|
||||
const roomArea = upgradeToRoomAreaProjectStateSnapshot(value);
|
||||
if (
|
||||
roomArea !== null &&
|
||||
typeof roomArea === "object" &&
|
||||
!Array.isArray(roomArea) &&
|
||||
(roomArea as { schemaVersion?: unknown }).schemaVersion ===
|
||||
roomAreaProjectStateSnapshotSchemaVersion
|
||||
) {
|
||||
const previous = roomAreaProjectStateSnapshotSchema.parse(roomArea);
|
||||
return {
|
||||
...previous,
|
||||
schemaVersion: projectStateSnapshotSchemaVersion,
|
||||
distributionBoards: withEmptyDistributionBoardFeeding(
|
||||
previous.distributionBoards
|
||||
),
|
||||
};
|
||||
}
|
||||
return roomArea;
|
||||
}
|
||||
|
||||
function upgradeToRoomAreaProjectStateSnapshot(value: unknown): unknown {
|
||||
const cover = upgradeToCoverProjectStateSnapshot(value);
|
||||
if (
|
||||
cover !== null &&
|
||||
|
|
@ -666,7 +716,7 @@ function upgradePreviousProjectStateSnapshot(value: unknown): unknown {
|
|||
const previous = buildingProjectStateSnapshotSchema.parse(cover);
|
||||
return {
|
||||
...previous,
|
||||
schemaVersion: projectStateSnapshotSchemaVersion,
|
||||
schemaVersion: roomAreaProjectStateSnapshotSchemaVersion,
|
||||
rooms: previous.rooms.map((room) => ({ ...room, areaM2: null })),
|
||||
};
|
||||
}
|
||||
|
|
@ -683,7 +733,7 @@ function upgradePreviousProjectStateSnapshot(value: unknown): unknown {
|
|||
// building. Build version 7 first so the room-area upgrade above remains a
|
||||
// single exact transition.
|
||||
const previous = coverProjectStateSnapshotSchema.parse(cover);
|
||||
return upgradePreviousProjectStateSnapshot({
|
||||
return upgradeToRoomAreaProjectStateSnapshot({
|
||||
...previous,
|
||||
schemaVersion: buildingProjectStateSnapshotSchemaVersion,
|
||||
buildings: [],
|
||||
|
|
@ -873,6 +923,15 @@ function assertProjectStateSnapshotRelations(
|
|||
const sectionById = new Map(
|
||||
snapshot.circuitSections.map((entry) => [entry.id, entry])
|
||||
);
|
||||
const boardById = new Map(
|
||||
snapshot.distributionBoards.map((entry) => [entry.id, entry])
|
||||
);
|
||||
const circuitListById = new Map(
|
||||
snapshot.circuitLists.map((entry) => [entry.id, entry])
|
||||
);
|
||||
const circuitById = new Map(
|
||||
snapshot.circuits.map((entry) => [entry.id, entry])
|
||||
);
|
||||
const componentById = new Map(
|
||||
snapshot.distributionBoardComponents.map((entry) => [
|
||||
entry.id,
|
||||
|
|
@ -901,6 +960,47 @@ function assertProjectStateSnapshotRelations(
|
|||
"Snapshot distribution board uses a disabled supply type."
|
||||
);
|
||||
}
|
||||
if (board.feedingDistributionBoardId === board.id) {
|
||||
throw new Error("Snapshot distribution board cannot feed itself.");
|
||||
}
|
||||
if (board.feedingDistributionBoardId !== null) {
|
||||
assertReference(
|
||||
boardIds,
|
||||
board.feedingDistributionBoardId,
|
||||
"feeding distribution board"
|
||||
);
|
||||
}
|
||||
if (board.feedingCircuitId !== null) {
|
||||
if (board.feedingDistributionBoardId === null) {
|
||||
throw new Error(
|
||||
"Snapshot feeding circuit requires a feeding distribution board."
|
||||
);
|
||||
}
|
||||
assertReference(circuitIds, board.feedingCircuitId, "feeding circuit");
|
||||
const feedingCircuit = circuitById.get(board.feedingCircuitId);
|
||||
const feedingList = feedingCircuit
|
||||
? circuitListById.get(feedingCircuit.circuitListId)
|
||||
: undefined;
|
||||
if (
|
||||
!feedingList ||
|
||||
feedingList.distributionBoardId !== board.feedingDistributionBoardId
|
||||
) {
|
||||
throw new Error(
|
||||
"Snapshot feeding circuit belongs to another distribution board."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const board of snapshot.distributionBoards) {
|
||||
const visited = new Set<string>([board.id]);
|
||||
let currentId = board.feedingDistributionBoardId;
|
||||
while (currentId !== null) {
|
||||
if (visited.has(currentId)) {
|
||||
throw new Error("Snapshot distribution-board feeding graph contains a cycle.");
|
||||
}
|
||||
visited.add(currentId);
|
||||
currentId = boardById.get(currentId)?.feedingDistributionBoardId ?? null;
|
||||
}
|
||||
}
|
||||
for (const list of snapshot.circuitLists) {
|
||||
assertProjectOwnership(list.projectId, projectId, "circuit list");
|
||||
|
|
|
|||
|
|
@ -223,6 +223,14 @@ export function remapProjectState(
|
|||
...board,
|
||||
id: requiredId(boardIds, board.id),
|
||||
projectId: targetProjectId,
|
||||
feedingDistributionBoardId:
|
||||
board.feedingDistributionBoardId === null
|
||||
? null
|
||||
: requiredId(boardIds, board.feedingDistributionBoardId),
|
||||
feedingCircuitId:
|
||||
board.feedingCircuitId === null
|
||||
? null
|
||||
: requiredId(circuitIds, board.feedingCircuitId),
|
||||
floorId:
|
||||
board.floorId === null
|
||||
? null
|
||||
|
|
|
|||
|
|
@ -195,6 +195,8 @@ export interface DistributionBoardDto extends DistributionBoardInfoDto {
|
|||
floorId: string | null;
|
||||
supplyType: DistributionBoardSupplyType | null;
|
||||
simultaneityFactor: number;
|
||||
feedingDistributionBoardId: string | null;
|
||||
feedingCircuitId: string | null;
|
||||
}
|
||||
|
||||
export interface DocumentLogoMetadataDto {
|
||||
|
|
|
|||
|
|
@ -372,7 +372,13 @@ export function updateDistributionBoard(
|
|||
floorId: string | null;
|
||||
supplyType: NonNullable<DistributionBoardDto["supplyType"]>;
|
||||
simultaneityFactor: number;
|
||||
} & Partial<DistributionBoardInfoDto>,
|
||||
} & Partial<DistributionBoardInfoDto> &
|
||||
Partial<
|
||||
Pick<
|
||||
DistributionBoardDto,
|
||||
"feedingDistributionBoardId" | "feedingCircuitId"
|
||||
>
|
||||
>,
|
||||
expectedRevision: number
|
||||
) {
|
||||
return request<DistributionBoardCommandResultDto>(
|
||||
|
|
|
|||
|
|
@ -69,6 +69,8 @@ export async function createDistributionBoard(req: Request, res: Response) {
|
|||
location: null,
|
||||
fedFrom: null,
|
||||
remark: null,
|
||||
feedingDistributionBoardId: null,
|
||||
feedingCircuitId: null,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
|
@ -98,6 +100,17 @@ export async function updateDistributionBoard(
|
|||
.filter((field) => parsed.data[field] !== undefined)
|
||||
.map((field) => [field, parsed.data[field]])
|
||||
);
|
||||
const feedingPatch = {
|
||||
...(parsed.data.feedingDistributionBoardId === undefined
|
||||
? {}
|
||||
: {
|
||||
feedingDistributionBoardId:
|
||||
parsed.data.feedingDistributionBoardId,
|
||||
}),
|
||||
...(parsed.data.feedingCircuitId === undefined
|
||||
? {}
|
||||
: { feedingCircuitId: parsed.data.feedingCircuitId }),
|
||||
};
|
||||
try {
|
||||
const result = projectCommandService.executeUser({
|
||||
projectId,
|
||||
|
|
@ -110,6 +123,7 @@ export async function updateDistributionBoard(
|
|||
supplyType: parsed.data.supplyType,
|
||||
simultaneityFactor: parsed.data.simultaneityFactor,
|
||||
...infoPatch,
|
||||
...feedingPatch,
|
||||
}
|
||||
),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -86,6 +86,8 @@ export const updateDistributionBoardSchema = z
|
|||
location: distributionBoardInfoValueSchema("location"),
|
||||
fedFrom: distributionBoardInfoValueSchema("fedFrom"),
|
||||
remark: distributionBoardInfoValueSchema("remark"),
|
||||
feedingDistributionBoardId: z.string().trim().min(1).nullable().optional(),
|
||||
feedingCircuitId: z.string().trim().min(1).nullable().optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue