forked from jappel/leistungsbilanz-ts
Harden grid deletion and BMK validation
This commit is contained in:
parent
ea61772e62
commit
f8802fecdb
6 changed files with 300 additions and 12 deletions
74
src/frontend/utils/circuit-grid-safety.ts
Normal file
74
src/frontend/utils/circuit-grid-safety.ts
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import type {
|
||||
GridInsertionCellKind,
|
||||
GridInsertionRowType,
|
||||
} from "./circuit-grid-insertion.js";
|
||||
|
||||
export interface GridDeleteSelection {
|
||||
rowType: GridInsertionRowType;
|
||||
cellKind: GridInsertionCellKind;
|
||||
circuitId?: string;
|
||||
deviceId?: string;
|
||||
}
|
||||
|
||||
export type GridDeleteIntent =
|
||||
| { kind: "circuit"; circuitId: string }
|
||||
| { kind: "device"; deviceId: string };
|
||||
|
||||
export interface CircuitIdentifierRecord {
|
||||
id: string;
|
||||
equipmentIdentifier: string;
|
||||
}
|
||||
|
||||
export function resolveGridDeleteIntent(selection: GridDeleteSelection | null): GridDeleteIntent | null {
|
||||
if (!selection || selection.rowType === "placeholder") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const targetsDevice =
|
||||
selection.rowType === "deviceRow" ||
|
||||
(selection.rowType === "circuitCompact" && selection.cellKind === "deviceField");
|
||||
|
||||
if (targetsDevice && selection.deviceId) {
|
||||
return { kind: "device", deviceId: selection.deviceId };
|
||||
}
|
||||
if (selection.circuitId) {
|
||||
return { kind: "circuit", circuitId: selection.circuitId };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function normalizeEquipmentIdentifier(value: string): string {
|
||||
return value.trim().toUpperCase();
|
||||
}
|
||||
|
||||
export function hasEquipmentIdentifierConflict(
|
||||
circuits: CircuitIdentifierRecord[],
|
||||
currentCircuitId: string,
|
||||
candidate: string
|
||||
): boolean {
|
||||
const normalizedCandidate = normalizeEquipmentIdentifier(candidate);
|
||||
if (!normalizedCandidate) {
|
||||
return false;
|
||||
}
|
||||
return circuits.some(
|
||||
(circuit) =>
|
||||
circuit.id !== currentCircuitId &&
|
||||
normalizeEquipmentIdentifier(circuit.equipmentIdentifier) === normalizedCandidate
|
||||
);
|
||||
}
|
||||
|
||||
export function findDuplicateEquipmentIdentifierCircuitIds(
|
||||
circuits: CircuitIdentifierRecord[]
|
||||
): string[] {
|
||||
const idsByIdentifier = new Map<string, string[]>();
|
||||
for (const circuit of circuits) {
|
||||
const normalized = normalizeEquipmentIdentifier(circuit.equipmentIdentifier);
|
||||
if (!normalized) {
|
||||
continue;
|
||||
}
|
||||
const ids = idsByIdentifier.get(normalized) ?? [];
|
||||
ids.push(circuit.id);
|
||||
idsByIdentifier.set(normalized, ids);
|
||||
}
|
||||
return [...idsByIdentifier.values()].filter((ids) => ids.length > 1).flat();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue