collabSupport/integrations/optisigns/client.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

154 lines
No EOL
4.5 KiB
JavaScript

// src/integrations/optisigns/client.js
import { GraphQLClient, gql } from 'graphql-request';
import { logger } from '../../utils/logger.js';
// Global caches (populated from bulk fetch in getOptiSignStatus)
const playlistById = new Map();
const assetById = new Map();
/**
* Update global caches from the playlistMap returned by getOptiSignStatus
*/
export function updateOptiSignsCaches(playlistMap) {
playlistMap.forEach((name, id) => {
playlistById.set(id, name);
});
logger('optisigns:client', `Updated global cache with ${playlistMap.size} playlists`, 'debug');
}
const client = new GraphQLClient('https://graphql-gateway.optisigns.com/graphql', {
headers: {
Authorization: `Bearer ${process.env.OPTISIGN_API_KEY}`
},
});
/**
* Fetch OptiSigns devices and playlists for a store
*/
export async function getOptiSignStatus(storeNumber) {
const paddedStore = String(storeNumber).padStart(6, '0');
logger('optisigns:client', `Fetching status for store ${storeNumber} (padded: ${paddedStore})`, 'debug');
let storeDevices = [];
const playlistMap = new Map();
try {
// 1. Fetch all devices with pagination
logger('optisigns:client', 'Fetching all devices...', 'debug');
let allDevices = [];
let after = null;
const pageSize = 50;
do {
const query = gql`
query GetDevices($first: Int, $after: String) {
devices(query: {}, first: $first, after: $after) {
page {
edges {
node {
_id
deviceName
UUID
pairingCode
currentType
currentAssetId
currentPlaylistId
localAppVersion
lastHeartBeat
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`;
const data = await client.request(query, { first: pageSize, after });
const pageEdges = data.devices.page.edges || [];
allDevices = allDevices.concat(pageEdges.map(e => e.node));
after = data.devices.page.pageInfo.hasNextPage
? data.devices.page.pageInfo.endCursor
: null;
logger('optisigns:client', `Fetched page with ${pageEdges.length} devices (after: ${after || 'null'})`, 'debug');
} while (after);
logger('optisigns:client', `Total devices fetched: ${allDevices.length}`, 'debug');
// Filter devices for this store
storeDevices = allDevices.filter(d => d.deviceName?.includes(paddedStore));
logger('optisigns:client', `Matching devices for store ${paddedStore}: ${storeDevices.length}`, 'debug');
// 2. Fetch all playlists (for name mapping)
logger('optisigns:client', 'Fetching all playlists...', 'debug');
let allPlaylists = [];
after = null;
do {
const playlistsData = await client.request(gql`
query GetAllPlaylists($first: Int, $after: String) {
playlists(first: $first, after: $after) {
page {
edges {
node {
_id
name
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`, { first: 100, after });
const pageEdges = playlistsData.playlists.page.edges || [];
allPlaylists = allPlaylists.concat(pageEdges.map(e => e.node));
after = playlistsData.playlists.page.pageInfo.hasNextPage
? playlistsData.playlists.page.pageInfo.endCursor
: null;
} while (after);
logger('optisigns:client', `Total playlists fetched: ${allPlaylists.length}`, 'debug');
// Build playlist name map
allPlaylists.forEach(pl => {
if (pl._id && pl.name) playlistMap.set(pl._id, pl.name);
});
// Update global cache so getPlaylistName can use it
updateOptiSignsCaches(playlistMap);
} catch (err) {
logger('optisigns:client', `Error fetching OptiSigns data: ${err.message}`, 'error');
if (err.response) {
logger('optisigns:client', `GraphQL response status: ${err.response.status}`, 'error');
}
}
return {
devices: storeDevices,
playlistMap
};
}
/**
* Fast cached lookup (synchronous)
*/
export function getPlaylistName(playlistId) {
if (!playlistId) return '—';
return playlistById.get(playlistId) || playlistId;
}
/**
* Fast cached lookup (synchronous)
*/
export function getAssetName(assetId) {
if (!assetId) return '—';
return assetById.get(assetId) || assetId;
}