Add component update commands
This commit is contained in:
@@ -13,6 +13,8 @@ export const distributionBoardComponentInsertCommandType =
|
||||
"distribution-board-component.insert" as const;
|
||||
export const distributionBoardComponentDeleteCommandType =
|
||||
"distribution-board-component.delete" as const;
|
||||
export const distributionBoardComponentUpdateCommandType =
|
||||
"distribution-board-component.update" as const;
|
||||
export const distributionBoardComponentStructureCommandSchemaVersion =
|
||||
1 as const;
|
||||
|
||||
@@ -47,6 +49,11 @@ interface DistributionBoardComponentStructurePayload {
|
||||
snapshot: DistributionBoardComponentSnapshot;
|
||||
}
|
||||
|
||||
interface DistributionBoardComponentUpdatePayload {
|
||||
expected: DistributionBoardComponentSnapshot;
|
||||
target: DistributionBoardComponentSnapshot;
|
||||
}
|
||||
|
||||
export interface DistributionBoardComponentInsertProjectCommand
|
||||
extends SerializedProjectCommand<DistributionBoardComponentStructurePayload> {
|
||||
schemaVersion: typeof distributionBoardComponentStructureCommandSchemaVersion;
|
||||
@@ -59,9 +66,16 @@ export interface DistributionBoardComponentDeleteProjectCommand
|
||||
type: typeof distributionBoardComponentDeleteCommandType;
|
||||
}
|
||||
|
||||
export interface DistributionBoardComponentUpdateProjectCommand
|
||||
extends SerializedProjectCommand<DistributionBoardComponentUpdatePayload> {
|
||||
schemaVersion: typeof distributionBoardComponentStructureCommandSchemaVersion;
|
||||
type: typeof distributionBoardComponentUpdateCommandType;
|
||||
}
|
||||
|
||||
export type DistributionBoardComponentStructureProjectCommand =
|
||||
| DistributionBoardComponentInsertProjectCommand
|
||||
| DistributionBoardComponentDeleteProjectCommand;
|
||||
| DistributionBoardComponentDeleteProjectCommand
|
||||
| DistributionBoardComponentUpdateProjectCommand;
|
||||
|
||||
export function createDistributionBoardComponentInsertProjectCommand(
|
||||
snapshot: DistributionBoardComponentSnapshot
|
||||
@@ -89,6 +103,20 @@ export function createDistributionBoardComponentDeleteProjectCommand(
|
||||
return command;
|
||||
}
|
||||
|
||||
export function createDistributionBoardComponentUpdateProjectCommand(
|
||||
expected: DistributionBoardComponentSnapshot,
|
||||
target: DistributionBoardComponentSnapshot
|
||||
): DistributionBoardComponentUpdateProjectCommand {
|
||||
const command: DistributionBoardComponentUpdateProjectCommand = {
|
||||
schemaVersion:
|
||||
distributionBoardComponentStructureCommandSchemaVersion,
|
||||
type: distributionBoardComponentUpdateCommandType,
|
||||
payload: { expected, target },
|
||||
};
|
||||
assertDistributionBoardComponentUpdateProjectCommand(command);
|
||||
return command;
|
||||
}
|
||||
|
||||
export function assertDistributionBoardComponentInsertProjectCommand(
|
||||
command: SerializedProjectCommand<unknown>
|
||||
): asserts command is DistributionBoardComponentInsertProjectCommand {
|
||||
@@ -107,6 +135,28 @@ export function assertDistributionBoardComponentDeleteProjectCommand(
|
||||
);
|
||||
}
|
||||
|
||||
export function assertDistributionBoardComponentUpdateProjectCommand(
|
||||
command: SerializedProjectCommand<unknown>
|
||||
): asserts command is DistributionBoardComponentUpdateProjectCommand {
|
||||
if (
|
||||
command.schemaVersion !==
|
||||
distributionBoardComponentStructureCommandSchemaVersion ||
|
||||
command.type !== distributionBoardComponentUpdateCommandType ||
|
||||
!isPlainObject(command.payload) ||
|
||||
Object.keys(command.payload).length !== 2
|
||||
) {
|
||||
throw new Error(
|
||||
"Unsupported distribution-board component update command."
|
||||
);
|
||||
}
|
||||
assertDistributionBoardComponentSnapshot(command.payload.expected);
|
||||
assertDistributionBoardComponentSnapshot(command.payload.target);
|
||||
assertSameComponentIdentity(
|
||||
command.payload.expected,
|
||||
command.payload.target
|
||||
);
|
||||
}
|
||||
|
||||
function assertStructureCommand(
|
||||
command: SerializedProjectCommand<unknown>,
|
||||
type:
|
||||
@@ -127,6 +177,25 @@ function assertStructureCommand(
|
||||
assertDistributionBoardComponentSnapshot(command.payload.snapshot);
|
||||
}
|
||||
|
||||
function assertSameComponentIdentity(
|
||||
expected: DistributionBoardComponentSnapshot,
|
||||
target: DistributionBoardComponentSnapshot
|
||||
) {
|
||||
for (const field of [
|
||||
"id",
|
||||
"circuitListId",
|
||||
"sectionId",
|
||||
"role",
|
||||
"placement",
|
||||
] as const) {
|
||||
if (expected.component[field] !== target.component[field]) {
|
||||
throw new Error(
|
||||
"Distribution-board component updates cannot change ownership or role."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function assertDistributionBoardComponentSnapshot(
|
||||
value: unknown
|
||||
): asserts value is DistributionBoardComponentSnapshot {
|
||||
|
||||
@@ -44,8 +44,10 @@ import {
|
||||
import {
|
||||
assertDistributionBoardComponentDeleteProjectCommand,
|
||||
assertDistributionBoardComponentInsertProjectCommand,
|
||||
assertDistributionBoardComponentUpdateProjectCommand,
|
||||
distributionBoardComponentDeleteCommandType,
|
||||
distributionBoardComponentInsertCommandType,
|
||||
distributionBoardComponentUpdateCommandType,
|
||||
} from "../models/distribution-board-component-structure-project-command.model.js";
|
||||
import {
|
||||
assertDistributionBoardDeleteProjectCommand,
|
||||
@@ -322,6 +324,15 @@ export class ProjectCommandService implements ProjectCommandExecutor {
|
||||
command: input.command,
|
||||
}).revision;
|
||||
}
|
||||
case distributionBoardComponentUpdateCommandType: {
|
||||
assertDistributionBoardComponentUpdateProjectCommand(
|
||||
input.command
|
||||
);
|
||||
return this.distributionBoardComponentStructureStore.execute({
|
||||
...input,
|
||||
command: input.command,
|
||||
}).revision;
|
||||
}
|
||||
case projectFloorInsertCommandType: {
|
||||
assertProjectFloorInsertProjectCommand(input.command);
|
||||
return this.projectLocationStructureStore.execute({
|
||||
|
||||
Reference in New Issue
Block a user