handle tx dirty flag

This commit is contained in:
Josh Harvey 2017-05-16 14:01:34 +03:00
parent 5ed29ee67d
commit db97533d5c

View file

@ -3,7 +3,7 @@ const BN = require('./bn')
const CashInTx = require('./cash-in-tx') const CashInTx = require('./cash-in-tx')
const CashOutTx = require('./cash-out-tx') const CashOutTx = require('./cash-out-tx')
function post (tx, pi) { function process (tx, pi) {
const mtx = massage(tx) const mtx = massage(tx)
if (mtx.direction === 'cashIn') return CashInTx.post(mtx, pi) if (mtx.direction === 'cashIn') return CashInTx.post(mtx, pi)
if (mtx.direction === 'cashOut') return CashOutTx.post(mtx, pi) if (mtx.direction === 'cashOut') return CashOutTx.post(mtx, pi)
@ -11,13 +11,18 @@ function post (tx, pi) {
return Promise.reject(new Error('No such tx direction: ' + mtx.direction)) return Promise.reject(new Error('No such tx direction: ' + mtx.direction))
} }
function post (tx, pi) {
return process(tx, pi)
.then(_.set('dirty', false))
}
function massage (tx) { function massage (tx) {
const isDateField = r => r === 'created' || _.endsWith('_time', r) const isDateField = r => r === 'created' || _.endsWith('_time', r)
const transformDate = (v, k) => isDateField(k) ? new Date(v) : v const transformDate = (v, k) => isDateField(k) ? new Date(v) : v
const mapValuesWithKey = _.mapValues.convert({'cap': false}) const mapValuesWithKey = _.mapValues.convert({'cap': false})
const transformDates = r => mapValuesWithKey(transformDate, r) const transformDates = r => mapValuesWithKey(transformDate, r)
const mapBN = r => _.assign(r, {cryptoAtoms: BN(r.cryptoAtoms), fiat: BN(r.fiat)}) const mapBN = r => _.assign(r, {cryptoAtoms: BN(r.cryptoAtoms), fiat: BN(r.fiat)})
const mapper = _.flow(transformDates, mapBN) const mapper = _.flow(transformDates, mapBN, _.unset('dirty'))
return mapper(tx) return mapper(tx)
} }