This commit is contained in:
Josh Harvey 2016-12-09 01:03:33 +02:00
parent 1b89ed5c76
commit 2355054cdc
3 changed files with 20 additions and 21 deletions

View file

@ -3,8 +3,6 @@ const R = require('ramda')
const prettyMs = require('pretty-ms')
const numeral = require('numeral')
const configManager = require('./config-manager')
const settingsLoader = require('./settings-loader')
const db = require('./postgresql_interface')
const T = require('./time')
const logger = require('./logger')
@ -77,20 +75,6 @@ function checkNotification (plugins) {
.catch(logger.error)
}
function checkBalance (rec) {
const settings = settingsLoader.settings()
const config = configManager.unscoped(settings.config)
const lowBalanceThreshold = config.lowBalanceThreshold
return lowBalanceThreshold && rec.fiatBalance < lowBalanceThreshold
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode}
: null
}
function checkBalances (plugins) {
return plugins.checkBalances()
.then(balances => R.reject(R.isNil, balances.map(checkBalance)))
}
function checkPing (deviceEvents) {
const sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents))
const lastEvent = R.last(sortedEvents)
@ -139,7 +123,7 @@ function devicesAndEvents () {
function checkStatus (plugins) {
const alerts = {devices: {}, deviceNames: {}}
return Promise.all([checkBalances(plugins), devicesAndEvents()])
return Promise.all([plugins.checkBalances(), devicesAndEvents()])
.then(([balances, rec]) => {
const devices = rec.devices
const events = rec.events

View file

@ -88,7 +88,7 @@ function plugins (settings) {
}
function fetchCurrentConfigVersion () {
const sql = `select id from config_users
const sql = `select id from user_config
where type=$1
order by id desc
limit 1`
@ -425,6 +425,14 @@ function plugins (settings) {
})
}
function checkBalance (settings, rec) {
const config = configManager.unscoped(settings.config)
const lowBalanceThreshold = config.lowBalanceThreshold
return lowBalanceThreshold && rec.fiatBalance < lowBalanceThreshold
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode}
: null
}
function checkBalances () {
return dbm.devices()
.then(devices => {
@ -438,6 +446,7 @@ function plugins (settings) {
return R.values(R.reduceBy(min, Infinity, toMarket, R.flatten(arr)))
})
})
.then(balances => R.reject(R.isNil, balances.map(balance => checkBalance)))
}
function randomCode () {

View file

@ -153,13 +153,11 @@ function ca (req, res) {
}
function pair (req, res, next) {
console.log('DEBUG100')
const token = req.query.token
const deviceId = req.deviceId
return pairing.pair(token, deviceId)
.then(valid => {
console.log('DEBUG102')
if (valid) return res.end()
throw httpError('Pairing failed')
})
@ -422,14 +420,22 @@ function populateDeviceId (req, res, next) {
}
function populateSettings (req, res, next) {
if (req.query.force_config === 'true') {
return settingsLoader.loadLatest()
.then(settings => { req.settings = settings })
.then(() => next())
.catch(next)
}
const versionId = req.headers['config-version']
if (!versionId) {
logger.debug('No config-version header')
return res.sendStatus(400)
}
settingsLoader.log(versionId)
settingsLoader.load(versionId)
.then(settings => { req.settings = settings })
.then(() => helpers.updateDeviceConfigVersion(versionId))
.then(() => next())
.catch(next)
}