From deef6e52a37aba7d5e24339d4a04d2744c39fe53 Mon Sep 17 00:00:00 2001 From: Nikola Ubavic <53820106+ubavic@users.noreply.github.com> Date: Fri, 24 Dec 2021 23:30:25 +0100 Subject: [PATCH] fix: throw error on missing resource --- lib/machine-loader.js | 58 ++++----- .../src/pages/Machines/Machines.js | 117 ++++++++---------- 2 files changed, 79 insertions(+), 96 deletions(-) diff --git a/lib/machine-loader.js b/lib/machine-loader.js index b2ae3afb..5790673f 100644 --- a/lib/machine-loader.js +++ b/lib/machine-loader.js @@ -12,30 +12,35 @@ const configManager = require('./new-config-manager') const settingsLoader = require('./new-settings-loader') const notifierUtils = require('./notifier/utils') const notifierQueries = require('./notifier/queries') +const { ApolloError } = require('apollo-server-errors'); const fullyFunctionalStatus = { label: 'Fully functional', type: 'success' } const unresponsiveStatus = { label: 'Unresponsive', type: 'error' } const stuckStatus = { label: 'Stuck', type: 'error' } +function toMachineObject (r) { + return { + deviceId: r.device_id, + cashbox: r.cashbox, + cassette1: r.cassette1, + cassette2: r.cassette2, + cassette3: r.cassette3, + cassette4: r.cassette4, + numberOfCassettes: r.number_of_cassettes, + version: r.version, + model: r.model, + pairedAt: new Date(r.created), + lastPing: new Date(r.last_online), + name: r.name, + paired: r.paired + // TODO: we shall start using this JSON field at some point + // location: r.location, + } +} + function getMachines () { return db.any('SELECT * FROM devices WHERE display=TRUE ORDER BY created') - .then(rr => rr.map(r => ({ - deviceId: r.device_id, - cashbox: r.cashbox, - cassette1: r.cassette1, - cassette2: r.cassette2, - cassette3: r.cassette3, - cassette4: r.cassette4, - numberOfCassettes: r.number_of_cassettes, - version: r.version, - model: r.model, - pairedAt: new Date(r.created), - lastPing: new Date(r.last_online), - name: r.name, - // TODO: we shall start using this JSON field at some point - // location: r.location, - paired: r.paired - }))) + .then(rr => rr.map(toMachineObject)) } function getConfig (defaultConfig) { @@ -100,21 +105,10 @@ function getMachineName (machineId) { function getMachine (machineId, config) { const sql = 'SELECT * FROM devices WHERE device_id=$1' - const queryMachine = db.oneOrNone(sql, [machineId]).then(r => ({ - deviceId: r.device_id, - cashbox: r.cashbox, - cassette1: r.cassette1, - cassette2: r.cassette2, - cassette3: r.cassette3, - cassette4: r.cassette4, - numberOfCassettes: r.number_of_cassettes, - version: r.version, - model: r.model, - pairedAt: new Date(r.created), - lastPing: new Date(r.last_online), - name: r.name, - paired: r.paired - })) + const queryMachine = db.oneOrNone(sql, [machineId]).then(r => { + if (r === null) throw new ApolloError('Resource doesn\'t exist', 'NOT_FOUND') + else return toMachineObject(r) + }) return Promise.all([queryMachine, dbm.machineEvents(), config]) .then(([machine, events, config]) => { diff --git a/new-lamassu-admin/src/pages/Machines/Machines.js b/new-lamassu-admin/src/pages/Machines/Machines.js index c4b5635f..66fcf19d 100644 --- a/new-lamassu-admin/src/pages/Machines/Machines.js +++ b/new-lamassu-admin/src/pages/Machines/Machines.js @@ -50,15 +50,6 @@ const GET_INFO = gql` } ` -const GET_MACHINES = gql` - { - machines { - name - deviceId - } - } -` - const getMachineID = path => path.slice(path.lastIndexOf('/') + 1) const MachineRoute = () => { @@ -69,14 +60,15 @@ const MachineRoute = () => { const [loading, setLoading] = useState(true) - useQuery(GET_MACHINES, { + const { data, refetch } = useQuery(GET_INFO, { onCompleted: data => { - const machines = data.machines - const machineFound = machines.map(m => m.deviceId).includes(id) - - if (!machineFound) return history.push('/maintenance/machine-status') + if (data.machine === null) + return history.push('/maintenance/machine-status') setLoading(false) + }, + variables: { + deviceId: id } }) @@ -84,15 +76,14 @@ const MachineRoute = () => { return history.push(location.pathname) } - return !loading && + return ( + !loading && ( + + ) + ) } -const Machines = ({ id, reload }) => { - const { data, loading, refetch } = useQuery(GET_INFO, { - variables: { - deviceId: id - } - }) +const Machines = ({ data, refetch, reload }) => { const classes = useStyles() const timezone = R.path(['config', 'locale_timezone'], data) ?? {} @@ -104,52 +95,50 @@ const Machines = ({ id, reload }) => { const machineID = R.path(['deviceId'])(machine) ?? null return ( - !loading && ( - - - -
- }> - - - Dashboard - - - - {machineName} - - - -
-
-
- -
-
- {'Details'} -
-
-
- {'Cash cassettes'} - -
-
- {'Latest transactions'} - -
-
- {'Commissions'} - -
+ + + +
+ }> + + + Dashboard + + + + {machineName} + + +
- ) + +
+
+ {'Details'} +
+
+
+ {'Cash cassettes'} + +
+
+ {'Latest transactions'} + +
+
+ {'Commissions'} + +
+
+
+
) }