From 36ac2f3ba5e6994f8bebdeadded2e6ead32fda7e Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Thu, 16 Mar 2017 07:50:41 +0200 Subject: [PATCH] WIPP --- lib/cash-in-tx.js | 67 +++++++++++++++++++++++++++--- lib/cash-out-tx.js | 21 ++++++---- migrations/022-add_cash_in_sent.js | 4 ++ 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/lib/cash-in-tx.js b/lib/cash-in-tx.js index e55fa334..8ee69d31 100644 --- a/lib/cash-in-tx.js +++ b/lib/cash-in-tx.js @@ -3,6 +3,8 @@ const pgp = require('pg-promise')() const db = require('./db') const BN = require('./bn') +const mapValuesWithKey = _.mapValues.convert({cap: false}) + module.exports = {post} const UPDATEABLE_FIELDS = ['fee', 'txHash', 'phone', 'error', 'send'] @@ -14,10 +16,20 @@ function post (tx, pi) { function transaction (t) { const sql = 'select * from cash_in_txs where id=$1' + const sql2 = 'select * from bills where cash_in_txs_id=$1' console.log('DEBUG888: %j', tx) return t.oneOrNone(sql, [tx.id]) - .then(row => upsert(row, tx)) + .then(row => { + return t.any(sql2, [tx.id]) + .then(billRows => { + return upsert(row, tx) + .then(vector => { + return insertNewBills(billRows, tx) + .then(newBills => _.concat(vector, [billRows])) + }) + }) + }) } transaction.txMode = tmSRD @@ -73,11 +85,45 @@ function toObj (row) { return newObj } +function convertBigNumFields (obj) { + const convert = (value, key) => _.includes(key, ['cryptoAtoms', 'fiat']) + ? value.toString() + : value + + const convertKey = key => _.includes(key, ['cryptoAtoms', 'fiat']) + ? key + '#' + : key + + return _.mapKeys(convertKey, mapValuesWithKey(convert, obj)) +} + +function pullNewBills (billRows, tx) { + if (_.isEmpty(tx.bills)) return [] + + const toBill = _.mapKeys(_.camelCase) + const bills = _.map(toBill, billRows) + + return _.differenceBy(_.get('id'), tx.bills, bills) +} + +const massage = _.flow(_.omit(['direction', 'bills']), convertBigNumFields, _.mapKeys(_.snakeCase)) + +function insertNewBills (billRows, tx) { + const bills = pullNewBills(billRows, tx) + if (_.isEmpty(bills)) return Promise.resolve() + + const dbBills = _.map(massage, bills) + const columns = _.keys(dbBills[0]) + console.log('DEBUG333: %j', dbBills) + const sql = pgp.helpers.insert(dbBills, columns, 'bills') + + return db.none(sql) + .then(() => bills) +} + function upsert (row, tx) { const oldTx = toObj(row) - // insert bills - if (!oldTx) { return insert(tx) .then(newTx => [oldTx, newTx]) @@ -88,17 +134,28 @@ function upsert (row, tx) { } function insert (tx) { - const dbTx = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], tx)) + const dbTx = massage(tx) + + console.log('DEBUG334: %j', dbTx) const sql = pgp.helpers.insert(dbTx, null, 'cash_in_txs') + ' returning *' return db.one(sql) .then(toObj) } +// const tx = JSON.parse('{"id":"677ec2b7-8e7a-4efc-99fc-1c1aa1b6a3a6","fiat":"1","cryptoAtoms":"73100","bills":[{"id":"afc6103f-b8bf-4ef3-aa28-6bd14f0c2633","fiat":"1","fiatCode":"USD","cryptoAtoms":"73100","cryptoCode":"BTC","deviceTime":1489642154270,"cashInTxsId":"677ec2b7-8e7a-4efc-99fc-1c1aa1b6a3a6"}],"fiatCode":"USD","cryptoCode":"BTC","direction":"cashIn","toAddress":"1MyRmwUVffy5QC5NEbdu9u1Lb9pZkwcNGg","deviceId":"F2:9C:7F:2C:59:F6:3C:EB:C5:A7:AE:4D:C0:59:32:70:0B:9D:3D:FE"}') + +// insert(tx) +// .then(console.log) +// .catch(err => { +// console.log(err.stack) +// process.exit(1) +// }) + function update (tx, changes) { if (_.isEmpty(changes)) return Promise.resolve(tx) - const dbChanges = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], changes)) + const dbChanges = massage(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 *' diff --git a/lib/cash-out-tx.js b/lib/cash-out-tx.js index cfa24f6a..88f61bf9 100644 --- a/lib/cash-out-tx.js +++ b/lib/cash-out-tx.js @@ -105,19 +105,26 @@ function mapDispense (tx) { rejected2: bills[1].rejected, denomination1: bills[0].denomination, denomination2: bills[1].denomination, - dispenseTime: 'NOW()^' + 'dispenseTime^': 'NOW()' } return _.assign(tx, extra) } -function toDb (tx) { - const mapper = (v, k) => { - if (k === 'fiat' || k === 'cryptoAtoms') return v.toNumber() - return v - } +function convertBigNumFields (obj) { + const convert = (value, key) => _.includes(key, ['cryptoAtoms', 'fiat']) + ? value.toString() + : value - const massager = _.flow(mapValuesWithKey(mapper), mapDispense, _.omit(['direction', 'bills']), _.mapKeys(_.snakeCase)) + const convertKey = key => _.includes(key, ['cryptoAtoms', 'fiat']) + ? key + '#' + : key + + return _.mapKeys(convertKey, mapValuesWithKey(convert, obj)) +} + +function toDb (tx) { + const massager = _.flow(convertBigNumFields, mapDispense, _.omit(['direction', 'bills']), _.mapKeys(_.snakeCase)) return massager(tx) } diff --git a/migrations/022-add_cash_in_sent.js b/migrations/022-add_cash_in_sent.js index 41aeb0c9..74423dab 100644 --- a/migrations/022-add_cash_in_sent.js +++ b/migrations/022-add_cash_in_sent.js @@ -4,6 +4,10 @@ exports.up = function (next) { var sql = [ 'alter table cash_in_txs add column send boolean not null default false', 'alter table cash_in_txs rename currency_code to fiat_code', + 'alter table bills rename currency_code to fiat_code', + 'alter table bills rename denomination to fiat', + 'alter table bills drop column to_address', + 'alter table bills drop column device_id', 'alter table cash_out_txs rename currency_code to fiat_code' ] db.multi(sql, next)