Merge pull request #1612 from siiky/refactor/plugins.js
LAM-101 fix recycler bills retrieval + refactor `plugins.js`
This commit is contained in:
commit
b19b857d68
2 changed files with 57 additions and 101 deletions
|
|
@ -138,7 +138,7 @@ function toObj (row) {
|
||||||
|
|
||||||
const billFieldsArr = _.concat(
|
const billFieldsArr = _.concat(
|
||||||
_.map(it => ({ name: `cassette${it + 1}`, denomination: newObj[`denomination${it + 1}`], provisioned: newObj[`provisioned${it + 1}`] }))(_.range(0, MAX_CASSETTES)),
|
_.map(it => ({ name: `cassette${it + 1}`, denomination: newObj[`denomination${it + 1}`], provisioned: newObj[`provisioned${it + 1}`] }))(_.range(0, MAX_CASSETTES)),
|
||||||
_.map(it => ({ name: `recycler${it + 1}`, denomination: newObj[`denomination_recycler${it + 1}`], provisioned: newObj[`provisioned_recycler${it + 1}`] }))(_.range(0, MAX_RECYCLERS)),
|
_.map(it => ({ name: `recycler${it + 1}`, denomination: newObj[`denominationRecycler${it + 1}`], provisioned: newObj[`provisionedRecycler${it + 1}`] }))(_.range(0, MAX_RECYCLERS)),
|
||||||
)
|
)
|
||||||
|
|
||||||
// There can't be bills with denomination === 0.
|
// There can't be bills with denomination === 0.
|
||||||
|
|
|
||||||
156
lib/plugins.js
156
lib/plugins.js
|
|
@ -109,79 +109,42 @@ function plugins (settings, deviceId) {
|
||||||
return tx.fiat.lte(zeroConfLimit)
|
return tx.fiat.lte(zeroConfLimit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const accountProvisioned = (cashUnitType, cashUnits, redeemableTxs) => {
|
||||||
|
const kons = (cashUnits, tx) => {
|
||||||
|
// cash-out-helper sends 0 as fallback value, need to filter it out as there are no '0' denominations
|
||||||
|
const cashUnitsBills = _.flow(
|
||||||
|
_.get(['bills']),
|
||||||
|
_.filter(it => _.includes(cashUnitType, it.name) && it.denomination > 0),
|
||||||
|
_.zip(cashUnits),
|
||||||
|
)(tx)
|
||||||
|
|
||||||
|
const sameDenominations = ([cashUnit, bill]) => cashUnit?.denomination === bill?.denomination
|
||||||
|
if (!_.every(sameDenominations, cashUnitsBills))
|
||||||
|
throw new Error(`Denominations don't add up, ${cashUnitType}s were changed.`)
|
||||||
|
|
||||||
|
return _.map(
|
||||||
|
([cashUnit, { provisioned }]) => _.set('count', cashUnit.count - provisioned, cashUnit),
|
||||||
|
cashUnitsBills
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return _.reduce(kons, cashUnits, redeemableTxs)
|
||||||
|
}
|
||||||
|
|
||||||
function computeAvailableCassettes (cassettes, redeemableTxs) {
|
function computeAvailableCassettes (cassettes, redeemableTxs) {
|
||||||
if (_.isEmpty(redeemableTxs)) return cassettes
|
if (_.isEmpty(redeemableTxs)) return cassettes
|
||||||
|
cassettes = accountProvisioned('cassette', cassettes, redeemableTxs)
|
||||||
const sumTxs = (sum, tx) => {
|
if (_.some(({ count }) => count < 0, cassettes))
|
||||||
// cash-out-helper sends 0 as fallback value, need to filter it out as there are no '0' denominations
|
|
||||||
const bills = _.filter(it => _.includes('cassette', it.name) && it.denomination > 0, tx.bills)
|
|
||||||
const sameDenominations = a => a[0]?.denomination === a[1]?.denomination
|
|
||||||
|
|
||||||
const doDenominationsMatch = _.every(sameDenominations, _.zip(cassettes, bills))
|
|
||||||
|
|
||||||
if (!doDenominationsMatch) {
|
|
||||||
throw new Error('Denominations don\'t add up, cassettes were changed.')
|
|
||||||
}
|
|
||||||
|
|
||||||
return _.map(r => r[0] + r[1].provisioned, _.zip(sum, tx.bills))
|
|
||||||
}
|
|
||||||
|
|
||||||
const provisioned = _.reduce(sumTxs, _.times(_.constant(0), _.size(cassettes)), redeemableTxs)
|
|
||||||
const zipped = _.zip(_.map('count', cassettes), provisioned)
|
|
||||||
const counts = _.map(r => r[0] - r[1], zipped)
|
|
||||||
|
|
||||||
if (_.some(_.lt(_, 0), counts)) {
|
|
||||||
throw new Error('Negative note count: %j', counts)
|
throw new Error('Negative note count: %j', counts)
|
||||||
}
|
return cassettes
|
||||||
|
|
||||||
const computedCassettes = []
|
|
||||||
_.forEach(it => {
|
|
||||||
computedCassettes.push({
|
|
||||||
name: cassettes[it].name,
|
|
||||||
denomination: cassettes[it].denomination,
|
|
||||||
count: counts[it]
|
|
||||||
})
|
|
||||||
}, _.times(_.identity(), _.size(cassettes)))
|
|
||||||
|
|
||||||
return computedCassettes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeAvailableRecyclers (recyclers, redeemableTxs) {
|
function computeAvailableRecyclers (recyclers, redeemableTxs) {
|
||||||
if (_.isEmpty(redeemableTxs)) return recyclers
|
if (_.isEmpty(redeemableTxs)) return recyclers
|
||||||
|
recyclers = accountProvisioned('recycler', recyclers, redeemableTxs)
|
||||||
const sumTxs = (sum, tx) => {
|
if (_.some(({ count }) => count < 0, recyclers))
|
||||||
// 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 sameDenominations = a => a[0]?.denomination === a[1]?.denomination
|
|
||||||
|
|
||||||
const doDenominationsMatch = _.every(sameDenominations, _.zip(recyclers, bills))
|
|
||||||
|
|
||||||
if (!doDenominationsMatch) {
|
|
||||||
throw new Error('Denominations don\'t add up, recyclers were changed.')
|
|
||||||
}
|
|
||||||
|
|
||||||
return _.map(r => r[0] + r[1].provisioned, _.zip(sum, tx.bills))
|
|
||||||
}
|
|
||||||
|
|
||||||
const provisioned = _.reduce(sumTxs, _.times(_.constant(0), _.size(recyclers)), redeemableTxs)
|
|
||||||
const zipped = _.zip(_.map('count', recyclers), provisioned)
|
|
||||||
const counts = _.map(r => r[0] - r[1], zipped)
|
|
||||||
|
|
||||||
if (_.some(_.lt(_, 0), counts)) {
|
|
||||||
throw new Error('Negative note count: %j', counts)
|
throw new Error('Negative note count: %j', counts)
|
||||||
}
|
return recyclers
|
||||||
|
|
||||||
const computedRecyclers = []
|
|
||||||
_.forEach(it => {
|
|
||||||
computedRecyclers.push({
|
|
||||||
number: recyclers[it].number,
|
|
||||||
name: recyclers[it].name,
|
|
||||||
denomination: recyclers[it].denomination,
|
|
||||||
count: counts[it]
|
|
||||||
})
|
|
||||||
}, _.times(_.identity(), _.size(recyclers)))
|
|
||||||
|
|
||||||
return computedRecyclers
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildAvailableCassettes (excludeTxId) {
|
function buildAvailableCassettes (excludeTxId) {
|
||||||
|
|
@ -190,30 +153,27 @@ function plugins (settings, deviceId) {
|
||||||
if (!cashOutConfig.active) return Promise.resolve()
|
if (!cashOutConfig.active) return Promise.resolve()
|
||||||
|
|
||||||
return Promise.all([dbm.cassetteCounts(deviceId), cashOutHelper.redeemableTxs(deviceId, excludeTxId)])
|
return Promise.all([dbm.cassetteCounts(deviceId), cashOutHelper.redeemableTxs(deviceId, excludeTxId)])
|
||||||
.then(([_cassettes, _redeemableTxs]) => {
|
.then(([{ counts, numberOfCassettes }, redeemableTxs]) => {
|
||||||
const redeemableTxs = _.reject(_.matchesProperty('id', excludeTxId), _redeemableTxs)
|
redeemableTxs = _.reject(_.matchesProperty('id', excludeTxId), redeemableTxs)
|
||||||
|
|
||||||
const denominations = []
|
const denominations = _.map(
|
||||||
_.forEach(it => {
|
it => cashOutConfig[`cassette${it}`],
|
||||||
denominations.push(cashOutConfig[`cassette${it + 1}`])
|
_.range(1, numberOfCassettes+1)
|
||||||
}, _.times(_.identity(), _cassettes.numberOfCassettes))
|
)
|
||||||
|
|
||||||
const virtualCassettes = denominations.length ? [Math.max(...denominations) * 2] : []
|
if (counts.length !== denominations.length)
|
||||||
|
|
||||||
const counts = _cassettes.counts
|
|
||||||
|
|
||||||
if (counts.length !== denominations.length) {
|
|
||||||
throw new Error('Denominations and respective counts do not match!')
|
throw new Error('Denominations and respective counts do not match!')
|
||||||
}
|
|
||||||
|
|
||||||
const cassettes = []
|
const cassettes = _.map(
|
||||||
_.forEach(it => {
|
it => ({
|
||||||
cassettes.push({
|
|
||||||
name: `cassette${it + 1}`,
|
name: `cassette${it + 1}`,
|
||||||
denomination: parseInt(denominations[it], 10),
|
denomination: parseInt(denominations[it], 10),
|
||||||
count: parseInt(counts[it], 10)
|
count: parseInt(counts[it], 10)
|
||||||
})
|
}),
|
||||||
}, _.times(_.identity(), _cassettes.numberOfCassettes))
|
_.range(0, numberOfCassettes)
|
||||||
|
)
|
||||||
|
|
||||||
|
const virtualCassettes = denominations.length ? [Math.max(...denominations) * 2] : []
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return {
|
return {
|
||||||
|
|
@ -236,32 +196,28 @@ function plugins (settings, deviceId) {
|
||||||
if (!cashOutConfig.active) return Promise.resolve()
|
if (!cashOutConfig.active) return Promise.resolve()
|
||||||
|
|
||||||
return Promise.all([dbm.recyclerCounts(deviceId), cashOutHelper.redeemableTxs(deviceId, excludeTxId)])
|
return Promise.all([dbm.recyclerCounts(deviceId), cashOutHelper.redeemableTxs(deviceId, excludeTxId)])
|
||||||
.then(([_recyclers, _redeemableTxs]) => {
|
.then(([{ counts, numberOfRecyclers }, redeemableTxs]) => {
|
||||||
const redeemableTxs = _.reject(_.matchesProperty('id', excludeTxId), _redeemableTxs)
|
redeemableTxs = _.reject(_.matchesProperty('id', excludeTxId), redeemableTxs)
|
||||||
|
|
||||||
const denominations = []
|
const denominations = _.map(
|
||||||
|
it => cashOutConfig[`recycler${it}`],
|
||||||
|
_.range(1, numberOfRecyclers+1)
|
||||||
|
)
|
||||||
|
|
||||||
_.forEach(it => {
|
if (counts.length !== denominations.length)
|
||||||
denominations.push(cashOutConfig[`recycler${it + 1}`])
|
|
||||||
}, _.times(_.identity(), _recyclers.numberOfRecyclers))
|
|
||||||
|
|
||||||
const virtualRecyclers = denominations.length ? [Math.max(..._.flatten(denominations)) * 2] : []
|
|
||||||
|
|
||||||
const counts = _recyclers.counts
|
|
||||||
|
|
||||||
if (counts.length !== denominations.length) {
|
|
||||||
throw new Error('Denominations and respective counts do not match!')
|
throw new Error('Denominations and respective counts do not match!')
|
||||||
}
|
|
||||||
|
|
||||||
const recyclers = []
|
const recyclers = _.map(
|
||||||
_.forEach(it => {
|
it => ({
|
||||||
recyclers.push({
|
|
||||||
number: it + 1,
|
number: it + 1,
|
||||||
name: `recycler${it + 1}`,
|
name: `recycler${it + 1}`,
|
||||||
denomination: parseInt(denominations[it], 10),
|
denomination: parseInt(denominations[it], 10),
|
||||||
count: parseInt(counts[it], 10)
|
count: parseInt(counts[it], 10)
|
||||||
})
|
}),
|
||||||
}, _.times(_.identity(), _recyclers.numberOfRecyclers))
|
_.range(0, numberOfRecyclers)
|
||||||
|
)
|
||||||
|
|
||||||
|
const virtualRecyclers = denominations.length ? [Math.max(..._.flatten(denominations)) * 2] : []
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue