This commit is contained in:
Josh Harvey 2017-01-11 01:15:05 +02:00
parent f877f1f977
commit bef0a8d98d
2 changed files with 44 additions and 9 deletions

View file

@ -17,6 +17,8 @@ const settingsLoader = require('./settings-loader')
const plugins = require('./plugins') const plugins = require('./plugins')
const helpers = require('./route-helpers') const helpers = require('./route-helpers')
const poller = require('./poller') const poller = require('./poller')
const Tx = require('./tx')
const argv = require('minimist')(process.argv.slice(2)) const argv = require('minimist')(process.argv.slice(2))
const CLOCK_SKEW = 60 * 1000 const CLOCK_SKEW = 60 * 1000
@ -79,6 +81,12 @@ function poll (req, res, next) {
.catch(next) .catch(next)
} }
function postTx (req, res, next) {
return Tx.post(req.body)
.then(tx => res.json(tx))
.catch(next)
}
function trade (req, res, next) { function trade (req, res, next) {
const tx = req.body const tx = req.body
const pi = plugins(req.settings) const pi = plugins(req.settings)
@ -359,22 +367,13 @@ app.use(filterOldRequests)
app.post('*', cacheAction) app.post('*', cacheAction)
app.get('/poll', poll) app.get('/poll', poll)
app.post('/trade', trade)
app.post('/send', send)
app.post('/state', stateChange) app.post('/state', stateChange)
app.post('/cash_out', cashOut)
app.post('/dispense_ack', dispenseAck)
app.post('/event', deviceEvent) app.post('/event', deviceEvent)
app.post('/verify_user', verifyUser) app.post('/verify_user', verifyUser)
app.post('/verify_transaction', verifyTx) app.post('/verify_transaction', verifyTx)
app.post('/phone_code', phoneCode) app.post('/phone_code', phoneCode)
app.post('/update_phone', updatePhone)
app.get('/phone_tx', fetchPhoneTx)
app.post('/register_redeem/:txId', registerRedeem)
app.get('/await_dispense/:txId', waitForDispense)
app.post('/dispense', dispense)
app.use(errorHandler) app.use(errorHandler)
app.use((req, res) => res.status(404).json({err: 'No such route'})) app.use((req, res) => res.status(404).json({err: 'No such route'}))

36
lib/tx.js Normal file
View file

@ -0,0 +1,36 @@
const db = require('./db')
const pgp = require('pg-promise')()
function postCashIn (tx) {
const TransactionMode = pgp.txMode.TransactionMode
const isolationLevel = pgp.txMode.isolationLevel
const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable})
function transaction (t) {
const sql = 'select * from cash_in_txs where id=$1'
return t.one(sql, [tx.id])
.then(row => {
const newTx = executeTxChange(tx, row)
if (row) return updateCashOutTx(newTx)
insertCashOutTx(newTx)
})
}
transaction.txMode = tmSRD
return db.tx(transaction)
// retry failed
}
function postCashOut (tx) {
}
function post (tx) {
if (tx.direction === 'cashIn') return postCashIn(tx)
if (tx.direction === 'cashOut') return postCashOut(tx)
throw new Error('No such tx direction: %s', tx.direction)
}
module.exports = {post}