refactor: use recyclers instead of stackers
This commit is contained in:
parent
dc52cf4414
commit
21b88f182e
37 changed files with 2868 additions and 9599 deletions
145
lib/plugins.js
145
lib/plugins.js
|
|
@ -147,42 +147,42 @@ function plugins (settings, deviceId) {
|
|||
return computedCassettes
|
||||
}
|
||||
|
||||
function computeAvailableStackers (stackers, redeemableTxs) {
|
||||
if (_.isEmpty(redeemableTxs)) return stackers
|
||||
function computeAvailableRecyclers (recyclers, redeemableTxs) {
|
||||
if (_.isEmpty(redeemableTxs)) return recyclers
|
||||
|
||||
const sumTxs = (sum, tx) => {
|
||||
// cash-out-helper sends 0 as fallback value, need to filter it out as there are no '0' denominations
|
||||
const bills = _.filter(it => _.includes('stacker', it.name) && it.denomination > 0, tx.bills)
|
||||
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(stackers, bills))
|
||||
const doDenominationsMatch = _.every(sameDenominations, _.zip(recyclers, bills))
|
||||
|
||||
if (!doDenominationsMatch) {
|
||||
throw new Error('Denominations don\'t add up, stackers were changed.')
|
||||
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(stackers)), redeemableTxs)
|
||||
const zipped = _.zip(_.map('count', stackers), provisioned)
|
||||
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)
|
||||
}
|
||||
|
||||
const computedStackers = []
|
||||
const computedRecyclers = []
|
||||
_.forEach(it => {
|
||||
computedStackers.push({
|
||||
number: stackers[it].number,
|
||||
name: stackers[it].name,
|
||||
denomination: stackers[it].denomination,
|
||||
computedRecyclers.push({
|
||||
number: recyclers[it].number,
|
||||
name: recyclers[it].name,
|
||||
denomination: recyclers[it].denomination,
|
||||
count: counts[it]
|
||||
})
|
||||
}, _.times(_.identity(), _.size(stackers)))
|
||||
}, _.times(_.identity(), _.size(recyclers)))
|
||||
|
||||
return computedStackers
|
||||
return computedRecyclers
|
||||
}
|
||||
|
||||
function buildAvailableCassettes (excludeTxId) {
|
||||
|
|
@ -231,62 +231,57 @@ function plugins (settings, deviceId) {
|
|||
})
|
||||
}
|
||||
|
||||
function buildAvailableStackers (excludeTxId) {
|
||||
function buildAvailableRecyclers (excludeTxId) {
|
||||
const cashOutConfig = configManager.getCashOut(deviceId, settings.config)
|
||||
|
||||
if (!cashOutConfig.active) return Promise.resolve()
|
||||
|
||||
return Promise.all([dbm.stackerCounts(deviceId), cashOutHelper.redeemableTxs(deviceId, excludeTxId)])
|
||||
.then(([_stackers, _redeemableTxs]) => {
|
||||
return Promise.all([dbm.recyclerCounts(deviceId), cashOutHelper.redeemableTxs(deviceId, excludeTxId)])
|
||||
.then(([_recyclers, _redeemableTxs]) => {
|
||||
const redeemableTxs = _.reject(_.matchesProperty('id', excludeTxId), _redeemableTxs)
|
||||
|
||||
const denominations = []
|
||||
_.forEach(it => {
|
||||
denominations.push([cashOutConfig[`stacker${it + 1}f`], cashOutConfig[`stacker${it + 1}r`]])
|
||||
}, _.times(_.identity(), _stackers.numberOfStackers))
|
||||
|
||||
const virtualStackers = denominations.length ? [Math.max(..._.flatten(denominations)) * 2] : []
|
||||
|
||||
const counts = _stackers.counts
|
||||
_.forEach(it => {
|
||||
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!')
|
||||
}
|
||||
|
||||
const stackers = []
|
||||
const recyclers = []
|
||||
_.forEach(it => {
|
||||
stackers.push({
|
||||
number: 1 + it * 2,
|
||||
name: `stacker${it + 1}f`,
|
||||
denomination: parseInt(denominations[it][0], 10),
|
||||
count: parseInt(counts[it][0], 10)
|
||||
recyclers.push({
|
||||
number: it + 1,
|
||||
name: `recycler${it + 1}`,
|
||||
denomination: parseInt(denominations[it], 10),
|
||||
count: parseInt(counts[it], 10)
|
||||
})
|
||||
stackers.push({
|
||||
number: 2 + it * 2,
|
||||
name: `stacker${it + 1}r`,
|
||||
denomination: parseInt(denominations[it][1], 10),
|
||||
count: parseInt(counts[it][1], 10)
|
||||
})
|
||||
}, _.times(_.identity(), _stackers.numberOfStackers))
|
||||
}, _.times(_.identity(), _recyclers.numberOfRecyclers))
|
||||
|
||||
try {
|
||||
return {
|
||||
stackers: computeAvailableStackers(stackers, redeemableTxs),
|
||||
virtualStackers
|
||||
recyclers: computeAvailableRecyclers(recyclers, redeemableTxs),
|
||||
virtualRecyclers
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
return {
|
||||
stackers,
|
||||
virtualStackers
|
||||
recyclers,
|
||||
virtualRecyclers
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function buildAvailableUnits (excludeTxId) {
|
||||
return Promise.all([buildAvailableCassettes(excludeTxId), buildAvailableStackers(excludeTxId)])
|
||||
.then(([cassettes, stackers]) => ({ cassettes: cassettes.cassettes, stackers: stackers.stackers }))
|
||||
return Promise.all([buildAvailableCassettes(excludeTxId), buildAvailableRecyclers(excludeTxId)])
|
||||
.then(([cassettes, recyclers]) => ({ cassettes: cassettes.cassettes, recyclers: recyclers.recyclers }))
|
||||
}
|
||||
|
||||
function fetchCurrentConfigVersion () {
|
||||
|
|
@ -336,7 +331,7 @@ function plugins (settings, deviceId) {
|
|||
|
||||
return Promise.all([
|
||||
buildAvailableCassettes(),
|
||||
buildAvailableStackers(),
|
||||
buildAvailableRecyclers(),
|
||||
fetchCurrentConfigVersion(),
|
||||
millisecondsToMinutes(getTimezoneOffset(localeConfig.timezone)),
|
||||
loyalty.getNumberOfAvailablePromoCodes(),
|
||||
|
|
@ -347,7 +342,7 @@ function plugins (settings, deviceId) {
|
|||
])
|
||||
.then(([
|
||||
cassettes,
|
||||
stackers,
|
||||
recyclers,
|
||||
configVersion,
|
||||
timezone,
|
||||
numberOfAvailablePromoCodes,
|
||||
|
|
@ -371,7 +366,7 @@ function plugins (settings, deviceId) {
|
|||
|
||||
return {
|
||||
cassettes,
|
||||
stackers,
|
||||
recyclers: recyclers,
|
||||
rates: buildRates(tickers),
|
||||
balances: buildBalances(balances),
|
||||
coins,
|
||||
|
|
@ -744,12 +739,12 @@ function plugins (settings, deviceId) {
|
|||
const denomination2 = cashOutConfig.cassette2
|
||||
const denomination3 = cashOutConfig.cassette3
|
||||
const denomination4 = cashOutConfig.cassette4
|
||||
const denomination1f = cashOutConfig.stacker1f
|
||||
const denomination1r = cashOutConfig.stacker1r
|
||||
const denomination2f = cashOutConfig.stacker2f
|
||||
const denomination2r = cashOutConfig.stacker2r
|
||||
const denomination3f = cashOutConfig.stacker3f
|
||||
const denomination3r = cashOutConfig.stacker3r
|
||||
const denominationRecycler1 = cashOutConfig.recycler1
|
||||
const denominationRecycler2 = cashOutConfig.recycler2
|
||||
const denominationRecycler3 = cashOutConfig.recycler3
|
||||
const denominationRecycler4 = cashOutConfig.recycler4
|
||||
const denominationRecycler5 = cashOutConfig.recycler5
|
||||
const denominationRecycler6 = cashOutConfig.recycler6
|
||||
const cashOutEnabled = cashOutConfig.active
|
||||
const isUnitLow = (have, max, limit) => cashOutEnabled && ((have / max) * 100) < limit
|
||||
// const isUnitHigh = (have, max, limit) => cashOutEnabled && ((have / max) * 100) > limit
|
||||
|
|
@ -817,74 +812,74 @@ function plugins (settings, deviceId) {
|
|||
}
|
||||
: null
|
||||
|
||||
const stacker1fAlert = device.numberOfStackers >= 1 && isUnitLow(device.cashUnits.stacker1f, getCashUnitCapacity(device.model, 'stacker'), notifications.fillingPercentageStacker1f)
|
||||
const recycler1Alert = device.numberOfRecyclers >= 1 && isUnitLow(device.cashUnits.recycler1, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler1)
|
||||
? {
|
||||
code: 'LOW_RECYCLER_STACKER',
|
||||
cassette: 4,
|
||||
machineName,
|
||||
deviceId: device.deviceId,
|
||||
notes: device.cashUnits.stacker1f,
|
||||
denomination: denomination1f,
|
||||
notes: device.cashUnits.recycler1,
|
||||
denomination: denominationRecycler1,
|
||||
fiatCode
|
||||
}
|
||||
: null
|
||||
|
||||
const stacker1rAlert = device.numberOfStackers >= 1 && isUnitLow(device.cashUnits.stacker1r, getCashUnitCapacity(device.model, 'stacker'), notifications.fillingPercentageStacker1r)
|
||||
const recycler2Alert = device.numberOfRecyclers >= 2 && isUnitLow(device.cashUnits.recycler2, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler2)
|
||||
? {
|
||||
code: 'LOW_RECYCLER_STACKER',
|
||||
cassette: 4,
|
||||
machineName,
|
||||
deviceId: device.deviceId,
|
||||
notes: device.cashUnits.stacker1r,
|
||||
denomination: denomination1r,
|
||||
notes: device.cashUnits.recycler2,
|
||||
denomination: denominationRecycler2,
|
||||
fiatCode
|
||||
}
|
||||
: null
|
||||
|
||||
const stacker2fAlert = device.numberOfStackers >= 2 && isUnitLow(device.cashUnits.stacker2f, getCashUnitCapacity(device.model, 'stacker'), notifications.fillingPercentageStacker2f)
|
||||
const recycler3Alert = device.numberOfRecyclers >= 3 && isUnitLow(device.cashUnits.recycler3, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler3)
|
||||
? {
|
||||
code: 'LOW_RECYCLER_STACKER',
|
||||
cassette: 4,
|
||||
machineName,
|
||||
deviceId: device.deviceId,
|
||||
notes: device.cashUnits.stacker2f,
|
||||
denomination: denomination2f,
|
||||
notes: device.cashUnits.recycler3,
|
||||
denomination: denominatinoRecycler3,
|
||||
fiatCode
|
||||
}
|
||||
: null
|
||||
|
||||
const stacker2rAlert = device.numberOfStackers >= 2 && isUnitLow(device.cashUnits.stacker2r, getCashUnitCapacity(device.model, 'stacker'), notifications.fillingPercentageStacker2r)
|
||||
const recycler4Alert = device.numberOfRecyclers >= 4 && isUnitLow(device.cashUnits.recycler4, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler4)
|
||||
? {
|
||||
code: 'LOW_RECYCLER_STACKER',
|
||||
cassette: 4,
|
||||
machineName,
|
||||
deviceId: device.deviceId,
|
||||
notes: device.cashUnits.stacker2r,
|
||||
denomination: denomination2r,
|
||||
notes: device.cashUnits.recycler4,
|
||||
denomination: denominationRecycler4,
|
||||
fiatCode
|
||||
}
|
||||
: null
|
||||
|
||||
const stacker3fAlert = device.numberOfStackers >= 3 && isUnitLow(device.cashUnits.stacker3f, getCashUnitCapacity(device.model, 'stacker'), notifications.fillingPercentageStacker3f)
|
||||
const recycler5Alert = device.numberOfRecyclers >= 5 && isUnitLow(device.cashUnits.recycler5, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler5)
|
||||
? {
|
||||
code: 'LOW_RECYCLER_STACKER',
|
||||
cassette: 4,
|
||||
machineName,
|
||||
deviceId: device.deviceId,
|
||||
notes: device.cashUnits.stacker3f,
|
||||
denomination: denomination3f,
|
||||
notes: device.cashUnits.recycler5,
|
||||
denomination: denominationRecycler5,
|
||||
fiatCode
|
||||
}
|
||||
: null
|
||||
|
||||
const stacker3rAlert = device.numberOfStackers >= 3 && isUnitLow(device.cashUnits.stacker3r, getCashUnitCapacity(device.model, 'stacker'), notifications.fillingPercentageStacker3r)
|
||||
const recycler6Alert = device.numberOfRecyclers >= 6 && isUnitLow(device.cashUnits.recycler6, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler6)
|
||||
? {
|
||||
code: 'LOW_RECYCLER_STACKER',
|
||||
cassette: 4,
|
||||
machineName,
|
||||
deviceId: device.deviceId,
|
||||
notes: device.cashUnits.stacker3r,
|
||||
denomination: denomination3r,
|
||||
notes: device.cashUnits.recycler6,
|
||||
denomination: denominationRecycler6,
|
||||
fiatCode
|
||||
}
|
||||
: null
|
||||
|
|
@ -895,12 +890,12 @@ function plugins (settings, deviceId) {
|
|||
cassette2Alert,
|
||||
cassette3Alert,
|
||||
cassette4Alert,
|
||||
stacker1fAlert,
|
||||
stacker1rAlert,
|
||||
stacker2fAlert,
|
||||
stacker2rAlert,
|
||||
stacker3fAlert,
|
||||
stacker3rAlert
|
||||
recycler1Alert,
|
||||
recycler2Alert,
|
||||
recycler3Alert,
|
||||
recycler4Alert,
|
||||
recycler5Alert,
|
||||
recycler6Alert
|
||||
])
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue