This commit is contained in:
Josh Harvey 2016-11-08 00:28:57 +00:00
parent 855546f886
commit 785749133d
5 changed files with 50 additions and 19 deletions

View file

@ -9,20 +9,23 @@ const CA_PATH = path.resolve(__dirname, '..', 'certs', 'root-ca.crt.pem')
function pullToken (token) { function pullToken (token) {
const sql = `delete from pairing_tokens const sql = `delete from pairing_tokens
where token=$1 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]) return db.one(sql, [token])
.then(r => r.expired)
} }
function pair (token, deviceId) { function pair (token, deviceId) {
pullToken(token) pullToken(token)
.then(valid => { .then(r => {
if (!valid) return false if (r.expired) return false
const pairSql = 'insert into paired_devices (device_id) values ($1)' const insertSql = `insert into devices (device_id, name) values ($1, $2)
return db.none(pairSql, [deviceId]) on conflict (device_id)
do update set name=$1, paired=TRUE, display=TRUE`
return db.none(insertSql, [deviceId, r.name])
.then(() => true) .then(() => true)
}) })
.catch(() => false)
} }
function authorizeCaDownload (caToken) { function authorizeCaDownload (caToken) {
@ -34,7 +37,7 @@ function ca () {
} }
function isPaired (deviceId) { 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]) return db.one(sql, [deviceId])
.then(() => true) .then(() => true)

View file

@ -276,8 +276,7 @@ function buildCartridges (cartridges, virtualCartridges, rec) {
count: parseInt(rec.counts[1], 10) count: parseInt(rec.counts[1], 10)
} }
], ],
virtualCartridges, virtualCartridges
id: rec.id
} }
} }

View file

@ -149,7 +149,14 @@ function insertDispense (deviceId, tx, cartridges) {
false, tx.error 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) return db.none(sql, values)
.then(() => db.none(sql2, [pulled1, pulled2, deviceId]))
} }
exports.addIncomingPhone = function addIncomingPhone (tx, notified) { exports.addIncomingPhone = function addIncomingPhone (tx, notified) {
@ -236,15 +243,13 @@ exports.addDispense = function addDispense (deviceId, tx, cartridges) {
} }
exports.cartridgeCounts = function cartridgeCounts (deviceId) { exports.cartridgeCounts = function cartridgeCounts (deviceId) {
const sql = 'SELECT id, count1, count2 FROM dispenses ' + const sql = 'SELECT cassette1, cassette2 FROM devices ' +
'WHERE device_id=$1 AND refill=$2 ' + 'WHERE device_id=$1'
'ORDER BY id DESC LIMIT 1'
return db.oneOrNone(sql, [deviceId, true]) return db.one(sql, [deviceId])
.then(row => { .then(row => {
const counts = row ? [row.count1, row.count2] : [0, 0] const counts = [row.cassette1, row.cassette2]
const id = row ? row.id : 0 return {counts}
return {id, counts}
}) })
} }

View file

@ -26,8 +26,8 @@ function buildRates (deviceId) {
const cryptoCodes = plugins.getCryptoCodes() const cryptoCodes = plugins.getCryptoCodes()
const config = plugins.getConfig(deviceId) const config = plugins.getConfig(deviceId)
const cashInCommission = new BigNumber(config.commissions.cashInCommission).div(100) const cashInCommission = new BigNumber(config.commissions.cashInCommission).div(100).plus(1)
const cashOutCommission = new BigNumber(config.commissions.cashOutCommission).div(100) const cashOutCommission = new BigNumber(config.commissions.cashOutCommission).div(100).plus(1)
const rates = {} const rates = {}
cryptoCodes.forEach(cryptoCode => { cryptoCodes.forEach(cryptoCode => {
@ -196,7 +196,7 @@ function pair (req, res) {
return pair.pair(token, deviceId) return pair.pair(token, deviceId)
.then(valid => { .then(valid => {
if (valid) return cacheAndRespond(req, res) if (valid) return cacheAndRespond(req, res)
throw httpError('Invalid token', 408) throw httpError('Pairing failed')
}) })
} }

View 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()
}