Create support_logs (db & api)

This commit is contained in:
goga-m 2017-10-27 17:36:40 +03:00 committed by Josh Harvey
parent b7d6f3f419
commit 62d606cc80
8 changed files with 279 additions and 58 deletions

View file

@ -29,6 +29,7 @@ const server = require('./server')
const transactions = require('./transactions')
const customers = require('../customers')
const logs = require('../logs')
const supportLogs = require('../support_logs')
const funding = require('./funding')
const _ = require('lodash/fp')
@ -202,6 +203,43 @@ app.get('/api/logs', (req, res, next) => {
.catch(next)
})
/**
* Get support_logs for the last 2 days
*
* @name get
* @function
* @async
*
* @param {string} '/api/support_logs/ URl to handle
* @param {object} req Request object
* @param {object} res Response object
* @param {function} next Callback
*/
app.get('/api/support_logs/:timestamp', (req, res, next) => {
const timestamp = req.params.timestamp || new Date().toISOString()
return supportLogs.batch(timestamp)
.then(r => res.send(r))
.catch(next)
})
/**
* Create a new support log
*
* @name post
* @function
* @async
*
* @param {string} '/api/support_logs' URL Endpoint
* @param {object} req Request object
* @param {object} res Response object
* @param {function} next Callback
*/
app.post('/api/support_logs', (req, res, next) => {
return supportLogs.insert(req.query.deviceId)
.then(r => res.send(r))
.catch(next)
})
/**
* Endpoint for patching customer's data
*

View file

@ -3,9 +3,7 @@ const _ = require('lodash/fp')
const db = require('./db')
const pgp = require('pg-promise')()
const settingsLoader = require('./settings-loader')
const configManager = require('./config-manager')
const getMachineName = require('./machine-loader').getMachineName
const NUM_RESULTS = 1000
/**
@ -79,22 +77,4 @@ function getMachineLogs (deviceId) {
}))
}
/**
* Given the machine id, get the machine name
*
* @name getMachineName
* @function
* @async
*
* @param {string} machineId machine id
* @returns {string} machine name
*/
function getMachineName (machineId) {
return settingsLoader.loadRecentConfig()
.then(config => {
const machineScoped = configManager.machineScoped(machineId, config)
return machineScoped.machineName
})
}
module.exports = { getMachineLogs, update, getLastSeen }

View file

@ -6,7 +6,7 @@ const pairing = require('./pairing')
const configManager = require('./config-manager')
const settingsLoader = require('./settings-loader')
module.exports = {getMachines, getMachineNames, setMachine}
module.exports = {getMachineName, getMachines, getMachineNames, setMachine}
function getMachines () {
return db.any('select * from devices where display=TRUE order by created')
@ -40,6 +40,24 @@ function getMachineNames (config) {
})
}
/**
* Given the machine id, get the machine name
*
* @name getMachineName
* @function
* @async
*
* @param {string} machineId machine id
* @returns {string} machine name
*/
function getMachineName (machineId) {
return settingsLoader.loadRecentConfig()
.then(config => {
const machineScoped = configManager.machineScoped(machineId, config)
return machineScoped.machineName
})
}
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])

View file

@ -46,12 +46,12 @@ function pair (token, deviceId, machineModel) {
.then(r => {
if (r.expired) return false
const insertSql = `insert into devices (device_id) values ($1)
const insertSql = `insert into devices (device_id, name) values ($1, $2)
on conflict (device_id)
do update set paired=TRUE, display=TRUE`
return configureNewDevice(deviceId, r.name, machineModel)
.then(() => db.none(insertSql, [deviceId]))
.then(() => db.none(insertSql, [deviceId, r.name]))
.then(() => true)
})
.catch(err => {

44
lib/support_logs.js Normal file
View file

@ -0,0 +1,44 @@
const _ = require('lodash/fp')
const uuid = require('uuid')
const db = require('./db')
/**
* Insert a single support_logs row in db
*
* @name insert
* @function
* @async
*
* @param {string} deviceId Machine's id for the log
*
* @returns {object} Newly created support_log
*/
function insert (deviceId) {
const sql = `insert into support_logs
(id, device_id) values ($1, $2) returning *`
return db.one(sql, [uuid.v4(), deviceId])
.then(_.mapKeys(_.camelCase))
}
/**
* Get the latest 48-hour logs
*
* @name batch
* @function
* @async
*
* @param {string} deviceId Machine's id
* @param {date} timestamp Fetch the last 48-hour logs before this timestamp
*
* @returns {array} List of all support_logs rows
*/
function batch (timestamp) {
const sql = `select * from support_logs
where timestamp > $1 - interval '2 days'
order by timestamp desc`
return db.oneOrNone(sql, [timestamp])
.then(_.map(_.mapKeys(_.camelCase)))
}
module.exports = { insert, batch }