Create support_logs (db & api)
This commit is contained in:
parent
b7d6f3f419
commit
62d606cc80
8 changed files with 279 additions and 58 deletions
44
lib/support_logs.js
Normal file
44
lib/support_logs.js
Normal 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 }
|
||||
Loading…
Add table
Add a link
Reference in a new issue