diff --git a/lib/machine-loader.js b/lib/machine-loader.js index fe80e0109..126407efd 100644 --- a/lib/machine-loader.js +++ b/lib/machine-loader.js @@ -207,8 +207,10 @@ function restartServices (rec) { )]) } -function setMachine (rec, operatorId) { +function setMachine (rec, operatorId, userId) { rec.operatorId = operatorId + rec.userId = userId + logMachineAction(rec) switch (rec.action) { case 'rename': return renameMachine(rec) case 'emptyCashInBills': return emptyCashInBills(rec) @@ -329,6 +331,16 @@ function assignLocation (machineId, locationId) { return db.none(sql, [locationId, machineId]) } +function logMachineAction (rec) { + const userId = rec.userId + const deviceId = rec.deviceId + const action = rec.action + const values = _.omit(['userId', 'operatorId', 'deviceId', 'action'], rec) + const sql = `INSERT INTO machine_action_logs (id, device_id, action, values, performed_by) VALUES ($1, $2, $3, $4, $5)` + // console.log([uuid.v4(), deviceId, _.kebabCase(action), values, userId]) + return db.none(sql, [uuid.v4(), deviceId, _.kebabCase(action), values, userId]) +} + module.exports = { getMachineName, getMachines, diff --git a/lib/new-admin/services/machines.js b/lib/new-admin/services/machines.js index 84ee08e6a..67c204300 100644 --- a/lib/new-admin/services/machines.js +++ b/lib/new-admin/services/machines.js @@ -8,12 +8,13 @@ function getMachine (machineId) { function machineAction ({ deviceId, action, cashbox, cassette1, cassette2, cassette3, cassette4, newName, location }, context) { const operatorId = context.res.locals.operatorId + const userId = context.req.session.user.id return getMachine(deviceId) .then(machine => { if (!machine) throw new UserInputError(`machine:${deviceId} not found`, { deviceId }) return machine }) - .then(machineLoader.setMachine({ deviceId, action, cashbox, cassettes: [cassette1, cassette2, cassette3, cassette4], newName, location }, operatorId)) + .then(machineLoader.setMachine({ deviceId, action, cashbox, cassettes: [cassette1, cassette2, cassette3, cassette4], newName, location }, operatorId, userId)) .then(getMachine(deviceId)) } diff --git a/migrations/1664748434695-machine-actions-auditing.js b/migrations/1664748434695-machine-actions-auditing.js new file mode 100644 index 000000000..15fb222d1 --- /dev/null +++ b/migrations/1664748434695-machine-actions-auditing.js @@ -0,0 +1,33 @@ +var db = require('./db') + +exports.up = function (next) { + var sql = [ + `CREATE TYPE machine_action AS ENUM ( + 'rename', + 'empty-cash-in-bills', + 'reset-cash-out-bills', + 'set-cassette-bills', + 'unpair', + 'reboot', + 'shutdown', + 'restart-services', + 'edit-location', + 'delete-location', + 'create-location' + )`, + `CREATE TABLE machine_action_logs ( + id UUID PRIMARY KEY, + device_id TEXT NOT NULL REFERENCES devices(device_id), + action machine_action NOT NULL, + values JSONB NOT NULL, + performed_by UUID NOT NULL REFERENCES users(id), + performed_at TIMESTAMPTZ NOT NULL DEFAULT now() + )` + ] + + db.multi(sql, next) +} + +exports.down = function (next) { + next() +}