refactor: deduplicate balance alerts checking code
This commit is contained in:
parent
f597c0e4f8
commit
7a7292d2fa
1 changed files with 55 additions and 148 deletions
197
lib/plugins.js
197
lib/plugins.js
|
|
@ -682,166 +682,73 @@ function plugins (settings, deviceId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkDeviceCashBalances (fiatCode, device) {
|
function checkDeviceCashBalances (fiatCode, device) {
|
||||||
const cashOutConfig = configManager.getCashOut(device.deviceId, settings.config)
|
const deviceId = device.deviceId
|
||||||
const denomination1 = cashOutConfig.cassette1
|
|
||||||
const denomination2 = cashOutConfig.cassette2
|
|
||||||
const denomination3 = cashOutConfig.cassette3
|
|
||||||
const denomination4 = cashOutConfig.cassette4
|
|
||||||
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 notifications = configManager.getNotifications(null, device.deviceId, settings.config)
|
|
||||||
|
|
||||||
const machineName = device.name
|
const machineName = device.name
|
||||||
|
const notifications = configManager.getNotifications(null, deviceId, settings.config)
|
||||||
|
|
||||||
const cashInAlert = device.cashUnits.cashbox > notifications.cashInAlertThreshold
|
const cashInAlerts = device.cashUnits.cashbox > notifications.cashInAlertThreshold
|
||||||
? {
|
? [{
|
||||||
code: 'CASH_BOX_FULL',
|
code: 'CASH_BOX_FULL',
|
||||||
machineName,
|
machineName,
|
||||||
deviceId: device.deviceId,
|
deviceId,
|
||||||
notes: device.cashUnits.cashbox
|
notes: device.cashUnits.cashbox
|
||||||
}
|
}]
|
||||||
: null
|
: []
|
||||||
|
|
||||||
const cassette1Alert = device.numberOfCassettes >= 1 && isUnitLow(device.cashUnits.cassette1, getCashUnitCapacity(device.model, 'cassette'), notifications.fillingPercentageCassette1)
|
const cashOutConfig = configManager.getCashOut(deviceId, settings.config)
|
||||||
? {
|
const cashOutEnabled = cashOutConfig.active
|
||||||
|
const isUnitLow = (have, max, limit) => ((have / max) * 100) < limit
|
||||||
|
|
||||||
|
if (!cashOutEnabled)
|
||||||
|
return cashInAlerts
|
||||||
|
|
||||||
|
const cassetteCapacity = getCashUnitCapacity(device.model, 'cassette')
|
||||||
|
const cassetteAlerts = Array(Math.min(device.numberOfCassettes ?? 0, 4))
|
||||||
|
.fill(null)
|
||||||
|
.flatMap((_elem, idx) => {
|
||||||
|
const nth = idx + 1
|
||||||
|
const cassetteField = `cassette${nth}`
|
||||||
|
const notes = device.cashUnits[cassetteField]
|
||||||
|
const denomination = cashOutConfig[cassetteField]
|
||||||
|
|
||||||
|
const limit = notifications[`fillingPercentageCassette${nth}`]
|
||||||
|
return isUnitLow(notes, cassetteCapacity, limit) ?
|
||||||
|
[{
|
||||||
code: 'LOW_CASH_OUT',
|
code: 'LOW_CASH_OUT',
|
||||||
cassette: 1,
|
cassette: nth,
|
||||||
machineName,
|
machineName,
|
||||||
deviceId: device.deviceId,
|
deviceId,
|
||||||
notes: device.cashUnits.cassette1,
|
notes,
|
||||||
denomination: denomination1,
|
denomination,
|
||||||
fiatCode
|
fiatCode
|
||||||
}
|
}] :
|
||||||
: null
|
[]
|
||||||
|
})
|
||||||
|
|
||||||
const cassette2Alert = device.numberOfCassettes >= 2 && isUnitLow(device.cashUnits.cassette2, getCashUnitCapacity(device.model, 'cassette'), notifications.fillingPercentageCassette2)
|
const recyclerCapacity = getCashUnitCapacity(device.model, 'recycler')
|
||||||
? {
|
const recyclerAlerts = Array(Math.min(device.numberOfRecyclers ?? 0, 6))
|
||||||
code: 'LOW_CASH_OUT',
|
.fill(null)
|
||||||
cassette: 2,
|
.flatMap((_elem, idx) => {
|
||||||
machineName,
|
const nth = idx + 1
|
||||||
deviceId: device.deviceId,
|
const recyclerField = `recycler${nth}`
|
||||||
notes: device.cashUnits.cassette2,
|
const notes = device.cashUnits[recyclerField]
|
||||||
denomination: denomination2,
|
const denomination = cashOutConfig[recyclerField]
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
const cassette3Alert = device.numberOfCassettes >= 3 && isUnitLow(device.cashUnits.cassette3, getCashUnitCapacity(device.model, 'cassette'), notifications.fillingPercentageCassette3)
|
const limit = notifications[`fillingPercentageRecycler${nth}`]
|
||||||
? {
|
return isUnitLow(notes, recyclerCapacity, limit) ?
|
||||||
code: 'LOW_CASH_OUT',
|
[{
|
||||||
cassette: 3,
|
|
||||||
machineName,
|
|
||||||
deviceId: device.deviceId,
|
|
||||||
notes: device.cashUnits.cassette3,
|
|
||||||
denomination: denomination3,
|
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
const cassette4Alert = device.numberOfCassettes >= 4 && isUnitLow(device.cashUnits.cassette4, getCashUnitCapacity(device.model, 'cassette'), notifications.fillingPercentageCassette4)
|
|
||||||
? {
|
|
||||||
code: 'LOW_CASH_OUT',
|
|
||||||
cassette: 4,
|
|
||||||
machineName,
|
|
||||||
deviceId: device.deviceId,
|
|
||||||
notes: device.cashUnits.cassette4,
|
|
||||||
denomination: denomination4,
|
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
const recycler1Alert = device.numberOfRecyclers >= 1 && isUnitLow(device.cashUnits.recycler1, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler1)
|
|
||||||
? {
|
|
||||||
code: 'LOW_RECYCLER_STACKER',
|
code: 'LOW_RECYCLER_STACKER',
|
||||||
cassette: 4,
|
cassette: nth, // @see DETAIL_TEMPLATE in /lib/notifier/utils.js
|
||||||
machineName,
|
machineName,
|
||||||
deviceId: device.deviceId,
|
deviceId,
|
||||||
notes: device.cashUnits.recycler1,
|
notes,
|
||||||
denomination: denominationRecycler1,
|
denomination,
|
||||||
fiatCode
|
fiatCode
|
||||||
}
|
}] :
|
||||||
: null
|
[]
|
||||||
|
})
|
||||||
|
|
||||||
const recycler2Alert = device.numberOfRecyclers >= 2 && isUnitLow(device.cashUnits.recycler2, getCashUnitCapacity(device.model, 'recycler'), notifications.fillingPercentageRecycler2)
|
return [].concat(cashInAlerts, cassetteAlerts, recyclerAlerts)
|
||||||
? {
|
|
||||||
code: 'LOW_RECYCLER_STACKER',
|
|
||||||
cassette: 4,
|
|
||||||
machineName,
|
|
||||||
deviceId: device.deviceId,
|
|
||||||
notes: device.cashUnits.recycler2,
|
|
||||||
denomination: denominationRecycler2,
|
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
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.recycler3,
|
|
||||||
denomination: denominationRecycler3,
|
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
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.recycler4,
|
|
||||||
denomination: denominationRecycler4,
|
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
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.recycler5,
|
|
||||||
denomination: denominationRecycler5,
|
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
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.recycler6,
|
|
||||||
denomination: denominationRecycler6,
|
|
||||||
fiatCode
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
|
|
||||||
return _.compact([
|
|
||||||
cashInAlert,
|
|
||||||
cassette1Alert,
|
|
||||||
cassette2Alert,
|
|
||||||
cassette3Alert,
|
|
||||||
cassette4Alert,
|
|
||||||
recycler1Alert,
|
|
||||||
recycler2Alert,
|
|
||||||
recycler3Alert,
|
|
||||||
recycler4Alert,
|
|
||||||
recycler5Alert,
|
|
||||||
recycler6Alert
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkCryptoBalances (fiatCode, devices) {
|
function checkCryptoBalances (fiatCode, devices) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue