WIP
This commit is contained in:
parent
855546f886
commit
785749133d
5 changed files with 50 additions and 19 deletions
17
lib/pair.js
17
lib/pair.js
|
|
@ -9,20 +9,23 @@ const CA_PATH = path.resolve(__dirname, '..', 'certs', 'root-ca.crt.pem')
|
|||
function pullToken (token) {
|
||||
const sql = `delete from pairing_tokens
|
||||
where token=$1
|
||||
returning created < now() - interval '1 hour' as expired`
|
||||
returning name, created < now() - interval '1 hour' as expired`
|
||||
return db.one(sql, [token])
|
||||
.then(r => r.expired)
|
||||
}
|
||||
|
||||
function pair (token, deviceId) {
|
||||
pullToken(token)
|
||||
.then(valid => {
|
||||
if (!valid) return false
|
||||
.then(r => {
|
||||
if (r.expired) return false
|
||||
|
||||
const pairSql = 'insert into paired_devices (device_id) values ($1)'
|
||||
return db.none(pairSql, [deviceId])
|
||||
const insertSql = `insert into devices (device_id, name) values ($1, $2)
|
||||
on conflict (device_id)
|
||||
do update set name=$1, paired=TRUE, display=TRUE`
|
||||
|
||||
return db.none(insertSql, [deviceId, r.name])
|
||||
.then(() => true)
|
||||
})
|
||||
.catch(() => false)
|
||||
}
|
||||
|
||||
function authorizeCaDownload (caToken) {
|
||||
|
|
@ -34,7 +37,7 @@ function ca () {
|
|||
}
|
||||
|
||||
function isPaired (deviceId) {
|
||||
const sql = 'select device_id from paired_devices where device_id=$1'
|
||||
const sql = 'select device_id from devices where device_id=$1 and paired=TRUE'
|
||||
|
||||
return db.one(sql, [deviceId])
|
||||
.then(() => true)
|
||||
|
|
|
|||
|
|
@ -276,8 +276,7 @@ function buildCartridges (cartridges, virtualCartridges, rec) {
|
|||
count: parseInt(rec.counts[1], 10)
|
||||
}
|
||||
],
|
||||
virtualCartridges,
|
||||
id: rec.id
|
||||
virtualCartridges
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -149,7 +149,14 @@ function insertDispense (deviceId, tx, cartridges) {
|
|||
false, tx.error
|
||||
]
|
||||
|
||||
const sql2 = `update devices set cassette1=cassette1-$1, cassette2=cassette2-$2
|
||||
where device_id=$3`
|
||||
|
||||
const pulled1 = dispense1 + reject1
|
||||
const pulled2 = dispense2 + reject2
|
||||
|
||||
return db.none(sql, values)
|
||||
.then(() => db.none(sql2, [pulled1, pulled2, deviceId]))
|
||||
}
|
||||
|
||||
exports.addIncomingPhone = function addIncomingPhone (tx, notified) {
|
||||
|
|
@ -236,15 +243,13 @@ exports.addDispense = function addDispense (deviceId, tx, cartridges) {
|
|||
}
|
||||
|
||||
exports.cartridgeCounts = function cartridgeCounts (deviceId) {
|
||||
const sql = 'SELECT id, count1, count2 FROM dispenses ' +
|
||||
'WHERE device_id=$1 AND refill=$2 ' +
|
||||
'ORDER BY id DESC LIMIT 1'
|
||||
const sql = 'SELECT cassette1, cassette2 FROM devices ' +
|
||||
'WHERE device_id=$1'
|
||||
|
||||
return db.oneOrNone(sql, [deviceId, true])
|
||||
return db.one(sql, [deviceId])
|
||||
.then(row => {
|
||||
const counts = row ? [row.count1, row.count2] : [0, 0]
|
||||
const id = row ? row.id : 0
|
||||
return {id, counts}
|
||||
const counts = [row.cassette1, row.cassette2]
|
||||
return {counts}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,8 +26,8 @@ function buildRates (deviceId) {
|
|||
const cryptoCodes = plugins.getCryptoCodes()
|
||||
const config = plugins.getConfig(deviceId)
|
||||
|
||||
const cashInCommission = new BigNumber(config.commissions.cashInCommission).div(100)
|
||||
const cashOutCommission = new BigNumber(config.commissions.cashOutCommission).div(100)
|
||||
const cashInCommission = new BigNumber(config.commissions.cashInCommission).div(100).plus(1)
|
||||
const cashOutCommission = new BigNumber(config.commissions.cashOutCommission).div(100).plus(1)
|
||||
|
||||
const rates = {}
|
||||
cryptoCodes.forEach(cryptoCode => {
|
||||
|
|
@ -196,7 +196,7 @@ function pair (req, res) {
|
|||
return pair.pair(token, deviceId)
|
||||
.then(valid => {
|
||||
if (valid) return cacheAndRespond(req, res)
|
||||
throw httpError('Invalid token', 408)
|
||||
throw httpError('Pairing failed')
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
24
migrations/018-alter_devices.js
Normal file
24
migrations/018-alter_devices.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
var db = require('./db')
|
||||
|
||||
exports.up = function (next) {
|
||||
var sql = [
|
||||
'drop table if exists devices',
|
||||
'drop table if exists paired_devices',
|
||||
`create table devices (
|
||||
device_id text PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
cashbox integer NOT NULL default 0,
|
||||
cassette1 integer NOT NULL default 0,
|
||||
cassette2 integer NOT NULL default 0,
|
||||
paired boolean NOT NULL default TRUE,
|
||||
display boolean NOT NULL default TRUE,
|
||||
created timestamptz NOT NULL default now()
|
||||
)`,
|
||||
'alter table pairing_tokens add column name text NOT NULL'
|
||||
]
|
||||
db.multi(sql, next)
|
||||
}
|
||||
|
||||
exports.down = function (next) {
|
||||
next()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue