lamassu-server/lib/pair.js
2016-10-21 18:18:15 +03:00

24 lines
600 B
JavaScript

const db = require('./db')
function pair (token, deviceId) {
const sql = `delete from pairing_tokens
where token=$1
returning created < now() - interval '1 hour' as expired`
return db.one(sql, [token])
.then(r => {
if (r.expired) return false
const pairSql = 'insert into paired_devices (device_id) values ($1)'
return db.none(pairSql, [deviceId])
.then(() => true)
})
}
function isPaired (deviceId) {
const sql = 'select device_id from paired_devices where device_id=$1'
return db.one(sql, [deviceId])
.then(() => true)
}
module.exports = {pair, isPaired}