Add transaction versioning, tx cancellation (#79)

This commit is contained in:
Josh Harvey 2017-08-29 16:08:06 +03:00 committed by GitHub
parent 4a97535dec
commit 500edcf279
15 changed files with 2223 additions and 1468 deletions

View file

@ -150,6 +150,24 @@ app.get('/api/transactions', (req, res, next) => {
.catch(next)
})
app.get('/api/transaction/:id', (req, res, next) => {
return transactions.single(req.params.id)
.then(r => {
if (!r) return res.status(404).send({Error: 'Not found'})
return res.send(r)
})
})
app.patch('/api/transaction/:id', (req, res, next) => {
if (!req.query.cancel) return res.status(400).send({Error: 'Requires cancel'})
return transactions.cancel(req.params.id)
.then(r => {
return res.send(r)
})
.catch(() => res.status(404).send({Error: 'Not found'}))
})
app.use((err, req, res, next) => {
console.error(err)
@ -223,7 +241,6 @@ function establishSocket (ws, token) {
if (!success) return ws.close(1008, 'Authentication error')
const listener = data => {
console.log('DEBUG200: %j', data)
ws.send(JSON.stringify(data))
}
@ -239,8 +256,6 @@ function establishSocket (ws, token) {
}, REAUTHENTICATE_INTERVAL)
socketEmitter.on('message', listener)
console.log('DEBUG120: %j', token)
ws.send('Testing123')
})
}

View file

@ -2,6 +2,8 @@ const _ = require('lodash/fp')
const db = require('../db')
const machineLoader = require('../machine-loader')
const tx = require('../tx')
const cashInTx = require('../cash-in-tx')
const NUM_RESULTS = 20
@ -18,21 +20,49 @@ function addNames (txs) {
})
}
const camelize = _.mapKeys(_.camelCase)
function batch () {
const camelize = _.mapKeys(_.camelCase)
const packager = _.flow(_.flatten, _.orderBy(_.property('created'), ['desc']),
_.take(NUM_RESULTS), _.map(camelize), addNames)
const cashInSql = `select 'cashIn' as tx_class, cash_in_txs.*
const cashInSql = `select 'cashIn' as tx_class, cash_in_txs.*,
((not send_confirmed) and (created <= now() - interval $1)) as expired
from cash_in_txs
order by created desc limit $1`
order by created desc limit $2`
const cashOutSql = `select 'cashOut' as tx_class, cash_out_txs.*
from cash_out_txs
order by created desc limit $1`
return Promise.all([db.any(cashInSql, [NUM_RESULTS]), db.any(cashOutSql, [NUM_RESULTS])])
return Promise.all([db.any(cashInSql, [cashInTx.PENDING_INTERVAL, NUM_RESULTS]), db.any(cashOutSql, [NUM_RESULTS])])
.then(packager)
}
module.exports = {batch}
function single (txId) {
const packager = _.flow(_.compact, _.map(camelize), addNames)
const cashInSql = `select 'cashIn' as tx_class,
((not send_confirmed) and (created <= now() - interval $1)) as expired,
cash_in_txs.*
from cash_in_txs
where id=$2`
const cashOutSql = `select 'cashOut' as tx_class, cash_out_txs.*
from cash_out_txs
where id=$1`
return Promise.all([
db.oneOrNone(cashInSql, [cashInTx.PENDING_INTERVAL, txId]),
db.oneOrNone(cashOutSql, [txId])
])
.then(packager)
.then(_.head)
}
function cancel (txId) {
return tx.cancel(txId)
.then(() => single(txId))
}
module.exports = {batch, single, cancel}