refactor: inline computations into reduce
This commit is contained in:
parent
d47101215f
commit
ca706c24a9
1 changed files with 12 additions and 12 deletions
|
|
@ -149,25 +149,25 @@ function plugins (settings, deviceId) {
|
||||||
function computeAvailableRecyclers (recyclers, redeemableTxs) {
|
function computeAvailableRecyclers (recyclers, redeemableTxs) {
|
||||||
if (_.isEmpty(redeemableTxs)) return recyclers
|
if (_.isEmpty(redeemableTxs)) return recyclers
|
||||||
|
|
||||||
const sumTxs = (sum, tx) => {
|
const accountProvisioned = (recyclers, tx) => {
|
||||||
// cash-out-helper sends 0 as fallback value, need to filter it out as there are no '0' denominations
|
// cash-out-helper sends 0 as fallback value, need to filter it out as there are no '0' denominations
|
||||||
const bills = _.filter(it => _.includes('recycler', it.name) && it.denomination > 0, tx.bills)
|
const recyclersBills = _.flow(
|
||||||
|
_.get(['bills']),
|
||||||
|
_.filter(it => _.includes('recycler', it.name) && it.denomination > 0)
|
||||||
|
_.zip(recyclers)
|
||||||
|
)(tx)
|
||||||
|
|
||||||
const sameDenominations = ([recycler, bill]) => recycler?.denomination === bill?.denomination
|
const sameDenominations = ([recycler, bill]) => recycler?.denomination === bill?.denomination
|
||||||
if (!_.every(sameDenominations, _.zip(recyclers, bills))) {
|
if (!_.every(sameDenominations, recyclersBills))
|
||||||
throw new Error('Denominations don\'t add up, recyclers were changed.')
|
throw new Error('Denominations don\'t add up, recyclers were changed.')
|
||||||
}
|
|
||||||
|
|
||||||
return _.map(([acc, bill]) => acc + bill.provisioned, _.zip(sum, bills))
|
return _.map(
|
||||||
|
([recycler, { provisioned }]) => _.set('count', recycler.count - provisioned, recycler),
|
||||||
|
recyclersBills
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
recyclers = _.flow(
|
recyclers = _.reduce(accountProvisioned, recyclers, redeemableTxs)
|
||||||
// Sum "used" bills per recycler
|
|
||||||
_.reduce(sumTxs, _.map(_.constant(0), recyclers)),
|
|
||||||
_.zip(recyclers),
|
|
||||||
// Remove used bills from recycler counts
|
|
||||||
_.map(([recycler, provisioned]) => _.set('count', recycler.count - provisioned, recycler)),
|
|
||||||
)(redeemableTxs)
|
|
||||||
|
|
||||||
if (_.some(({ count }) => count < 0, recyclers))
|
if (_.some(({ count }) => count < 0, recyclers))
|
||||||
throw new Error('Negative note count: %j', counts)
|
throw new Error('Negative note count: %j', counts)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue