fix: throw error on missing resource
This commit is contained in:
parent
f620927f3b
commit
deef6e52a3
2 changed files with 79 additions and 96 deletions
|
|
@ -12,14 +12,14 @@ 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 getMachines () {
|
||||
return db.any('SELECT * FROM devices WHERE display=TRUE ORDER BY created')
|
||||
.then(rr => rr.map(r => ({
|
||||
function toMachineObject (r) {
|
||||
return {
|
||||
deviceId: r.device_id,
|
||||
cashbox: r.cashbox,
|
||||
cassette1: r.cassette1,
|
||||
|
|
@ -32,10 +32,15 @@ function getMachines () {
|
|||
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,
|
||||
paired: r.paired
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
function getMachines () {
|
||||
return db.any('SELECT * FROM devices WHERE display=TRUE ORDER BY created')
|
||||
.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]) => {
|
||||
|
|
|
|||
|
|
@ -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 && <Machines id={id} reload={reload}></Machines>
|
||||
return (
|
||||
!loading && (
|
||||
<Machines data={data} refetch={refetch} reload={reload}></Machines>
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
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,7 +95,6 @@ const Machines = ({ id, reload }) => {
|
|||
const machineID = R.path(['deviceId'])(machine) ?? null
|
||||
|
||||
return (
|
||||
!loading && (
|
||||
<Grid container className={classes.grid}>
|
||||
<Grid item xs={3}>
|
||||
<Grid item xs={12}>
|
||||
|
|
@ -150,7 +140,6 @@ const Machines = ({ id, reload }) => {
|
|||
</Grid>
|
||||
</Grid>
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export default MachineRoute
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue