refactor: generalize accountProvisioned to cassettes&recyclers
This commit is contained in:
parent
be63cd6ccb
commit
3accb2b54e
1 changed files with 16 additions and 36 deletions
|
|
@ -109,61 +109,41 @@ function plugins (settings, deviceId) {
|
||||||
return tx.fiat.lte(zeroConfLimit)
|
return tx.fiat.lte(zeroConfLimit)
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeAvailableCassettes (cassettes, redeemableTxs) {
|
const accountProvisioned = (cashUnitType, cashUnits, redeemableTxs) => {
|
||||||
if (_.isEmpty(redeemableTxs)) return cassettes
|
const kons = (cashUnits, tx) => {
|
||||||
|
|
||||||
const accountProvisioned = (cassettes, 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 cassettesBills = _.flow(
|
const cashUnitsBills = _.flow(
|
||||||
_.get(['bills']),
|
_.get(['bills']),
|
||||||
_.filter(it => _.includes('cassette', it.name) && it.denomination > 0)
|
_.filter(it => _.includes(cashUnitType, it.name) && it.denomination > 0),
|
||||||
_.zip(cassettes)
|
_.zip(cashUnits),
|
||||||
)(tx)
|
)(tx)
|
||||||
|
|
||||||
const sameDenominations = ([cassette, bill]) => cassette?.denomination === bill?.denomination
|
const sameDenominations = ([cashUnit, bill]) => cashUnit?.denomination === bill?.denomination
|
||||||
if (!_.every(sameDenominations, cassettesBills))
|
if (!_.every(sameDenominations, cashUnitsBills))
|
||||||
throw new Error('Denominations don\'t add up, cassettes were changed.')
|
throw new Error(`Denominations don't add up, ${cashUnitType}s were changed.`)
|
||||||
|
|
||||||
return _.map(
|
return _.map(
|
||||||
([cassette, { provisioned }]) => _.set('count', cassette.count - provisioned, cassette),
|
([cashUnit, { provisioned }]) => _.set('count', cashUnit.count - provisioned, cashUnit),
|
||||||
cassettesBills
|
cashUnitsBills
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
cassettes = _.reduce(accountProvisioned, cassettes, redeemableTxs)
|
return _.reduce(kons, cashUnits, redeemableTxs)
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeAvailableCassettes (cassettes, redeemableTxs) {
|
||||||
|
if (_.isEmpty(redeemableTxs)) return cassettes
|
||||||
|
cassettes = accountProvisioned('cassette', cassettes, redeemableTxs)
|
||||||
if (_.some(({ count }) => count < 0, cassettes))
|
if (_.some(({ count }) => count < 0, cassettes))
|
||||||
throw new Error('Negative note count: %j', counts)
|
throw new Error('Negative note count: %j', counts)
|
||||||
|
|
||||||
return cassettes
|
return cassettes
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeAvailableRecyclers (recyclers, redeemableTxs) {
|
function computeAvailableRecyclers (recyclers, redeemableTxs) {
|
||||||
if (_.isEmpty(redeemableTxs)) return recyclers
|
if (_.isEmpty(redeemableTxs)) return recyclers
|
||||||
|
recyclers = accountProvisioned('recycler', recyclers, redeemableTxs)
|
||||||
const accountProvisioned = (recyclers, tx) => {
|
|
||||||
// cash-out-helper sends 0 as fallback value, need to filter it out as there are no '0' denominations
|
|
||||||
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
|
|
||||||
if (!_.every(sameDenominations, recyclersBills))
|
|
||||||
throw new Error('Denominations don\'t add up, recyclers were changed.')
|
|
||||||
|
|
||||||
return _.map(
|
|
||||||
([recycler, { provisioned }]) => _.set('count', recycler.count - provisioned, recycler),
|
|
||||||
recyclersBills
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
recyclers = _.reduce(accountProvisioned, recyclers, 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)
|
||||||
|
|
||||||
return recyclers
|
return recyclers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue