new admin structure

This commit is contained in:
Josh Harvey 2017-05-09 00:37:38 +03:00
parent 7d82874916
commit c2282e61b4
17 changed files with 5768 additions and 1204 deletions

View file

@ -12,8 +12,7 @@ const db = require('../db')
const options = require('../options')
const configManager = require('../config-manager')
const configValidate = require('../config-validate')
const machines = require('./machines')
const machineLoader = require('../machine-loader')
function fetchSchema () {
const schemaPath = path.resolve(options.lamassuServerPath, 'lamassu-schema.json')
@ -64,7 +63,7 @@ function getField (schema, group, fieldCode) {
return R.merge(R.pick(['cryptoScope', 'machineScope'], group), field)
}
const fetchMachines = () => machines.getMachines()
const fetchMachines = () => machineLoader.getMachines()
.then(machineList => machineList.map(r => r.deviceId))
function validateCurrentConfig () {
@ -134,7 +133,7 @@ const supportedLanguages = languageRec.supported
const languages = supportedLanguages.map(mapLanguage).filter(r => r)
function fetchData () {
return machines.getMachines()
return machineLoader.getMachineNames()
.then(machineList => ({
currencies: massageCurrencies(currencies),
cryptoCurrencies: [{crypto: 'BTC', display: 'Bitcoin'}, {crypto: 'ETH', display: 'Ethereum'}],
@ -163,36 +162,8 @@ function fetchData () {
function saveConfigGroup (results) {
if (results.values.length === 0) return fetchConfigGroup(results.groupCode)
return configValidate.ensureConstraints(results.values)
.then(fetchConfig)
.then(oldValues => {
results.values.forEach(newValue => {
const oldValueIndex = oldValues
.findIndex(old => old.fieldLocator.code === newValue.fieldLocator.code &&
old.fieldLocator.fieldScope.crypto === newValue.fieldLocator.fieldScope.crypto &&
old.fieldLocator.fieldScope.machine === newValue.fieldLocator.fieldScope.machine
)
const existingValue = oldValueIndex > -1 &&
oldValues[oldValueIndex]
if (existingValue) {
// Delete value record
if (R.isNil(newValue.fieldValue)) {
oldValues.splice(oldValueIndex, 1)
return
}
existingValue.fieldValue = newValue.fieldValue
return
}
if (!R.isNil(newValue.fieldValue)) oldValues.push(newValue)
})
return settingsLoader.save(oldValues)
.then(() => fetchConfigGroup(results.groupCode))
})
return settingsLoader.modifyConfig(results.values)
.then(() => fetchConfigGroup(results.groupCode))
}
module.exports = {

View file

@ -1,33 +0,0 @@
const db = require('../db')
const pairing = require('./pairing')
function getMachines () {
return db.any('select * from devices where display=TRUE order by name')
.then(rr => rr.map(r => ({
deviceId: r.device_id,
name: r.name,
cashbox: r.cashbox,
cassette1: r.cassette1,
cassette2: r.cassette2,
paired: r.paired
})))
}
function resetCashOutBills (rec) {
const sql = 'update devices set cassette1=$1, cassette2=$2 where device_id=$3'
return db.none(sql, [rec.cassettes[0], rec.cassettes[1], rec.deviceId])
}
function unpair (rec) {
return pairing.unpair(rec.deviceId)
}
function setMachine (rec) {
switch (rec.action) {
case 'resetCashOutBills': return resetCashOutBills(rec)
case 'unpair': return unpair(rec)
default: throw new Error('No such action: ' + rec.action)
}
}
module.exports = {getMachines, setMachine}

View file

@ -1,24 +1,32 @@
const _ = require('lodash/fp')
const moment = require('moment')
const ticker = require('../ticker')
const settingsLoader = require('../settings-loader')
const db = require('../db')
const machineLoader = require('../machine-loader')
const CONSIDERED_UP_SECS = 30
function machinesLastPing () {
const sql = `select name, min(extract(epoch from (now() - machine_events.created))) as age
from machine_events, devices
where machine_events.device_id = devices.device_id
and devices.paired
group by name`
const sql = `select min(extract(epoch from (now() - created))) as age
from machine_events
group by device_id`
return db.any(sql)
.then(r => {
if (r.length === 0) return 'No paired machines'
return Promise.all([machineLoader.getMachineNames(), db.any(sql)])
.then(([machines, events]) => {
if (machines.length === 0) return 'No paired machines'
const addName = event => {
const machine = _.find(['deviceId', event.deviceId], machines)
if (!machine) return null
return _.set('name', machine.name, event)
}
const mapper = _.flow(_.filter(row => row.age > CONSIDERED_UP_SECS), _.map(addName), _.compact)
const downRows = mapper(events)
const downRows = r.filter(row => row.age > CONSIDERED_UP_SECS)
if (downRows.length === 0) return 'All machines are up'
if (downRows.length === 1) {

View file

@ -1,21 +1,34 @@
const _ = require('lodash/fp')
const db = require('../db')
const machineLoader = require('../machine-loader')
const NUM_RESULTS = 20
function addNames (txs) {
return machineLoader.getMachineNames()
.then(machines => {
const addName = tx => {
const machine = _.find(['deviceId', tx.deviceId], machines)
const name = machine ? machine.name : 'Unpaired'
return _.set('machineName', name, tx)
}
return _.map(addName, txs)
})
}
function batch () {
const camelize = _.mapKeys(_.camelCase)
const packager = _.flow(_.flatten, _.orderBy(_.property('created'), ['desc']), _.take(NUM_RESULTS), _.map(camelize))
const packager = _.flow(_.flatten, _.orderBy(_.property('created'), ['desc']),
_.take(NUM_RESULTS), _.map(camelize), addNames)
const cashInSql = `select 'cashIn' as tx_class, devices.name as machine_name, cash_in_txs.*
from cash_in_txs, devices
where devices.device_id=cash_in_txs.device_id
const cashInSql = `select 'cashIn' as tx_class, cash_in_txs.*
from cash_in_txs
order by created desc limit $1`
const cashOutSql = `select 'cashOut' as tx_class, devices.name as machine_name, cash_out_txs.*
from cash_out_txs, devices
where devices.device_id=cash_out_txs.device_id
const cashOutSql = `select 'cashOut' as tx_class, cash_out_txs.*
from cash_out_txs
order by created desc limit $1`
return Promise.all([db.any(cashInSql, [NUM_RESULTS]), db.any(cashOutSql, [NUM_RESULTS])])