netanalyzer/services/mdm.js
Joseph McQueen 0a46d25bd6 chore: project cleanup — dead code, logging, tests, WS hardening
- Delete unused dataMerger, formatter, Device model, dead service exports,
  and the empty agents/ + .gitkeep placeholders.
- Extract STORE_MODES and MDM device-type filters into a shared constants.js.
- Anchor bot regexes (^help|^store|^analyze) so "analyze store 305" no
  longer fires both handlers; replace catch-all noise.
- Hoist inline require() calls in integrations to top-of-file imports.
- Harden WebSocket server: Authorization header support, single-agent
  enforcement, bounded pending requests, server-level error handler,
  coalesced cache refresh in Meraki client.
- Wrap Meraki/MDM network calls with withRetry; add request timeouts.
- Migrate all console.* calls onto utils/logger.js (LOG_LEVEL aware);
  drive Webex framework logLevel from env.
- Refactor storeDetail.js: shared renderClientLine + buildMdmSection
  helpers cut duplication roughly in half.
- Refresh README structure, document LOG_LEVEL, add npm run agent script,
  add jest testMatch + new tests (handlers, HealthReport, Store, ws).

Verified: npm run lint clean, 7 suites / 31 tests passing.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 17:21:22 -04:00

62 lines
1.6 KiB
JavaScript

const axios = require('axios');
const config = require('../config');
const { withRetry } = require('../utils/retry');
const logger = require('../utils/logger');
const RETRY_OPTS = { retries: 2, initialDelayMs: 300 };
async function getMDMToken() {
logger.debug('Requesting Workspace ONE token');
const response = await withRetry(
() =>
axios.post(
config.mdm.tokenUrl,
new URLSearchParams({
grant_type: 'client_credentials',
client_id: config.mdm.clientId,
client_secret: config.mdm.clientSecret,
}),
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
timeout: 15000,
}
),
RETRY_OPTS
);
logger.info('MDM token acquired');
return response.data.access_token;
}
async function getMDMDevices(storeNumber) {
const storePadded = String(storeNumber).padStart(6, '0');
logger.info('Fetching MDM devices', { storePadded });
try {
const token = await getMDMToken();
const response = await withRetry(
() =>
axios.get(`${config.mdm.baseUrl}/api/mdm/devices/search`, {
params: { user: storePadded },
headers: {
Authorization: `Bearer ${token}`,
'aw-tenant-code': config.mdm.tenantCode,
Accept: 'application/json',
},
timeout: 20000,
}),
RETRY_OPTS
);
const devices = response.data.Devices || [];
logger.info('Fetched MDM devices', { count: devices.length });
return devices;
} catch (err) {
logger.error('MDM devices fetch failed', { error: err.message });
return [];
}
}
module.exports = { getMDMDevices };