Merge pull request #1159 from siiky/fix/lam-396/cashbox-history-crash

fix: don't crash with batches of unpaired machines
This commit is contained in:
Rafael Taranto 2022-03-14 23:06:40 +00:00 committed by GitHub
commit 729662d980
5 changed files with 38 additions and 5 deletions

View file

@ -43,6 +43,16 @@ function getMachines () {
.then(rr => rr.map(toMachineObject)) .then(rr => rr.map(toMachineObject))
} }
function getUnpairedMachines () {
return db.any('SELECT * FROM unpaired_devices')
.then(_.map(r =>
_.flow(
_.set('deviceId', _.get('device_id', r)),
_.unset('device_id')
)(r)
))
}
function getConfig (defaultConfig) { function getConfig (defaultConfig) {
if (defaultConfig) return Promise.resolve(defaultConfig) if (defaultConfig) return Promise.resolve(defaultConfig)
@ -257,6 +267,7 @@ function getNetworkHeartbeatByDevice (deviceId) {
module.exports = { module.exports = {
getMachineName, getMachineName,
getMachines, getMachines,
getUnpairedMachines,
getMachine, getMachine,
getMachineNames, getMachineNames,
setMachine, setMachine,

View file

@ -15,7 +15,8 @@ const resolvers = {
}, },
Query: { Query: {
machines: () => machineLoader.getMachineNames(), machines: () => machineLoader.getMachineNames(),
machine: (...[, { deviceId }]) => machineLoader.getMachine(deviceId) machine: (...[, { deviceId }]) => machineLoader.getMachine(deviceId),
unpairedMachines: () => machineLoader.getUnpairedMachines()
}, },
Mutation: { Mutation: {
machineAction: (...[, { deviceId, action, cashbox, cassette1, cassette2, cassette3, cassette4, newName }, context]) => machineAction: (...[, { deviceId, action, cashbox, cassette1, cassette2, cassette3, cassette4, newName }, context]) =>

View file

@ -27,6 +27,15 @@ const typeDef = gql`
packetLoss: String packetLoss: String
} }
type UnpairedMachine {
id: ID!
deviceId: ID!
name: String
model: String
paired: Date!
unpaired: Date!
}
type MachineEvent { type MachineEvent {
id: ID id: ID
deviceId: String deviceId: String
@ -51,6 +60,7 @@ const typeDef = gql`
type Query { type Query {
machines: [Machine] @auth machines: [Machine] @auth
machine(deviceId: ID!): Machine @auth machine(deviceId: ID!): Machine @auth
unpairedMachines: [UnpairedMachine!]! @auth
} }
type Mutation { type Mutation {

View file

@ -98,6 +98,10 @@ const GET_MACHINES_AND_CONFIG = gql`
cassette4 cassette4
numberOfCassettes numberOfCassettes
} }
unpairedMachines {
id: deviceId
name
}
config config
bills(filters: $billFilters) { bills(filters: $billFilters) {
id id
@ -160,6 +164,7 @@ const CashCassettes = () => {
const [machineId, setMachineId] = useState('') const [machineId, setMachineId] = useState('')
const machines = R.path(['machines'])(data) ?? [] const machines = R.path(['machines'])(data) ?? []
const unpairedMachines = R.path(['unpairedMachines'])(data) ?? []
const config = R.path(['config'])(data) ?? {} const config = R.path(['config'])(data) ?? {}
const fillingPercentageSettings = fromNamespace('notifications', config) const fillingPercentageSettings = fromNamespace('notifications', config)
const [setCassetteBills, { error }] = useMutation(SET_CASSETTE_BILLS, { const [setCassetteBills, { error }] = useMutation(SET_CASSETTE_BILLS, {
@ -356,7 +361,10 @@ const CashCassettes = () => {
</> </>
)} )}
{showHistory && ( {showHistory && (
<CashboxHistory machines={machines} currency={fiatCurrency} /> <CashboxHistory
machines={R.concat(machines, unpairedMachines)}
currency={fiatCurrency}
/>
)} )}
<CashCassettesFooter <CashCassettesFooter
currencyCode={fiatCurrency} currencyCode={fiatCurrency}

View file

@ -161,9 +161,12 @@ const CashboxHistory = ({ machines, currency }) => {
header: 'Machine', header: 'Machine',
width: 200, width: 200,
textAlign: 'left', textAlign: 'left',
view: it => { view: R.pipe(
return R.find(R.propEq('id', it.deviceId))(machines).name R.prop('deviceId'),
} id => R.find(R.propEq('id', id), machines),
R.defaultTo({ name: <i>Unpaired device</i> }),
R.prop('name')
)
}, },
{ {
name: 'billCount', name: 'billCount',