add sendConfirmed

This commit is contained in:
Josh Harvey 2017-04-19 02:25:08 +03:00
parent 9495aa8424
commit d5aab9dfd0
5 changed files with 39 additions and 14 deletions

View file

@ -2,19 +2,20 @@ const _ = require('lodash/fp')
const pgp = require('pg-promise')()
const db = require('./db')
const BN = require('./bn')
const logger = require('./logger')
const mapValuesWithKey = _.mapValues.convert({cap: false})
module.exports = {post}
const UPDATEABLE_FIELDS = ['fee', 'txHash', 'phone', 'error', 'send', 'cryptoAtoms', 'fiat']
const UPDATEABLE_FIELDS = ['fee', 'txHash', 'phone', 'error', 'send',
'cryptoAtoms', 'fiat', 'timedout']
function post (tx, pi) {
const TransactionMode = pgp.txMode.TransactionMode
const isolationLevel = pgp.txMode.isolationLevel
const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable})
console.log('DEBUG502: %j', tx)
function transaction (t) {
const sql = 'select * from cash_in_txs where id=$1'
const sql2 = 'select * from bills where cash_in_txs_id=$1'
@ -36,9 +37,11 @@ function post (tx, pi) {
return db.tx(transaction)
.then(txVector => {
const [, newTx] = txVector
const [oldTx, newTx, newBills] = txVector
const oldBills = oldTx ? oldTx.bills : []
return postProcess(txVector, pi)
.then(changes => update(newTx, changes))
.then(tx => _.merge({bills: _.concat(oldBills, newBills)}, tx))
})
}
@ -109,7 +112,7 @@ const massage = _.flow(_.omit(['direction', 'bills']), convertBigNumFields, _.ma
function insertNewBills (billRows, tx) {
const bills = pullNewBills(billRows, tx)
if (_.isEmpty(bills)) return Promise.resolve()
if (_.isEmpty(bills)) return Promise.resolve([])
const dbBills = _.map(massage, bills)
const columns = _.keys(dbBills[0])
@ -120,9 +123,11 @@ function insertNewBills (billRows, tx) {
}
function upsert (row, tx) {
console.log('DEBUG501: %j', row)
const oldTx = toObj(row)
if (!oldTx) {
console.log('DEBUG500: %j', tx)
return insert(tx)
.then(newTx => [oldTx, newTx])
}
@ -164,7 +169,11 @@ function postProcess (txVector, pi) {
if (newTx.send && !oldTx.send) {
return pi.sendCoins(newTx)
.then(txHash => ({txHash}))
.then(txHash => ({
txHash,
sendConfirmed: true,
sendTime: 'now()^'
}))
}
return Promise.resolve({})