Add section renumber history

This commit is contained in:
2026-07-24 08:34:31 +02:00
parent 332dfdb5d9
commit 4b4603b71b
14 changed files with 1057 additions and 14 deletions
@@ -0,0 +1,130 @@
import type { SerializedProjectCommand } from "./project-command.model.js";
export const circuitSectionRenumberCommandType =
"circuit.renumber-section" as const;
export const circuitSectionRenumberCommandSchemaVersion = 1 as const;
export interface CircuitSectionRenumberAssignment {
circuitId: string;
expectedEquipmentIdentifier: string;
targetEquipmentIdentifier: string;
}
export interface CircuitSectionRenumberCommandPayload {
sectionId: string;
assignments: CircuitSectionRenumberAssignment[];
}
export interface CircuitSectionRenumberProjectCommand
extends SerializedProjectCommand<CircuitSectionRenumberCommandPayload> {
schemaVersion: typeof circuitSectionRenumberCommandSchemaVersion;
type: typeof circuitSectionRenumberCommandType;
}
export function createCircuitSectionRenumberProjectCommand(
sectionId: string,
assignments: CircuitSectionRenumberAssignment[]
): CircuitSectionRenumberProjectCommand {
const command: CircuitSectionRenumberProjectCommand = {
schemaVersion: circuitSectionRenumberCommandSchemaVersion,
type: circuitSectionRenumberCommandType,
payload: { sectionId, assignments },
};
assertCircuitSectionRenumberProjectCommand(command);
return command;
}
export function assertCircuitSectionRenumberProjectCommand(
command: SerializedProjectCommand<unknown>
): asserts command is CircuitSectionRenumberProjectCommand {
if (
command.schemaVersion !==
circuitSectionRenumberCommandSchemaVersion ||
command.type !== circuitSectionRenumberCommandType
) {
throw new Error("Unsupported circuit section renumber command.");
}
if (!isPlainObject(command.payload)) {
throw new Error(
"Circuit section renumber payload must be an object."
);
}
const { sectionId, assignments } = command.payload;
assertNonEmptyString(sectionId, "sectionId");
if (!Array.isArray(assignments) || assignments.length === 0) {
throw new Error(
"Circuit section renumber command requires assignments."
);
}
const circuitIds = new Set<string>();
const expectedIdentifiers = new Set<string>();
const targetIdentifiers = new Set<string>();
let hasChangedIdentifier = false;
for (const assignment of assignments) {
if (!isPlainObject(assignment)) {
throw new Error(
"Circuit section renumber command contains an invalid assignment."
);
}
assertNonEmptyString(assignment.circuitId, "circuitId");
assertNonEmptyString(
assignment.expectedEquipmentIdentifier,
"expectedEquipmentIdentifier"
);
assertNonEmptyString(
assignment.targetEquipmentIdentifier,
"targetEquipmentIdentifier"
);
if (circuitIds.has(assignment.circuitId)) {
throw new Error(
"Circuit section renumber command contains duplicate circuit ids."
);
}
if (
expectedIdentifiers.has(
assignment.expectedEquipmentIdentifier
)
) {
throw new Error(
"Circuit section renumber command contains duplicate expected identifiers."
);
}
if (
targetIdentifiers.has(assignment.targetEquipmentIdentifier)
) {
throw new Error(
"Circuit section renumber command contains duplicate target identifiers."
);
}
if (
assignment.expectedEquipmentIdentifier !==
assignment.targetEquipmentIdentifier
) {
hasChangedIdentifier = true;
}
circuitIds.add(assignment.circuitId);
expectedIdentifiers.add(
assignment.expectedEquipmentIdentifier
);
targetIdentifiers.add(assignment.targetEquipmentIdentifier);
}
if (!hasChangedIdentifier) {
throw new Error(
"Circuit section renumber command must change at least one identifier."
);
}
}
function assertNonEmptyString(
value: unknown,
field: string
): asserts value is string {
if (typeof value !== "string" || !value.trim()) {
throw new Error(`${field} must be a non-empty string.`);
}
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
@@ -0,0 +1,26 @@
import type { CircuitSectionRenumberProjectCommand } from "../models/circuit-section-renumber-project-command.model.js";
import type {
AppendedProjectRevision,
ProjectRevisionSource,
} from "./project-revision.store.js";
export interface ExecuteCircuitSectionRenumberCommandInput {
projectId: string;
expectedRevision: number;
source: ProjectRevisionSource;
description?: string;
actorId?: string;
historyTargetChangeSetId?: string;
command: CircuitSectionRenumberProjectCommand;
}
export interface ExecutedCircuitSectionRenumberCommand {
revision: AppendedProjectRevision;
inverse: CircuitSectionRenumberProjectCommand;
}
export interface CircuitSectionRenumberProjectCommandStore {
execute(
input: ExecuteCircuitSectionRenumberCommandInput
): ExecutedCircuitSectionRenumberCommand;
}
@@ -23,6 +23,10 @@ import {
assertCircuitSectionReorderProjectCommand,
circuitSectionReorderCommandType,
} from "../models/circuit-section-reorder-project-command.model.js";
import {
assertCircuitSectionRenumberProjectCommand,
circuitSectionRenumberCommandType,
} from "../models/circuit-section-renumber-project-command.model.js";
import {
assertCircuitDeleteProjectCommand,
assertCircuitInsertProjectCommand,
@@ -38,6 +42,7 @@ import type { CircuitDeviceRowMoveProjectCommandStore } from "../ports/circuit-d
import type { CircuitDeviceRowStructureProjectCommandStore } from "../ports/circuit-device-row-structure-project-command.store.js";
import type { CircuitProjectCommandStore } from "../ports/circuit-project-command.store.js";
import type { CircuitSectionReorderProjectCommandStore } from "../ports/circuit-section-reorder-project-command.store.js";
import type { CircuitSectionRenumberProjectCommandStore } from "../ports/circuit-section-renumber-project-command.store.js";
import type { CircuitStructureProjectCommandStore } from "../ports/circuit-structure-project-command.store.js";
import type {
ExecuteProjectHistoryCommandInput,
@@ -69,6 +74,7 @@ export class ProjectCommandService implements ProjectCommandExecutor {
private readonly deviceRowMoveStore: CircuitDeviceRowMoveProjectCommandStore,
private readonly circuitStructureStore: CircuitStructureProjectCommandStore,
private readonly circuitSectionReorderStore: CircuitSectionReorderProjectCommandStore,
private readonly circuitSectionRenumberStore: CircuitSectionRenumberProjectCommandStore,
private readonly historyStore: ProjectHistoryStore
) {}
@@ -199,6 +205,13 @@ export class ProjectCommandService implements ProjectCommandExecutor {
command: input.command,
}).revision;
}
case circuitSectionRenumberCommandType: {
assertCircuitSectionRenumberProjectCommand(input.command);
return this.circuitSectionRenumberStore.execute({
...input,
command: input.command,
}).revision;
}
default:
throw new Error(
`Unsupported project command type: ${input.command.type}.`