fix redeem bugs

This commit is contained in:
Josh Harvey 2017-04-24 17:28:54 +03:00
parent 91e1077daa
commit dd7d06f38b
4 changed files with 17 additions and 6 deletions

View file

@ -25,6 +25,15 @@ const STALE_INCOMING_TX_AGE = T.week
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
const MAX_NOTIFY_AGE = 2 * T.days
const MIN_NOTIFY_AGE = 5 * T.minutes
const INSUFFICIENT_FUNDS_CODE = 570
function httpError (msg, code) {
const err = new Error(msg)
err.name = 'HTTPError'
err.code = code || 500
return err
}
function post (tx, pi) {
console.log('DEBUG101: %j', tx)
@ -275,7 +284,9 @@ function postProcess (txVector, pi) {
return pi.buildCassettes()
.then(cassettes => {
pi.sell(newTx)
return _.set('bills', billMath.makeChange(cassettes.cassettes, newTx.fiat), newTx)
const bills = billMath.makeChange(cassettes.cassettes, newTx.fiat)
if (!bills) throw httpError('Out of bills', INSUFFICIENT_FUNDS_CODE)
return _.set('bills', bills, newTx)
})
.then(tx => {
const rec = {

View file

@ -28,7 +28,7 @@ function stateChange (deviceId, deviceTime, rec) {
return dbm.machineEvent(event)
}
function toObj (row) {
function toCashOutTx (row) {
if (!row) return null
const keys = _.keys(row)
@ -44,7 +44,7 @@ function toObj (row) {
newObj[objKey] = row[key]
})
return newObj
return _.set('direction', 'cashOut', newObj)
}
function fetchPhoneTx (phone) {
@ -55,7 +55,7 @@ function fetchPhoneTx (phone) {
const values = [phone, false, TRANSACTION_EXPIRATION]
return db.any(sql, values)
.then(_.map(toObj))
.then(_.map(toCashOutTx))
.then(txs => {
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
if (confirmedTxs.length > 0) {
@ -75,7 +75,7 @@ function fetchStatusTx (txId, status) {
const sql = 'select * from cash_out_txs where id=$1'
return db.oneOrNone(sql, [txId])
.then(toObj)
.then(toCashOutTx)
.then(tx => {
if (!tx) throw httpError('No transaction', 404)
if (tx.status === status) throw httpError('Not Modified', 304)

View file

@ -90,6 +90,7 @@ function getTx (req, res, next) {
function getPhoneTx (req, res, next) {
if (req.query.phone) {
console.log('DEBUG120: %s', req.query.phone)
return helpers.fetchPhoneTx(req.query.phone)
.then(r => res.json(r))
.catch(next)

View file

@ -30,7 +30,6 @@ exports.up = function (next) {
'alter table cash_out_txs drop column denomination_2',
'alter table cash_out_txs drop column dispense_error',
'alter table cash_out_txs drop column dispense_time',
'alter table cash_out_txs drop column confirmation_time',
'alter table cash_out_txs add column dispense_confirmed boolean default false',
'alter table cash_out_txs rename column dispensed to dispense'
]