This commit is contained in:
Josh Harvey 2017-03-08 15:39:29 +02:00
parent bd95a90097
commit b4d8f3cd4c
4 changed files with 194 additions and 6 deletions

View file

@ -1,10 +1,21 @@
const _ = require('lodash/fp')
const R = require('ramda')
const db = require('./db')
const dbm = require('./postgresql_interface')
const T = require('./time')
const BN = require('./bn')
const TRANSACTION_EXPIRATION = 2 * T.days
function httpError (msg, code) {
const err = new Error(msg)
err.name = 'HTTPError'
err.code = code || 500
return err
}
function stateChange (deviceId, deviceTime, rec) {
const event = {
id: rec.uuid,
@ -16,8 +27,34 @@ function stateChange (deviceId, deviceTime, rec) {
return dbm.machineEvent(event)
}
function toObj (row) {
if (!row) return null
const keys = _.keys(row)
let newObj = {}
keys.forEach(key => {
const objKey = _.camelCase(key)
if (key === 'crypto_atoms' || key === 'fiat') {
newObj[objKey] = BN(row[key])
return
}
newObj[objKey] = row[key]
})
return newObj
}
function fetchPhoneTx (phone) {
return dbm.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION)
const sql = `select * from cash_out_txs
where phone=$1 and dispensed=$2
and (extract(epoch from (coalesce(confirmation_time, now()) - created))) * 1000 < $3`
const values = [phone, false, TRANSACTION_EXPIRATION]
return db.any(sql, values)
.then(_.map(toObj))
.then(txs => {
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
if (confirmedTxs.length > 0) {
@ -25,11 +62,23 @@ function fetchPhoneTx (phone) {
return !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc
}, null, confirmedTxs)
return {tx: maxTx}
return maxTx
}
if (txs.length > 0) return {pending: true}
return {}
if (txs.length > 0) throw httpError('Pending transactions', 412)
throw httpError('No transactions', 404)
})
}
function fetchStatusTx (txId, status) {
const sql = 'select * from cash_out_txs where id=$1'
return db.oneOrNone(sql, [txId, status])
.then(toObj)
.then(tx => {
if (!tx) throw httpError('No transaction', 404)
if (tx.status === status) throw httpError('Not Modified', 304)
return tx
})
}
@ -37,4 +86,4 @@ function updateDeviceConfigVersion (versionId) {
return db.none('update devices set user_config_id=$1', [versionId])
}
module.exports = {stateChange, fetchPhoneTx, updateDeviceConfigVersion}
module.exports = {stateChange, fetchPhoneTx, fetchStatusTx, updateDeviceConfigVersion}