This commit is contained in:
Josh Harvey 2017-03-15 22:54:40 +02:00
parent b4d8f3cd4c
commit 340b39d47d
9 changed files with 189 additions and 72 deletions

View file

@ -2,25 +2,11 @@ const _ = require('lodash/fp')
const pgp = require('pg-promise')()
const db = require('./db')
const BN = require('./bn')
const billMath = require('./bill-math')
module.exports = {post}
// id | uuid | not null
// device_id | text | not null
// to_address | text | not null
// crypto_atoms | bigint | not null
// crypto_code | text | not null
// fiat | numeric(14,5) | not null
// currency_code | text | not null
// tx_hash | text |
// status | status_stage | not null default 'notSeen'::status_stage
// dispensed | boolean | not null default false
// notified | boolean | not null default false
// redeem | boolean | not null default false
// phone | text |
// error | text |
// created | timestamp with time zone | not null default now()
// confirmation_time | timestamp with time zone |
const mapValuesWithKey = _.mapValues.convert({cap: false})
const UPDATEABLE_FIELDS = ['txHash', 'status', 'dispensed', 'notified', 'redeem',
'phone', 'error', 'confirmationTime']
@ -33,9 +19,13 @@ function post (tx, pi) {
function transaction (t) {
const sql = 'select * from cash_out_txs where id=$1'
console.log('DEBUG888: %j', tx)
console.log('DEBUG988: %j', tx)
return t.oneOrNone(sql, [tx.id])
.then(row => upsert(row, tx))
.then(toObj)
.then(oldTx => {
return preProcess(oldTx, tx, pi)
.then(preProcessedTx => upsert(oldTx, preProcessedTx))
})
}
transaction.txMode = tmSRD
@ -86,12 +76,12 @@ function toObj (row) {
newObj[objKey] = row[key]
})
newObj.direction = 'cashOut'
return newObj
}
function upsert (row, tx) {
const oldTx = toObj(row)
function upsert (oldTx, tx) {
// insert bills
if (!oldTx) {
@ -103,10 +93,40 @@ function upsert (row, tx) {
.then(newTx => [oldTx, newTx])
}
function mapDispense (tx) {
const bills = tx.bills
if (_.isEmpty(bills)) return tx
const extra = {
dispensed1: bills[0].actualDispense,
dispensed2: bills[1].actualDispense,
rejected1: bills[0].rejected,
rejected2: bills[1].rejected,
denomination1: bills[0].denomination,
denomination2: bills[1].denomination,
dispenseTime: 'NOW()^'
}
return _.assign(tx, extra)
}
function toDb (tx) {
const mapper = (v, k) => {
if (k === 'fiat' || k === 'cryptoAtoms') return v.toNumber()
return v
}
const massager = _.flow(mapValuesWithKey(mapper), mapDispense, _.omit(['direction', 'bills']), _.mapKeys(_.snakeCase))
return massager(tx)
}
function insert (tx) {
const dbTx = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], tx))
const dbTx = toDb(tx)
const sql = pgp.helpers.insert(dbTx, null, 'cash_out_txs') + ' returning *'
console.log('DEBUG901: %s', sql)
console.log('DEBUG902: %j', dbTx)
return db.one(sql)
.then(toObj)
}
@ -114,17 +134,36 @@ function insert (tx) {
function update (tx, changes) {
if (_.isEmpty(changes)) return Promise.resolve(tx)
const dbChanges = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], changes))
const dbChanges = toDb(tx)
console.log('DEBUG893: %j', dbChanges)
const sql = pgp.helpers.update(dbChanges, null, 'cash_out_txs') +
pgp.as.format(' where id=$1', [tx.id]) + ' returning *'
pgp.as.format(' where id=$1', [tx.id])
return db.one(sql)
.then(toObj)
const newTx = _.merge(tx, changes)
return db.none(sql)
.then(() => newTx)
}
function preProcess (tx, newTx, pi) {
if (!tx) {
console.log('DEBUG910')
return pi.newAddress(newTx)
.then(_.set('toAddress', _, newTx))
}
return Promise.resolve(newTx)
}
function postProcess (txVector, pi) {
const [oldTx, newTx] = txVector
const [, newTx] = txVector
return Promise.resolve({})
if (newTx.dispensed && !newTx.bills) {
return pi.buildCartridges()
.then(cartridges => {
return _.set('bills', billMath.makeChange(cartridges.cartridges, newTx.fiat), newTx)
})
}
return Promise.resolve(newTx)
}