chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -12,7 +12,7 @@ const CA_PATH = process.env.CA_PATH
|
|||
const DEFAULT_NUMBER_OF_CASSETTES = 2
|
||||
const DEFAULT_NUMBER_OF_RECYCLERS = 0
|
||||
|
||||
function pullToken (token) {
|
||||
function pullToken(token) {
|
||||
const sql = `delete from pairing_tokens
|
||||
where token=$1
|
||||
returning name, created < now() - interval '1 hour' as expired`
|
||||
|
|
@ -20,24 +20,41 @@ function pullToken (token) {
|
|||
}
|
||||
|
||||
// TODO new-admin: We should remove all configs related to that device. This can get tricky.
|
||||
function unpair (deviceId) {
|
||||
return db.tx(t =>
|
||||
t.none(`INSERT INTO unpaired_devices(id, device_id, name, model, paired, unpaired)
|
||||
function unpair(deviceId) {
|
||||
return db.tx(t =>
|
||||
t
|
||||
.none(
|
||||
`INSERT INTO unpaired_devices(id, device_id, name, model, paired, unpaired)
|
||||
SELECT $1, $2, d.name, d.model, d.created, now()
|
||||
FROM devices d
|
||||
WHERE device_id=$2`
|
||||
, [uuid.v4(), deviceId])
|
||||
.then(() => {
|
||||
WHERE device_id=$2`,
|
||||
[uuid.v4(), deviceId],
|
||||
)
|
||||
.then(() => {
|
||||
const q1 = t.none(`DELETE FROM devices WHERE device_id=$1`, [deviceId])
|
||||
const q2 = t.none(`DELETE FROM machine_pings WHERE device_id=$1`, [deviceId])
|
||||
const q3 = t.none(`DELETE FROM machine_network_heartbeat WHERE device_id=$1`, [deviceId])
|
||||
const q4 = t.none(`DELETE FROM machine_network_performance WHERE device_id=$1`, [deviceId])
|
||||
const q2 = t.none(`DELETE FROM machine_pings WHERE device_id=$1`, [
|
||||
deviceId,
|
||||
])
|
||||
const q3 = t.none(
|
||||
`DELETE FROM machine_network_heartbeat WHERE device_id=$1`,
|
||||
[deviceId],
|
||||
)
|
||||
const q4 = t.none(
|
||||
`DELETE FROM machine_network_performance WHERE device_id=$1`,
|
||||
[deviceId],
|
||||
)
|
||||
return t.batch([q1, q2, q3, q4])
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
function pair (token, deviceId, machineModel, numOfCassettes = DEFAULT_NUMBER_OF_CASSETTES, numOfRecyclers = DEFAULT_NUMBER_OF_RECYCLERS) {
|
||||
function pair(
|
||||
token,
|
||||
deviceId,
|
||||
machineModel,
|
||||
numOfCassettes = DEFAULT_NUMBER_OF_CASSETTES,
|
||||
numOfRecyclers = DEFAULT_NUMBER_OF_RECYCLERS,
|
||||
) {
|
||||
return pullToken(token)
|
||||
.then(r => {
|
||||
if (r.expired) return false
|
||||
|
|
@ -46,7 +63,8 @@ function pair (token, deviceId, machineModel, numOfCassettes = DEFAULT_NUMBER_OF
|
|||
on conflict (device_id)
|
||||
do update set paired=TRUE, display=TRUE`
|
||||
|
||||
return db.none(insertSql, [deviceId, r.name, numOfCassettes, numOfRecyclers])
|
||||
return db
|
||||
.none(insertSql, [deviceId, r.name, numOfCassettes, numOfRecyclers])
|
||||
.then(() => true)
|
||||
})
|
||||
.catch(err => {
|
||||
|
|
@ -55,20 +73,21 @@ function pair (token, deviceId, machineModel, numOfCassettes = DEFAULT_NUMBER_OF
|
|||
})
|
||||
}
|
||||
|
||||
function authorizeCaDownload (caToken) {
|
||||
return pullToken(caToken)
|
||||
.then(r => {
|
||||
if (r.expired) throw new Error('Expired')
|
||||
function authorizeCaDownload(caToken) {
|
||||
return pullToken(caToken).then(r => {
|
||||
if (r.expired) throw new Error('Expired')
|
||||
|
||||
return readFile(CA_PATH, {encoding: 'utf8'})
|
||||
})
|
||||
return readFile(CA_PATH, { encoding: 'utf8' })
|
||||
})
|
||||
}
|
||||
|
||||
function isPaired (deviceId) {
|
||||
const sql = 'select device_id, name from devices where device_id=$1 and paired=TRUE'
|
||||
function isPaired(deviceId) {
|
||||
const sql =
|
||||
'select device_id, name from devices where device_id=$1 and paired=TRUE'
|
||||
|
||||
return db.oneOrNone(sql, [deviceId])
|
||||
.then(row => row && row.device_id === deviceId ? row.name : false)
|
||||
return db
|
||||
.oneOrNone(sql, [deviceId])
|
||||
.then(row => (row && row.device_id === deviceId ? row.name : false))
|
||||
}
|
||||
|
||||
module.exports = {pair, unpair, authorizeCaDownload, isPaired}
|
||||
module.exports = { pair, unpair, authorizeCaDownload, isPaired }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue