Persist section renumbering
This commit is contained in:
parent
abcb468807
commit
c45afd0981
24 changed files with 317 additions and 638 deletions
54
src/frontend/utils/circuit-section-renumber-command.ts
Normal file
54
src/frontend/utils/circuit-section-renumber-command.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import type {
|
||||
CircuitSectionRenumberAssignment,
|
||||
} from "../../domain/models/circuit-section-renumber-project-command.model.js";
|
||||
import type { CircuitTreeSectionDto } from "../types.js";
|
||||
|
||||
export function buildCircuitSectionRenumberAssignments(
|
||||
sections: CircuitTreeSectionDto[],
|
||||
sectionId: string
|
||||
): CircuitSectionRenumberAssignment[] {
|
||||
const targetSection = sections.find(
|
||||
(section) => section.id === sectionId
|
||||
);
|
||||
if (!targetSection) {
|
||||
throw new Error("Der Bereich wurde nicht gefunden.");
|
||||
}
|
||||
|
||||
const identifiersOutsideSection = new Set(
|
||||
sections
|
||||
.filter((section) => section.id !== sectionId)
|
||||
.flatMap((section) =>
|
||||
section.circuits.map(
|
||||
(circuit) => circuit.equipmentIdentifier
|
||||
)
|
||||
)
|
||||
);
|
||||
const assignments: CircuitSectionRenumberAssignment[] = [];
|
||||
let suffix = 1;
|
||||
for (const circuit of targetSection.circuits) {
|
||||
let targetEquipmentIdentifier = `${targetSection.prefix}${suffix}`;
|
||||
while (
|
||||
identifiersOutsideSection.has(targetEquipmentIdentifier)
|
||||
) {
|
||||
suffix += 1;
|
||||
targetEquipmentIdentifier = `${targetSection.prefix}${suffix}`;
|
||||
}
|
||||
assignments.push({
|
||||
circuitId: circuit.id,
|
||||
expectedEquipmentIdentifier: circuit.equipmentIdentifier,
|
||||
targetEquipmentIdentifier,
|
||||
});
|
||||
suffix += 1;
|
||||
}
|
||||
|
||||
if (
|
||||
assignments.every(
|
||||
(assignment) =>
|
||||
assignment.expectedEquipmentIdentifier ===
|
||||
assignment.targetEquipmentIdentifier
|
||||
)
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
return assignments;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue