update cryptoBalance notification
This commit is contained in:
parent
6759bec826
commit
e8356c1041
8 changed files with 599 additions and 378 deletions
|
|
@ -470,48 +470,70 @@ function plugins (settings, deviceId) {
|
|||
return Promise.all(promises)
|
||||
}
|
||||
|
||||
function checkDeviceBalances (_deviceId) {
|
||||
const config = configManager.machineScoped(_deviceId, settings.config)
|
||||
const cryptoCodes = config.cryptoCurrencies
|
||||
const fiatCode = config.fiatCurrency
|
||||
|
||||
const fiatBalancePromises = cryptoCodes.map(c => fiatBalance(fiatCode, c))
|
||||
|
||||
return Promise.all(fiatBalancePromises)
|
||||
.then(arr => {
|
||||
return arr.map((balance, i) => ({
|
||||
fiatBalance: balance,
|
||||
cryptoCode: cryptoCodes[i],
|
||||
fiatCode,
|
||||
_deviceId
|
||||
}))
|
||||
})
|
||||
function checkDevicesCashBalances (fiatCode, devices) {
|
||||
return _.map(device => checkDeviceCashBalances(fiatCode, device), devices)
|
||||
}
|
||||
|
||||
function checkBalance (rec) {
|
||||
const config = configManager.unscoped(settings.config)
|
||||
const lowBalanceThreshold = config.lowBalanceThreshold
|
||||
function checkDeviceCashBalances (fiatCode, device) {
|
||||
const config = configManager.machineScoped(device.deviceId, settings.config)
|
||||
const denomination1 = config.topCashOutDenomination
|
||||
const denomination2 = config.bottomCashOutDenomination
|
||||
const machineName = config.machineName
|
||||
|
||||
return rec.fiatBalance.balance <= lowBalanceThreshold
|
||||
? {code: Symbol('LOW_BALANCE'), cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode}
|
||||
const cashInAlert = device.cashbox > config.cashInAlertThreshold
|
||||
? {code: 'CASH_BOX_FULL', machineName, deviceId: device.deviceId, notes: device.cashbox}
|
||||
: null
|
||||
|
||||
const cassette1Alert = device.cassette1 < config.cashOutCassette1AlertThreshold
|
||||
? {code: 'LOW_CASH_OUT', cassette: 1, machineName, deviceId: device.deviceId,
|
||||
notes: device.cassette1, denomination: denomination1, fiatCode}
|
||||
: null
|
||||
|
||||
const cassette2Alert = device.cassette2 < config.cashOutCassette2AlertThreshold
|
||||
? {code: 'LOW_CASH_OUT', cassette: 2, machineName, deviceId: device.deviceId,
|
||||
notes: device.cassette2, denomination: denomination2, fiatCode}
|
||||
: null
|
||||
|
||||
return _.compact([cashInAlert, cassette1Alert, cassette2Alert])
|
||||
}
|
||||
|
||||
function checkCryptoBalances (fiatCode, devices) {
|
||||
const fiatBalancePromises = cryptoCodes => _.map(c => fiatBalance(fiatCode, c), cryptoCodes)
|
||||
|
||||
const fetchCryptoCodes = _deviceId => {
|
||||
const config = configManager.machineScoped(_deviceId, settings.config)
|
||||
return config.cryptoCurrencies
|
||||
}
|
||||
|
||||
const union = _.flow(_.map(fetchCryptoCodes), _.flatten, _.uniq)
|
||||
const cryptoCodes = union(devices)
|
||||
const checkCryptoBalanceWithFiat = _.partial(checkCryptoBalance, [fiatCode])
|
||||
|
||||
return Promise.all(fiatBalancePromises(cryptoCodes))
|
||||
.then(balances => _.map(checkCryptoBalanceWithFiat, _.zip(cryptoCodes, balances)))
|
||||
}
|
||||
|
||||
function checkCryptoBalance (fiatCode, rec) {
|
||||
const [cryptoCode, fiatBalance] = rec
|
||||
const config = configManager.cryptoScoped(cryptoCode, settings.config)
|
||||
const cryptoAlertThreshold = config.cryptoAlertThreshold
|
||||
|
||||
return BN(fiatBalance.balance).lt(cryptoAlertThreshold)
|
||||
? {code: 'LOW_CRYPTO_BALANCE', cryptoCode, fiatBalance, fiatCode}
|
||||
: null
|
||||
}
|
||||
|
||||
function checkBalances () {
|
||||
const globalConfig = configManager.unscoped(settings.config)
|
||||
const fiatCode = globalConfig.fiatCurrency
|
||||
|
||||
return machineLoader.getMachines()
|
||||
.then(devices => {
|
||||
const deviceIds = devices.map(r => r.deviceId)
|
||||
const deviceBalancePromises = deviceIds.map(deviceId => checkDeviceBalances(deviceId))
|
||||
|
||||
return Promise.all(deviceBalancePromises)
|
||||
.then(arr => {
|
||||
const toMarket = r => [r.fiatCode, r.cryptoCode].join('')
|
||||
const min = _.minBy(r => r.fiatBalance)
|
||||
const byMarket = _.groupBy(toMarket, _.flatten(arr))
|
||||
const minByMarket = _.flatMap(min, byMarket)
|
||||
|
||||
return _.reject(_.isNil, _.map(checkBalance, minByMarket))
|
||||
})
|
||||
return Promise.all([
|
||||
checkCryptoBalances(fiatCode, devices),
|
||||
checkDevicesCashBalances(fiatCode, devices)
|
||||
])
|
||||
.then(_.flow(_.flattenDeep, _.compact))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue