forked from jappel/leistungsbilanz-ts
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import type {
|
|
CircuitSectionReorderAssignment,
|
|
} from "../../domain/models/circuit-section-reorder-project-command.model.js";
|
|
import type { CircuitTreeCircuitDto } from "../types.js";
|
|
|
|
export function buildCircuitSectionReorderAssignments(
|
|
circuits: CircuitTreeCircuitDto[],
|
|
orderedCircuitIds: string[]
|
|
): CircuitSectionReorderAssignment[] {
|
|
if (
|
|
circuits.length !== orderedCircuitIds.length ||
|
|
new Set(orderedCircuitIds).size !== orderedCircuitIds.length
|
|
) {
|
|
throw new Error(
|
|
"Die Reihenfolge muss jeden Stromkreis des Bereichs genau einmal enthalten."
|
|
);
|
|
}
|
|
|
|
const circuitsById = new Map(
|
|
circuits.map((circuit) => [circuit.id, circuit])
|
|
);
|
|
const assignments = orderedCircuitIds.map((circuitId, index) => {
|
|
const circuit = circuitsById.get(circuitId);
|
|
if (!circuit) {
|
|
throw new Error(
|
|
"Die Reihenfolge enthält einen ungültigen Stromkreis."
|
|
);
|
|
}
|
|
return {
|
|
circuitId,
|
|
expectedSortOrder: circuit.sortOrder,
|
|
targetSortOrder: (index + 1) * 10,
|
|
};
|
|
});
|
|
if (
|
|
assignments.every(
|
|
(assignment) =>
|
|
assignment.expectedSortOrder === assignment.targetSortOrder
|
|
)
|
|
) {
|
|
return [];
|
|
}
|
|
return assignments;
|
|
}
|