Skip to content

Commit

Permalink
feat: add machine action auditing
Browse files Browse the repository at this point in the history
  • Loading branch information
chaotixkilla committed Oct 2, 2022
1 parent ac34ab3 commit 8e8bf09
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/machine-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/new-admin/services/machines.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
33 changes: 33 additions & 0 deletions migrations/1664748434695-machine-actions-auditing.js
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 8e8bf09

Please sign in to comment.