Manage circuit groups
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import type {
|
||||
CircuitGroupSnapshot,
|
||||
} from "../../domain/models/circuit-group-structure-project-command.model";
|
||||
import {
|
||||
circuitGroupCategoryNumbers,
|
||||
circuitGroupCategoryLabels,
|
||||
type CircuitGroupCategory,
|
||||
} from "../../shared/constants/circuit-group";
|
||||
import type { CircuitTreeSectionDto } from "../types";
|
||||
|
||||
export function toCircuitGroupSnapshot(
|
||||
section: CircuitTreeSectionDto,
|
||||
circuitListId: string
|
||||
): CircuitGroupSnapshot {
|
||||
if (!section.category || !section.groupNumber) {
|
||||
throw new Error("Der Bereich ist keine verwaltbare Stromkreisgruppe.");
|
||||
}
|
||||
return {
|
||||
id: section.id,
|
||||
circuitListId,
|
||||
key: section.key,
|
||||
displayName: section.displayName,
|
||||
prefix: section.prefix,
|
||||
sortOrder: section.sortOrder,
|
||||
category: section.category,
|
||||
groupNumber: section.groupNumber,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildNewCircuitGroupSnapshot(input: {
|
||||
id: string;
|
||||
circuitListId: string;
|
||||
category: CircuitGroupCategory;
|
||||
displayName?: string;
|
||||
sections: readonly CircuitTreeSectionDto[];
|
||||
}): CircuitGroupSnapshot {
|
||||
const groupedSections = input.sections.filter(
|
||||
(section): section is CircuitTreeSectionDto & {
|
||||
category: CircuitGroupCategory;
|
||||
groupNumber: number;
|
||||
} => Boolean(section.category && section.groupNumber)
|
||||
);
|
||||
const groupNumber =
|
||||
Math.max(
|
||||
0,
|
||||
...groupedSections
|
||||
.filter((section) => section.category === input.category)
|
||||
.map((section) => section.groupNumber)
|
||||
) + 1;
|
||||
return {
|
||||
id: input.id,
|
||||
circuitListId: input.circuitListId,
|
||||
key: `${input.category}_${groupNumber}`,
|
||||
displayName:
|
||||
input.displayName?.trim() ||
|
||||
`${circuitGroupCategoryLabels[input.category]} ${groupNumber}`,
|
||||
prefix: `-${circuitGroupCategoryNumbers[input.category]}F${groupNumber}.`,
|
||||
sortOrder: getNewGroupSortOrder(
|
||||
input.category,
|
||||
groupedSections
|
||||
),
|
||||
category: input.category,
|
||||
groupNumber,
|
||||
};
|
||||
}
|
||||
|
||||
export function renameCircuitGroupSnapshot(
|
||||
expected: CircuitGroupSnapshot,
|
||||
displayName: string
|
||||
): CircuitGroupSnapshot {
|
||||
return { ...expected, displayName: displayName.trim() };
|
||||
}
|
||||
|
||||
export function canDeleteCircuitGroup(
|
||||
section: CircuitTreeSectionDto
|
||||
): boolean {
|
||||
return section.circuits.length === 0 && section.components.length === 0;
|
||||
}
|
||||
|
||||
function getNewGroupSortOrder(
|
||||
category: CircuitGroupCategory,
|
||||
sections: readonly (CircuitTreeSectionDto & {
|
||||
category: CircuitGroupCategory;
|
||||
groupNumber: number;
|
||||
})[]
|
||||
): number {
|
||||
const ordered = [...sections].sort(
|
||||
(left, right) => left.sortOrder - right.sortOrder
|
||||
);
|
||||
const sameCategory = ordered.filter(
|
||||
(section) => section.category === category
|
||||
);
|
||||
const previous = sameCategory[sameCategory.length - 1];
|
||||
if (!previous) {
|
||||
return ordered.length === 0
|
||||
? 10
|
||||
: Math.max(...ordered.map((section) => section.sortOrder)) + 10;
|
||||
}
|
||||
const next = ordered.find(
|
||||
(section) => section.sortOrder > previous.sortOrder
|
||||
);
|
||||
return next
|
||||
? previous.sortOrder + (next.sortOrder - previous.sortOrder) / 2
|
||||
: previous.sortOrder + 10;
|
||||
}
|
||||
Reference in New Issue
Block a user