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,30 +12,35 @@ const configManager = require('./new-config-manager')
|
||||||
const settingsLoader = require('./new-settings-loader')
|
const settingsLoader = require('./new-settings-loader')
|
||||||
const notifierUtils = require('./notifier/utils')
|
const notifierUtils = require('./notifier/utils')
|
||||||
const notifierQueries = require('./notifier/queries')
|
const notifierQueries = require('./notifier/queries')
|
||||||
|
const { ApolloError } = require('apollo-server-errors');
|
||||||
|
|
||||||
const fullyFunctionalStatus = { label: 'Fully functional', type: 'success' }
|
const fullyFunctionalStatus = { label: 'Fully functional', type: 'success' }
|
||||||
const unresponsiveStatus = { label: 'Unresponsive', type: 'error' }
|
const unresponsiveStatus = { label: 'Unresponsive', type: 'error' }
|
||||||
const stuckStatus = { label: 'Stuck', 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 () {
|
function getMachines () {
|
||||||
return db.any('SELECT * FROM devices WHERE display=TRUE ORDER BY created')
|
return db.any('SELECT * FROM devices WHERE display=TRUE ORDER BY created')
|
||||||
.then(rr => rr.map(r => ({
|
.then(rr => rr.map(toMachineObject))
|
||||||
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
|
|
||||||
})))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getConfig (defaultConfig) {
|
function getConfig (defaultConfig) {
|
||||||
|
|
@ -100,21 +105,10 @@ function getMachineName (machineId) {
|
||||||
|
|
||||||
function getMachine (machineId, config) {
|
function getMachine (machineId, config) {
|
||||||
const sql = 'SELECT * FROM devices WHERE device_id=$1'
|
const sql = 'SELECT * FROM devices WHERE device_id=$1'
|
||||||
const queryMachine = db.oneOrNone(sql, [machineId]).then(r => ({
|
const queryMachine = db.oneOrNone(sql, [machineId]).then(r => {
|
||||||
deviceId: r.device_id,
|
if (r === null) throw new ApolloError('Resource doesn\'t exist', 'NOT_FOUND')
|
||||||
cashbox: r.cashbox,
|
else return toMachineObject(r)
|
||||||
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
|
|
||||||
}))
|
|
||||||
|
|
||||||
return Promise.all([queryMachine, dbm.machineEvents(), config])
|
return Promise.all([queryMachine, dbm.machineEvents(), config])
|
||||||
.then(([machine, events, 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 getMachineID = path => path.slice(path.lastIndexOf('/') + 1)
|
||||||
|
|
||||||
const MachineRoute = () => {
|
const MachineRoute = () => {
|
||||||
|
|
@ -69,14 +60,15 @@ const MachineRoute = () => {
|
||||||
|
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
|
|
||||||
useQuery(GET_MACHINES, {
|
const { data, refetch } = useQuery(GET_INFO, {
|
||||||
onCompleted: data => {
|
onCompleted: data => {
|
||||||
const machines = data.machines
|
if (data.machine === null)
|
||||||
const machineFound = machines.map(m => m.deviceId).includes(id)
|
return history.push('/maintenance/machine-status')
|
||||||
|
|
||||||
if (!machineFound) return history.push('/maintenance/machine-status')
|
|
||||||
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
},
|
||||||
|
variables: {
|
||||||
|
deviceId: id
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -84,15 +76,14 @@ const MachineRoute = () => {
|
||||||
return history.push(location.pathname)
|
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 Machines = ({ data, refetch, reload }) => {
|
||||||
const { data, loading, refetch } = useQuery(GET_INFO, {
|
|
||||||
variables: {
|
|
||||||
deviceId: id
|
|
||||||
}
|
|
||||||
})
|
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
const timezone = R.path(['config', 'locale_timezone'], data) ?? {}
|
const timezone = R.path(['config', 'locale_timezone'], data) ?? {}
|
||||||
|
|
@ -104,52 +95,50 @@ const Machines = ({ id, reload }) => {
|
||||||
const machineID = R.path(['deviceId'])(machine) ?? null
|
const machineID = R.path(['deviceId'])(machine) ?? null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
!loading && (
|
<Grid container className={classes.grid}>
|
||||||
<Grid container className={classes.grid}>
|
<Grid item xs={3}>
|
||||||
<Grid item xs={3}>
|
<Grid item xs={12}>
|
||||||
<Grid item xs={12}>
|
<div className={classes.breadcrumbsContainer}>
|
||||||
<div className={classes.breadcrumbsContainer}>
|
<Breadcrumbs separator={<NavigateNextIcon fontSize="small" />}>
|
||||||
<Breadcrumbs separator={<NavigateNextIcon fontSize="small" />}>
|
<Link to="/dashboard" className={classes.breadcrumbLink}>
|
||||||
<Link to="/dashboard" className={classes.breadcrumbLink}>
|
<Label3 noMargin className={classes.subtitle}>
|
||||||
<Label3 noMargin className={classes.subtitle}>
|
Dashboard
|
||||||
Dashboard
|
</Label3>
|
||||||
</Label3>
|
</Link>
|
||||||
</Link>
|
<TL2 noMargin className={classes.subtitle}>
|
||||||
<TL2 noMargin className={classes.subtitle}>
|
{machineName}
|
||||||
{machineName}
|
</TL2>
|
||||||
</TL2>
|
</Breadcrumbs>
|
||||||
</Breadcrumbs>
|
<Overview data={machine} onActionSuccess={reload} />
|
||||||
<Overview data={machine} onActionSuccess={reload} />
|
|
||||||
</div>
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
<Grid item xs={9}>
|
|
||||||
<div className={classes.content}>
|
|
||||||
<div
|
|
||||||
className={classnames(classes.detailItem, classes.detailsMargin)}>
|
|
||||||
<TL1 className={classes.subtitle}>{'Details'}</TL1>
|
|
||||||
<Details data={machine} timezone={timezone} />
|
|
||||||
</div>
|
|
||||||
<div className={classes.detailItem}>
|
|
||||||
<TL1 className={classes.subtitle}>{'Cash cassettes'}</TL1>
|
|
||||||
<Cassettes
|
|
||||||
refetchData={refetch}
|
|
||||||
machine={machine}
|
|
||||||
config={config ?? false}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className={classes.transactionsItem}>
|
|
||||||
<TL1 className={classes.subtitle}>{'Latest transactions'}</TL1>
|
|
||||||
<Transactions id={machineID} />
|
|
||||||
</div>
|
|
||||||
<div className={classes.detailItem}>
|
|
||||||
<TL1 className={classes.subtitle}>{'Commissions'}</TL1>
|
|
||||||
<Commissions name={'commissions'} id={machineID} />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
)
|
<Grid item xs={9}>
|
||||||
|
<div className={classes.content}>
|
||||||
|
<div
|
||||||
|
className={classnames(classes.detailItem, classes.detailsMargin)}>
|
||||||
|
<TL1 className={classes.subtitle}>{'Details'}</TL1>
|
||||||
|
<Details data={machine} timezone={timezone} />
|
||||||
|
</div>
|
||||||
|
<div className={classes.detailItem}>
|
||||||
|
<TL1 className={classes.subtitle}>{'Cash cassettes'}</TL1>
|
||||||
|
<Cassettes
|
||||||
|
refetchData={refetch}
|
||||||
|
machine={machine}
|
||||||
|
config={config ?? false}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className={classes.transactionsItem}>
|
||||||
|
<TL1 className={classes.subtitle}>{'Latest transactions'}</TL1>
|
||||||
|
<Transactions id={machineID} />
|
||||||
|
</div>
|
||||||
|
<div className={classes.detailItem}>
|
||||||
|
<TL1 className={classes.subtitle}>{'Commissions'}</TL1>
|
||||||
|
<Commissions name={'commissions'} id={machineID} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue