diff --git a/packages/server/bin/lamassu-batch-diagnostics b/packages/server/bin/lamassu-batch-diagnostics new file mode 100644 index 00000000..6d0d61cc --- /dev/null +++ b/packages/server/bin/lamassu-batch-diagnostics @@ -0,0 +1,36 @@ +#!/usr/bin/env node + +require('../lib/environment-helper') + +const db = require('../lib/db') +const machineLoader = require('../lib/machine-loader') +const operator = require('../lib/operator') + +console.log('Running diagnostics on all paired devices...\n') + +operator.getOperatorId('middleware') + .then(operatorId => { + if (!operatorId) { + throw new Error('Operator ID not found in database') + } + + return db.any('SELECT device_id, name FROM devices') + .then(devices => ({ operatorId, devices })) + }) + .then(({ operatorId, devices }) => { + if (devices.length === 0) { + console.log('No paired devices found.') + process.exit(0) + } + + const deviceIds = devices.map(d => d.device_id) + return machineLoader.batchDiagnostics(deviceIds, operatorId) + }) + .then(() => { + console.log('\n✓ Diagnostics initiated for all devices. It can take a few minutes for the results to appear on the admin.') + process.exit(0) + }) + .catch(err => { + console.error('Error:', err.message) + process.exit(1) + }) \ No newline at end of file diff --git a/packages/server/lib/machine-loader.js b/packages/server/lib/machine-loader.js index 2fd8da7c..f5fb29dc 100644 --- a/packages/server/lib/machine-loader.js +++ b/packages/server/lib/machine-loader.js @@ -523,6 +523,43 @@ function diagnostics(rec) { ) } +function batchDiagnostics(deviceIds, operatorId) { + const diagnosticsDir = `${OPERATOR_DATA_DIR}/diagnostics/` + + const removeDir = fsPromises + .rm(diagnosticsDir, { recursive: true }) + .catch(err => { + if (err.code !== 'ENOENT') { + throw err + } + }) + + const sql = `UPDATE devices + SET diagnostics_timestamp = NULL, + diagnostics_scan_updated_at = NULL, + diagnostics_front_updated_at = NULL + WHERE device_id = ANY($1)` + + // Send individual notifications for each machine + const sendNotifications = deviceIds.map(deviceId => + db.none('NOTIFY $1:name, $2', [ + 'machineAction', + JSON.stringify({ + action: 'diagnostics', + value: { + deviceId, + operatorId, + action: 'diagnostics', + }, + }), + ]), + ) + + return removeDir + .then(() => db.none(sql, [deviceIds])) + .then(() => Promise.all(sendNotifications)) +} + function setMachine(rec, operatorId) { rec.operatorId = operatorId switch (rec.action) { @@ -681,4 +718,5 @@ module.exports = { refillMachineUnits, updateDiagnostics, updateFailedQRScans, + batchDiagnostics, } diff --git a/packages/server/package.json b/packages/server/package.json index 43b00bb6..4cec4d47 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -123,7 +123,8 @@ "lamassu-eth-recovery": "./bin/lamassu-eth-recovery", "lamassu-trx-recovery": "./bin/lamassu-trx-recovery", "lamassu-update-cassettes": "./bin/lamassu-update-cassettes", - "lamassu-clean-parsed-id": "./bin/lamassu-clean-parsed-id" + "lamassu-clean-parsed-id": "./bin/lamassu-clean-parsed-id", + "lamassu-batch-diagnostics": "./bin/lamassu-batch-diagnostics" }, "scripts": { "dev": "concurrently \"npm:server\" \"npm:admin-server\"",