Add device row move history
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import type { SerializedProjectCommand } from "./project-command.model.js";
|
||||
|
||||
export const circuitDeviceRowMoveCommandType =
|
||||
"circuit-device-row.move" as const;
|
||||
export const circuitDeviceRowMoveCommandSchemaVersion = 1 as const;
|
||||
|
||||
export interface CircuitDeviceRowMoveAssignment {
|
||||
rowId: string;
|
||||
expectedCircuitId: string;
|
||||
expectedSortOrder: number;
|
||||
targetCircuitId: string;
|
||||
targetSortOrder: number;
|
||||
}
|
||||
|
||||
export interface CircuitDeviceRowMoveCommandPayload {
|
||||
moves: CircuitDeviceRowMoveAssignment[];
|
||||
}
|
||||
|
||||
export interface CircuitDeviceRowMoveProjectCommand
|
||||
extends SerializedProjectCommand<CircuitDeviceRowMoveCommandPayload> {
|
||||
schemaVersion: typeof circuitDeviceRowMoveCommandSchemaVersion;
|
||||
type: typeof circuitDeviceRowMoveCommandType;
|
||||
}
|
||||
|
||||
export function createCircuitDeviceRowMoveProjectCommand(
|
||||
moves: CircuitDeviceRowMoveAssignment[]
|
||||
): CircuitDeviceRowMoveProjectCommand {
|
||||
const command: CircuitDeviceRowMoveProjectCommand = {
|
||||
schemaVersion: circuitDeviceRowMoveCommandSchemaVersion,
|
||||
type: circuitDeviceRowMoveCommandType,
|
||||
payload: { moves },
|
||||
};
|
||||
assertCircuitDeviceRowMoveProjectCommand(command);
|
||||
return command;
|
||||
}
|
||||
|
||||
export function assertCircuitDeviceRowMoveProjectCommand(
|
||||
command: SerializedProjectCommand<unknown>
|
||||
): asserts command is CircuitDeviceRowMoveProjectCommand {
|
||||
if (
|
||||
command.schemaVersion !== circuitDeviceRowMoveCommandSchemaVersion ||
|
||||
command.type !== circuitDeviceRowMoveCommandType
|
||||
) {
|
||||
throw new Error("Unsupported circuit device-row move command.");
|
||||
}
|
||||
if (!isPlainObject(command.payload)) {
|
||||
throw new Error(
|
||||
"Circuit device-row move payload must be an object."
|
||||
);
|
||||
}
|
||||
const moves = command.payload.moves;
|
||||
if (!Array.isArray(moves) || moves.length === 0) {
|
||||
throw new Error(
|
||||
"Circuit device-row move command requires at least one move."
|
||||
);
|
||||
}
|
||||
|
||||
const rowIds = new Set<string>();
|
||||
for (const move of moves) {
|
||||
if (!isPlainObject(move)) {
|
||||
throw new Error(
|
||||
"Circuit device-row move command contains an invalid move."
|
||||
);
|
||||
}
|
||||
assertNonEmptyString(move.rowId, "rowId");
|
||||
assertNonEmptyString(
|
||||
move.expectedCircuitId,
|
||||
"expectedCircuitId"
|
||||
);
|
||||
assertFiniteNumber(
|
||||
move.expectedSortOrder,
|
||||
"expectedSortOrder"
|
||||
);
|
||||
assertNonEmptyString(move.targetCircuitId, "targetCircuitId");
|
||||
assertFiniteNumber(move.targetSortOrder, "targetSortOrder");
|
||||
if (rowIds.has(move.rowId)) {
|
||||
throw new Error(
|
||||
"Circuit device-row move command contains duplicate row ids."
|
||||
);
|
||||
}
|
||||
if (
|
||||
move.expectedCircuitId === move.targetCircuitId &&
|
||||
move.expectedSortOrder === move.targetSortOrder
|
||||
) {
|
||||
throw new Error(
|
||||
"Circuit device-row move command contains a no-op move."
|
||||
);
|
||||
}
|
||||
|
||||
rowIds.add(move.rowId);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user