forked from jappel/leistungsbilanz-ts
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
export interface LegacyConsumerForPlanning {
|
|
id: string;
|
|
circuitNumber: string | null;
|
|
category: string | null;
|
|
phaseType: string | null;
|
|
phaseCount: number | null;
|
|
}
|
|
|
|
// Accepts only normalized BMK-like legacy circuit numbers used for grouping.
|
|
export function normalizeCircuitNumber(value: string | null): string | null {
|
|
if (!value) {
|
|
return null;
|
|
}
|
|
const trimmed = value.trim().toUpperCase();
|
|
if (!trimmed) {
|
|
return null;
|
|
}
|
|
if (!/^-\d+F\d+$/.test(trimmed)) {
|
|
return null;
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
// Best-effort fallback when no valid circuit number exists. Keeps migration deterministic
|
|
// by preferring explicit category/phase cues over random assignment.
|
|
export function inferSectionKeyFromLegacyInput(consumer: LegacyConsumerForPlanning): string | null {
|
|
const category = (consumer.category ?? "").toLowerCase();
|
|
if (category.includes("light") || category.includes("beleuchtung")) {
|
|
return "lighting";
|
|
}
|
|
if (consumer.phaseCount === 3) {
|
|
return "three_phase";
|
|
}
|
|
if (consumer.phaseCount === 1) {
|
|
return "single_phase";
|
|
}
|
|
|
|
const phaseType = (consumer.phaseType ?? "").toLowerCase();
|
|
if (phaseType.includes("three") || phaseType.includes("3")) {
|
|
return "three_phase";
|
|
}
|
|
if (phaseType.includes("single") || phaseType.includes("1")) {
|
|
return "single_phase";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Prefix-based section inference for already normalized equipment identifiers.
|
|
export function inferSectionKeyFromEquipmentIdentifier(equipmentIdentifier: string): string | null {
|
|
if (equipmentIdentifier.startsWith("-1F")) {
|
|
return "lighting";
|
|
}
|
|
if (equipmentIdentifier.startsWith("-2F")) {
|
|
return "single_phase";
|
|
}
|
|
if (equipmentIdentifier.startsWith("-3F")) {
|
|
return "three_phase";
|
|
}
|
|
return null;
|
|
}
|