This commit is contained in:
Josh Harvey 2017-03-06 14:22:33 +02:00
parent 0bf54fa1d8
commit 30071151ff
6 changed files with 107 additions and 242 deletions

View file

@ -3,17 +3,19 @@ const pgp = require('pg-promise')()
const db = require('./db')
const BN = require('./bn')
module.exports = {postCashIn}
module.exports = {post}
const UPDATEABLE_FIELDS = ['fee', 'txHash', 'phone', 'error']
const UPDATEABLE_FIELDS = ['fee', 'txHash', 'phone', 'error', 'send']
function postCashIn (tx, pi) {
function post (tx, pi) {
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'
console.log('DEBUG888: %j', tx)
return t.oneOrNone(sql, [tx.id])
.then(row => upsert(row, tx))
}
@ -21,15 +23,18 @@ function postCashIn (tx, pi) {
transaction.txMode = tmSRD
return db.tx(transaction)
.then(txVector => postProcess(txVector, pi))
.then(changes => update(tx.id, changes))
.then(txVector => {
const [, newTx] = txVector
postProcess(txVector, pi)
.then(changes => update(newTx, changes))
})
}
function diff (oldTx, newTx) {
let updatedTx = {}
UPDATEABLE_FIELDS.forEach(fieldKey => {
if (_.isEqual(oldTx[fieldKey], newTx[fieldKey])) return
if (oldTx && _.isEqual(oldTx[fieldKey], newTx[fieldKey])) return
updatedTx[fieldKey] = newTx[fieldKey]
})
@ -37,6 +42,8 @@ function diff (oldTx, newTx) {
}
function toObj (row) {
if (!row) return null
const keys = _.keys(row)
let newObj = {}
@ -56,31 +63,48 @@ function toObj (row) {
function upsert (row, tx) {
const oldTx = toObj(row)
if (oldTx) return insert(tx)
return update(tx.id, diff(oldTx, tx))
// insert bills
if (!oldTx) {
return insert(tx)
.then(newTx => [oldTx, newTx])
}
return update(tx, diff(oldTx, tx))
.then(newTx => [oldTx, newTx])
}
function insert (tx) {
const dbTx = _.mapKeys(_.snakeCase, tx)
const dbTx = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], tx))
const sql = pgp.helpers.insert(dbTx, null, 'cash_in_txs')
return db.none(sql)
const sql = pgp.helpers.insert(dbTx, null, 'cash_in_txs') + ' returning *'
return db.one(sql)
.then(toObj)
}
function update (txId, changes) {
const dbChanges = _.mapKeys(_.snakeCase, changes)
const sql = pgp.helpers.update(dbChanges, null, 'cash_in_txs') +
pgp.as.format(' where id=$1', [txId])
function update (tx, changes) {
if (_.isEmpty(changes)) return Promise.resolve(tx)
return db.none(sql)
const dbChanges = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], changes))
console.log('DEBUG893: %j', dbChanges)
const sql = pgp.helpers.update(dbChanges, null, 'cash_in_txs') +
pgp.as.format(' where id=$1', [tx.id]) + ' returning *'
return db.one(sql)
.then(toObj)
}
function postProcess (txVector, pi) {
const [oldTx, newTx] = txVector
if (newTx.sent && !oldTx.sent) {
if (newTx.send && !oldTx.send) {
return pi.sendCoins(newTx)
.then(txHash => ({txHash}))
.catch(error => ({error}))
.catch(error => {
console.log('DEBUG895: %j', error)
return {error}
})
}
return Promise.resolve({})
}