import type {WebexSDK} from '../SDKConnector/types'; import {FAILURE_MESSAGE} from '../common/constants'; import {ACD_STATE, ACD_STATE_VALUES, UUID_PATTERN} from './constants'; import { CallQueue, CallQueueAgentStatus, CallQueueCatalogPage, CallQueueEventKind, CallQueueResponse, TelephonyAgentPayload, } from './types'; /** An update may have reached the server even if its response is stale. */ export function lifecycleConflictResponse(requestSent = false): CallQueueResponse { return { statusCode: 409, data: { error: requestSent ? 'Call queue client was reinitialized; request may have completed; refresh settings before retrying' : 'Call queue client was reinitialized; refresh settings', }, message: FAILURE_MESSAGE, }; } /** Device registration may populate entitlement models incrementally. */ export function hasReadyEntitlementModels(webex: WebexSDK): boolean { const models = webex.internal.device?.features?.entitlement?.models; return Array.isArray(models) && models.every((model) => typeof model?._values?.key === 'string'); } export default function actorIdentity(value: string): {id: string; type?: string} { const parse = (candidate: string) => /^ciscospark:\/\/[^/]+\/(PEOPLE|PERSON|VIRTUAL_LINE)\/([^/]+)$/i.exec(candidate); let match = parse(value); if (!match) { try { match = parse(atob(value.replace(/-/g, '+').replace(/_/g, '/'))); } catch { // An unencoded actor ID is also valid. } } return match ? {type: match[1].toUpperCase(), id: match[2].toLowerCase()} : {id: value.toLowerCase()}; } /** * Maps REST and Mercury ACD values onto one enum. * Unknown values are dropped so a bad event cannot invent a state. */ export function normalizeAcdState(value: string | undefined): ACD_STATE | undefined { return value === undefined ? undefined : ACD_STATE_VALUES.find(({values}) => values.includes(value))?.state; } function readString(value: unknown): string | undefined { return typeof value === 'string' && value.trim() ? value : undefined; } function readBoolean(value: unknown): boolean | undefined { return typeof value === 'boolean' ? value : undefined; } function readQueue(value: unknown): CallQueue | undefined { if (!value || typeof value !== 'object') { return undefined; } const record = value as Record; const id = readString(record.id); const available = readBoolean(record.available); if (!id) { return undefined; } const queue: CallQueue = {id}; if (available !== undefined) { queue.available = available; } const name = readString(record.name); const phoneNumber = readString(record.phoneNumber); const extension = readString(record.extension); const allowLogOffEnabled = readBoolean(record.allowLogOffEnabled); const hasCxEssentials = readBoolean(record.hasCxEssentials); if (name) { queue.name = name; } if (phoneNumber) { queue.phoneNumber = phoneNumber; } if (extension) { queue.extension = extension; } if (typeof record.skillLevel === 'number') { queue.skillLevel = record.skillLevel; } if (allowLogOffEnabled !== undefined) { queue.allowLogOffEnabled = allowLogOffEnabled; } if (hasCxEssentials !== undefined) { queue.hasCxEssentials = hasCxEssentials; } return queue; } /** Detect an incomplete settings queue list without treating omitted or non-array fields as lists. */ export function hasMalformedQueueRecords(body: unknown): boolean { if (!body || typeof body !== 'object') return false; const queues = (body as Record).queues; return Array.isArray(queues) && queues.some((queue) => !readQueue(queue)); } /** Match CSG UUIDs with catalog IDs encoded as ciscospark queue URIs. */ export function queueIdKey(id: string): string { if (UUID_PATTERN.test(id)) { return id.toLowerCase(); } try { const decoded = atob(id.replace(/-/g, '+').replace(/_/g, '/')); const match = /^ciscospark:\/\/[^/]+\/CALL_QUEUE\/([^/]+)$/i.exec(decoded); if (match && UUID_PATTERN.test(match[1])) { return match[1].toLowerCase(); } } catch { // IDs outside the encoded catalog format are compared unchanged. } return id; } /** Encode a raw UUID for public Webex APIs; leave already encoded IDs unchanged. */ export function publicApiId(id: string, type: 'CALL_QUEUE' | 'VIRTUAL_LINE'): string { return UUID_PATTERN.test(id) ? btoa(`ciscospark://us/${type}/${id}`) .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=+$/, '') : id; } /** * Reads the org queue list. That list carries the display name the agent settings call omits. */ export function parseCallQueueCatalog(body: unknown): CallQueueCatalogPage { if (!body || typeof body !== 'object') { return {entries: []}; } const record = body as Record; let list: unknown[] = []; if (Array.isArray(record.queues)) { list = record.queues; } else if (Array.isArray(record.items)) { list = record.items; } const entries = list.flatMap((value) => { if (!value || typeof value !== 'object') { return []; } const item = value as Record; const id = readString(item.id); if (!id) { return []; } const name = readString(item.name); return [name ? {id, name} : {id}]; }); const paging = record.paging; const next = paging && typeof paging === 'object' ? readString((paging as Record).next) : undefined; return next ? {entries, next} : {entries}; } /** * Fills a missing queue name from the catalog. A name already on the queue is kept. */ export function applyQueueNames( status: CallQueueAgentStatus, names: ReadonlyMap ): CallQueueAgentStatus { let changed = false; const queues = status.queues.map((queue) => { const name = names.get(queueIdKey(queue.id)); if (!name || queue.name) { return queue; } changed = true; return {...queue, name}; }); return changed ? {...status, queues} : status; } /** * Reads a GET/PUT body. Returns undefined when the body is not a queue settings document. */ export function parseCallQueueSettings( body: unknown, previous?: CallQueueAgentStatus ): CallQueueAgentStatus | undefined { if (!body || typeof body !== 'object') { return undefined; } const record = body as Record; const rawState = readString(record.agentACDState); const acdState = record.agentACDState == null ? previous?.acdState : normalizeAcdState(rawState); if ((!Array.isArray(record.queues) && !rawState) || !acdState) { return undefined; } const parsedQueues = Array.isArray(record.queues) ? record.queues.map(readQueue) : undefined; if (parsedQueues?.some((queue) => !queue)) return undefined; const queues = parsedQueues ? (parsedQueues as CallQueue[]) : previous?.queues ?? []; return {acdState, queues}; } export function unwrapAgentPayload(event: unknown): TelephonyAgentPayload | undefined { if (!event || typeof event !== 'object') { return undefined; } const record = event as Record; const nested = record.data; if (nested && typeof nested === 'object') { return nested as TelephonyAgentPayload; } return record as TelephonyAgentPayload; } function mergeQueues(current: CallQueue[], incoming: CallQueue[]): CallQueue[] { const byId = new Map(current.map((queue) => [queueIdKey(queue.id), queue])); incoming.forEach((queue) => { const key = queueIdKey(queue.id); const previous = byId.get(key); byId.set(key, previous ? {...previous, ...queue, id: previous.id} : queue); }); return Array.from(byId.values()); } /** * Applies one Mercury telephony_agent event onto the last known status. * A sync event replaces the queue list. State and join events merge by queue id. */ export function applyCallQueueEvent( current: CallQueueAgentStatus | undefined, kind: CallQueueEventKind, event: unknown ): CallQueueAgentStatus | undefined { const payload = unwrapAgentPayload(event); if (!payload) { return current; } const acdState = normalizeAcdState( payload.agentACDState ?? payload.acdStateInfo?.state ?? payload.state ); const rawQueues = Array.isArray(payload.queues) ? payload.queues : undefined; const incomingQueues = rawQueues ? rawQueues.map(readQueue).filter((queue): queue is CallQueue => Boolean(queue)) : undefined; const incompleteQueues = Boolean( rawQueues && incomingQueues && rawQueues.length !== incomingQueues.length ); const hasQueueUpdates = Boolean( rawQueues && incomingQueues && (!rawQueues.length || incomingQueues.length) ); const joinUpdates = Array.isArray(payload.joinInfo) ? payload.joinInfo.flatMap((entry) => { const id = readString(entry?.queueId); const status = readString(entry?.status)?.toLowerCase().trim(); if (!id || !status || !['joined', 'unjoined', 'unassigned'].includes(status)) { return []; } return [{id, status}]; }) : undefined; const hasJoinUpdates = joinUpdates !== undefined && (payload.joinInfo?.length === 0 || joinUpdates.length > 0); const incompleteJoinUpdates = Boolean( Array.isArray(payload.joinInfo) && joinUpdates && payload.joinInfo.length !== joinUpdates.length ); if (!acdState && !hasQueueUpdates && !hasJoinUpdates) { return current; } const base = current ?? {acdState: ACD_STATE.SIGN_OUT, queues: []}; let queues = base.queues; if (hasQueueUpdates && incomingQueues) { if ( kind === 'sync' && !incompleteQueues && (!incompleteJoinUpdates || rawQueues?.length === 0) ) { const previous = new Map(base.queues.map((queue) => [queueIdKey(queue.id), queue])); queues = incomingQueues.map((queue) => { const existing = previous.get(queueIdKey(queue.id)); return existing ? {...existing, ...queue, id: existing.id} : queue; }); } else { queues = mergeQueues(base.queues, incomingQueues); } } if (hasJoinUpdates && joinUpdates) { const currentById = new Map(base.queues.map((queue) => [queueIdKey(queue.id), queue])); queues.forEach((queue) => currentById.set(queueIdKey(queue.id), queue)); const joined = joinUpdates.flatMap(({id, status}) => { if (status === 'unassigned') return []; const previous = currentById.get(queueIdKey(id)); return [{...previous, id: previous?.id ?? id, available: status === 'joined'}]; }); if (kind === 'sync' && !incompleteJoinUpdates) { queues = joined; } else { const removed = new Set( joinUpdates.filter(({status}) => status === 'unassigned').map(({id}) => queueIdKey(id)) ); queues = mergeQueues( queues.filter((queue) => !removed.has(queueIdKey(queue.id))), joined ); } } return { acdState: acdState ?? base.acdState, queues, }; }