Files
adam-acs/app/utils/cwmp-events.ts
T
Kevin L'Hereec f9e56c78b1 refactor: migrate all internal imports to package.json subpath aliases
- Add #domain, #protocol, #utils aliases to package.json imports map
- Replace all relative imports (./, ../, ../..) in app/ and
  database/mongo_migrations/ with the corresponding #alias imports,
  eliminating relative path traversal across the codebase
- Remove dead code: ConfigurationVersionModel.findByConfigurationIdentifier
  (unused, superseded by paginateVersions) and unused exception classes
  (NotFoundException, UnprocessableEntityException, DatabaseException)
  from app/exceptions/index.ts, keeping only BadRequestException
- Fix pre-existing syntax error in configuration_version.ts (mismatched
  parenthesis) that broke tsc
- Fix eqeqeq violation in configuration_service.ts and isNaN ->
  Number.isNaN in configuration_engine.ts / cwmp-1-0.ts / exchange_controller.ts
- Fix no-shadow violation in cwmp-1-0.ts EventStruct constructor
- Prettier formatting pass on touched files

Validated: tsc --noEmit clean, eslint app/ clean, full test suite green.
2026-07-22 11:45:14 +02:00

78 lines
1.9 KiB
TypeScript

/**
* Shared CWMP event normalization and priority resolution utilities.
* Single source of truth for event code handling (fixes MINEUR-9).
*/
export const EVENT_PRIORITY: Record<string, number> = {
'BOOTSTRAP': 100,
'BOOT': 90,
'CONNECTION REQUEST': 80,
'PERIODIC': 70,
'VALUE CHANGE': 60,
'DIAGNOSTICS COMPLETE': 50,
'TRANSFER COMPLETE': 40,
'SCHEDULE': 30,
}
/**
* Normalize an EventCode from an Inform message.
* Strips numeric prefix and normalizes the label.
* Examples: '0 BOOTSTRAP' → 'BOOTSTRAP', '1 BOOT' → 'BOOT', '6' → 'CONNECTION REQUEST'
*/
export function normalizeEventCode(raw: string): string {
const trimmed = raw.trim().toUpperCase()
// Already a known label without numeric prefix
if (EVENT_PRIORITY[trimmed] !== undefined) return trimmed
// Full format like "0 BOOTSTRAP" → strip numeric prefix
const match = trimmed.match(/^\d+\s+(.+)$/)
if (match) return match[1]
// Numeric-only → expand to standard label
switch (trimmed) {
case '0':
return 'BOOTSTRAP'
case '1':
return 'BOOT'
case '2':
return 'PERIODIC'
case '3':
return 'SCHEDULED'
case '4':
return 'VALUE CHANGE'
case '5':
return 'KICKED'
case '6':
return 'CONNECTION REQUEST'
case '7':
return 'TRANSFER COMPLETE'
case '8':
return 'DIAGNOSTICS COMPLETE'
default:
return trimmed
}
}
/**
* Resolve the highest priority event from a list of raw event codes.
* Returns the normalized name of the highest-priority event.
*/
export function resolveHighestPriorityEvent(events: string[]): string {
if (events.length === 0) return ''
let highest = ''
let highestPriority = -1
for (const raw of events) {
const normalized = normalizeEventCode(raw)
const priority = EVENT_PRIORITY[normalized] ?? 0
if (priority > highestPriority) {
highestPriority = priority
highest = normalized
}
}
return highest
}