collabSupport/services/enrichment/optisignsMatcher.js
jmcqueen 351f89a9a4 Initial commit: CollabFinder Webex bot
Multi-integration Webex chat/HTTP bot that unifies phone, AV, and
network status for retail store support. Consolidates data from
Webex Calling, Meraki, Workspace ONE (MDM), Atlas AMP, RED digital
signage, and OptiSigns into rich per-store status commands.

Key surfaces:
- /phonestatus, /avstatus — per-store phone & AV device reports with
  clickable Meraki deep-links and per-port detail.
- /webexhost — check/assign Webex Meetings host licenses via the
  Service App; adaptive-card confirmation flow, HTTP-API-gated.
- /offboarduser — full Webex Admin offboarding (auth revoke, device
  wipe, license removal); adaptive-card confirmation.
- /jirapoll — on-demand trigger for the hourly Jira poller.
- /bulkavstatuscsv — bulk store CSV export with concurrency limits.

Automation:
- Hourly Jira poller (node-cron) with an X.AI (Grok) ticket classifier
  that categorizes unassigned tickets as phone/av/skip, extracts store
  numbers from free-text, and enriches Jira with the same detailed
  markdown the chat commands emit (converted to Jira ADF, preserves
  bold + Meraki links). Idempotent via a `bot-enriched` Jira label.

Architecture:
- Node.js 20+, ESM, Express 5, webex-node-bot-framework.
- Layered integrations (integrations/*), services (services/*),
  commands (commands/*), utils (utils/*).
- Shared markdown renderers (services/renderers/*) feed both chat
  handlers and the Jira poller so the two surfaces stay in sync.
- Hand-rolled markdown-to-ADF converter (utils/markdownToAdf.js) —
  no new npm dependency.
- Node built-in test runner (`node --test tests/*.test.js`), 30 tests
  covering the converter, renderers, and poller ADF assembly.

Docker + docker-compose deployment. Config via .env
(see .env.example for the full option surface).
2026-07-01 16:55:03 -04:00

97 lines
3.5 KiB
JavaScript

// services/enrichment/optisignsMatcher.js
//
// Shared OptiSigns device matching and enrichment helpers for AV.
// This centralizes the logic previously duplicated in deviceService, avDeviceBuilder, and avEnricher.
//
// - findBestOptiSignsMatch: unified finder (prefers normalized exact match for consistency,
// with loose contains fallback for compatibility).
// - createOptiSignsDisplay: produces the slim {content, lastHeartBeat, isOld} shape used by chat (/avstatus) and HTML.
// - createRichOptiSigns: produces the rich shape used by /build and modals (spreads raw + resolved names).
//
// Uses normalizePlayerName from normalizers and the resolution helpers from the OptiSigns client.
// Extracted as part of Option A incremental consolidation (after RED).
import { normalizePlayerName } from './normalizers.js';
import { getPlaylistName, getAssetName } from '../../integrations/optisigns/client.js';
/**
* Find the best matching OptiSigns device for a given AV/MDM identifier (e.g. friendlyName or device.identifier
* containing VW/LED).
*
* @param {string} identifier
* @param {Array} optiList - list of raw devices from getOptiSignStatus().devices
* @returns {Object|null} the matching raw OptiSigns device or null
*/
export function findBestOptiSignsMatch(identifier, optiList) {
if (!identifier || !Array.isArray(optiList) || optiList.length === 0) {
return null;
}
const normId = normalizePlayerName(identifier).toLowerCase().trim();
if (!normId) return null;
// Strategy 1: Exact normalized match on deviceName (preferred; matches deviceService and avEnricher logic)
let match = optiList.find(o =>
normalizePlayerName(o.deviceName || '').toLowerCase().trim() === normId
);
if (match) {
return match;
}
// Strategy 2: Loose contains fallback using original identifier (preserves builder's previous heuristic
// for cases with extra text in names; uses upper for broad match)
const idUpper = identifier.toUpperCase().trim();
if (idUpper) {
match = optiList.find(d =>
(d.deviceName || '').toUpperCase().includes(idUpper)
);
if (match) {
return match;
}
}
return null;
}
/**
* Build the slim OptiSigns display object used by chat commands (/avstatus) and HTML views.
* (Logic moved from deviceService for sharing.)
*/
export function createOptiSignsDisplay(rawOpti) {
if (!rawOpti) return null;
let content = 'Idle';
if (rawOpti.currentType === 'playlist' && rawOpti.currentPlaylistId) {
content = `Playing "${getPlaylistName(rawOpti.currentPlaylistId)}"`;
} else if (rawOpti.currentType === 'asset' && rawOpti.currentAssetId) {
content = `Showing "${getAssetName(rawOpti.currentAssetId)}"`;
} else if (rawOpti.currentType) {
content = rawOpti.currentType;
}
const hoursSinceHeartbeat = rawOpti.lastHeartBeat
? (Date.now() - new Date(rawOpti.lastHeartBeat).getTime()) / (1000 * 60 * 60)
: 999;
return {
content,
lastHeartBeat: rawOpti.lastHeartBeat,
isOld: hoursSinceHeartbeat > 24
};
}
/**
* Build the rich OptiSigns object used by the build endpoint and modals.
* Spreads the raw + resolves current names/ids for display (logic moved from avDeviceBuilder).
*/
export function createRichOptiSigns(rawOpti) {
if (!rawOpti) return null;
return {
...rawOpti,
currentPlaylistName: getPlaylistName(rawOpti.currentPlaylistId),
currentAssetName: rawOpti.currentAssetId ? getAssetName(rawOpti.currentAssetId) : null,
currentPlaylistId: rawOpti.currentPlaylistId,
currentAssetId: rawOpti.currentAssetId
};
}