refactor: logic and routing folders on new-admin

This commit is contained in:
Sérgio Salgado 2021-01-22 15:36:40 +00:00 committed by Josh Harvey
parent 0c7c7ccf42
commit d9e3a9e61f
10 changed files with 94 additions and 87 deletions

View file

@ -0,0 +1,19 @@
const machineLoader = require('../../machine-loader')
const { UserInputError } = require('apollo-server-express')
function getMachine (machineId) {
return machineLoader.getMachines()
.then(machines => machines.find(({ deviceId }) => deviceId === machineId))
}
function machineAction ({ deviceId, action, cashbox, cassette1, cassette2, newName }) {
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], newName }))
.then(getMachine(deviceId))
}
module.exports = { machineAction }