diff --git a/lib/cash-out-tx.js b/lib/cash-out-tx.js index d9fb363e..4e9c9116 100644 --- a/lib/cash-out-tx.js +++ b/lib/cash-out-tx.js @@ -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 = { diff --git a/lib/route-helpers.js b/lib/route-helpers.js index 02b1a0f7..5e7ee8ea 100644 --- a/lib/route-helpers.js +++ b/lib/route-helpers.js @@ -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) diff --git a/lib/routes.js b/lib/routes.js index 939fbdf2..93b44f47 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -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) diff --git a/migrations/028-cash_out_actions.js b/migrations/028-cash_out_actions.js index 6442f81b..61312f88 100644 --- a/migrations/028-cash_out_actions.js +++ b/migrations/028-cash_out_actions.js @@ -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' ]