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