Add circuit reorder history

This commit is contained in:
2026-07-24 08:21:32 +02:00
parent 53282e8c7c
commit 332dfdb5d9
14 changed files with 865 additions and 16 deletions
@@ -0,0 +1,115 @@
import type { SerializedProjectCommand } from "./project-command.model.js";
export const circuitSectionReorderCommandType =
"circuit.reorder-section" as const;
export const circuitSectionReorderCommandSchemaVersion = 1 as const;
export interface CircuitSectionReorderAssignment {
circuitId: string;
expectedSortOrder: number;
targetSortOrder: number;
}
export interface CircuitSectionReorderCommandPayload {
sectionId: string;
assignments: CircuitSectionReorderAssignment[];
}
export interface CircuitSectionReorderProjectCommand
extends SerializedProjectCommand<CircuitSectionReorderCommandPayload> {
schemaVersion: typeof circuitSectionReorderCommandSchemaVersion;
type: typeof circuitSectionReorderCommandType;
}
export function createCircuitSectionReorderProjectCommand(
sectionId: string,
assignments: CircuitSectionReorderAssignment[]
): CircuitSectionReorderProjectCommand {
const command: CircuitSectionReorderProjectCommand = {
schemaVersion: circuitSectionReorderCommandSchemaVersion,
type: circuitSectionReorderCommandType,
payload: { sectionId, assignments },
};
assertCircuitSectionReorderProjectCommand(command);
return command;
}
export function assertCircuitSectionReorderProjectCommand(
command: SerializedProjectCommand<unknown>
): asserts command is CircuitSectionReorderProjectCommand {
if (
command.schemaVersion !== circuitSectionReorderCommandSchemaVersion ||
command.type !== circuitSectionReorderCommandType
) {
throw new Error("Unsupported circuit section reorder command.");
}
if (!isPlainObject(command.payload)) {
throw new Error(
"Circuit section reorder 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 reorder command requires assignments."
);
}
const circuitIds = new Set<string>();
let hasChangedPosition = false;
for (const assignment of assignments) {
if (!isPlainObject(assignment)) {
throw new Error(
"Circuit section reorder command contains an invalid assignment."
);
}
assertNonEmptyString(assignment.circuitId, "circuitId");
assertFiniteNumber(
assignment.expectedSortOrder,
"expectedSortOrder"
);
assertFiniteNumber(
assignment.targetSortOrder,
"targetSortOrder"
);
if (circuitIds.has(assignment.circuitId)) {
throw new Error(
"Circuit section reorder command contains duplicate circuit ids."
);
}
if (
assignment.expectedSortOrder !== assignment.targetSortOrder
) {
hasChangedPosition = true;
}
circuitIds.add(assignment.circuitId);
}
if (!hasChangedPosition) {
throw new Error(
"Circuit section reorder command must change at least one position."
);
}
}
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 assertFiniteNumber(
value: unknown,
field: string
): asserts value is number {
if (typeof value !== "number" || !Number.isFinite(value)) {
throw new Error(`${field} must be a finite number.`);
}
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
@@ -0,0 +1,26 @@
import type { CircuitSectionReorderProjectCommand } from "../models/circuit-section-reorder-project-command.model.js";
import type {
AppendedProjectRevision,
ProjectRevisionSource,
} from "./project-revision.store.js";
export interface ExecuteCircuitSectionReorderCommandInput {
projectId: string;
expectedRevision: number;
source: ProjectRevisionSource;
description?: string;
actorId?: string;
historyTargetChangeSetId?: string;
command: CircuitSectionReorderProjectCommand;
}
export interface ExecutedCircuitSectionReorderCommand {
revision: AppendedProjectRevision;
inverse: CircuitSectionReorderProjectCommand;
}
export interface CircuitSectionReorderProjectCommandStore {
execute(
input: ExecuteCircuitSectionReorderCommandInput
): ExecutedCircuitSectionReorderCommand;
}
@@ -19,6 +19,10 @@ import {
assertCircuitUpdateProjectCommand,
circuitUpdateCommandType,
} from "../models/circuit-project-command.model.js";
import {
assertCircuitSectionReorderProjectCommand,
circuitSectionReorderCommandType,
} from "../models/circuit-section-reorder-project-command.model.js";
import {
assertCircuitDeleteProjectCommand,
assertCircuitInsertProjectCommand,
@@ -33,6 +37,7 @@ import type { CircuitDeviceRowProjectCommandStore } from "../ports/circuit-devic
import type { CircuitDeviceRowMoveProjectCommandStore } from "../ports/circuit-device-row-move-project-command.store.js";
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 { CircuitStructureProjectCommandStore } from "../ports/circuit-structure-project-command.store.js";
import type {
ExecuteProjectHistoryCommandInput,
@@ -63,6 +68,7 @@ export class ProjectCommandService implements ProjectCommandExecutor {
private readonly deviceRowStructureStore: CircuitDeviceRowStructureProjectCommandStore,
private readonly deviceRowMoveStore: CircuitDeviceRowMoveProjectCommandStore,
private readonly circuitStructureStore: CircuitStructureProjectCommandStore,
private readonly circuitSectionReorderStore: CircuitSectionReorderProjectCommandStore,
private readonly historyStore: ProjectHistoryStore
) {}
@@ -186,6 +192,13 @@ export class ProjectCommandService implements ProjectCommandExecutor {
command: input.command,
}).revision;
}
case circuitSectionReorderCommandType: {
assertCircuitSectionReorderProjectCommand(input.command);
return this.circuitSectionReorderStore.execute({
...input,
command: input.command,
}).revision;
}
default:
throw new Error(
`Unsupported project command type: ${input.command.type}.`