refactor: remove extra conditions and fields renaming

This commit is contained in:
José Oliveira 2021-05-14 17:05:53 +01:00 committed by Josh Harvey
parent b0bbb2983f
commit 0c0133d01c
2 changed files with 21 additions and 31 deletions

View file

@ -432,7 +432,8 @@ function plugins (settings, deviceId) {
if (filtered.length === 0) return null if (filtered.length === 0) return null
const internalTxIdList = _.uniqBy('txId', _.map(tradeElement => { return { txId: tradeElement.internalTxId, direction: tradeElement.direction } })(filtered)) const partitionByDirection = _.partition(({ direction }) => direction === 'cashIn')
const [cashInTxs, cashOutTxs] = _.compose(partitionByDirection, _.uniqBy('internalTxId'))(filtered)
const cryptoAtoms = filtered const cryptoAtoms = filtered
.reduce((prev, current) => prev.plus(current.cryptoAtoms), BN(0)) .reduce((prev, current) => prev.plus(current.cryptoAtoms), BN(0))
@ -440,7 +441,8 @@ function plugins (settings, deviceId) {
const timestamp = filtered.map(r => r.timestamp).reduce((acc, r) => Math.max(acc, r), 0) const timestamp = filtered.map(r => r.timestamp).reduce((acc, r) => Math.max(acc, r), 0)
const consolidatedTrade = { const consolidatedTrade = {
internalTxIdList, cashInTxs,
cashOutTxs,
fiatCode, fiatCode,
cryptoAtoms, cryptoAtoms,
cryptoCode, cryptoCode,
@ -511,28 +513,18 @@ function plugins (settings, deviceId) {
}) })
} }
function recordTradeAndTx (tradeId, internalTxIdList, dbTx) { function recordTradeAndTx (tradeId, { cashInTxs, cashOutTxs }, dbTx) {
const columnSetCashin = new pgp.helpers.ColumnSet(['cash_in_tx_id', 'trade_id'], { table: 'cashin_tx_trades' }) const columnSetCashIn = new pgp.helpers.ColumnSet(['tx_id', 'trade_id'], { table: 'cashin_tx_trades' })
const columnSetCashout = new pgp.helpers.ColumnSet(['cash_out_tx_id', 'trade_id'], { table: 'cashout_tx_trades' }) const columnSetCashOut = new pgp.helpers.ColumnSet(['tx_id', 'trade_id'], { table: 'cashout_tx_trades' })
let cashinTxs = [] const mapToEntry = _.map(tx => ({ tx_id: tx.internalTxId, trade_id: tradeId }))
let cashoutTxs = [] const queries = []
let queries = []
_.map(internalTxId => { if (!_.isEmpty(cashInTxs)) {
let entry = { trade_id: tradeId } const query = pgp.helpers.insert(mapToEntry(cashInTxs), columnSetCashIn)
if (internalTxId.direction === 'cashIn') {
entry.cash_in_tx_id = internalTxId.txId
cashinTxs.push(entry)
} else {
entry.cash_out_tx_id = internalTxId.txId
cashoutTxs.push(entry)
}
}, internalTxIdList)
if (!_.isEmpty(cashinTxs)) {
const query = pgp.helpers.insert(cashinTxs, columnSetCashin)
queries.push(dbTx.none(query)) queries.push(dbTx.none(query))
} }
if (!_.isEmpty(cashoutTxs)) { if (!_.isEmpty(cashOutTxs)) {
const query = pgp.helpers.insert(cashoutTxs, columnSetCashout) const query = pgp.helpers.insert(mapToEntry(cashOutTxs), columnSetCashOut)
queries.push(dbTx.none(query)) queries.push(dbTx.none(query))
} }
return Promise.all(queries) return Promise.all(queries)
@ -568,12 +560,10 @@ function plugins (settings, deviceId) {
) )
const tradeEntry = massage(_tradeEntry, error) const tradeEntry = massage(_tradeEntry, error)
const sql = pgp.helpers.insert(tradeEntry, null, 'trades') + 'RETURNING *' const sql = pgp.helpers.insert(tradeEntry, null, 'trades') + 'RETURNING *'
db.tx(async t => { db.tx(t => {
const newTrade = await t.oneOrNone(sql) return t.oneOrNone(sql)
return recordTradeAndTx(newTrade.id, _tradeEntry.internalTxIdList, t) .then(newTrade => recordTradeAndTx(newTrade.id, _tradeEntry, t))
}) })
.then(data => data)
.catch(error => error)
} }
function sendMessage (rec) { function sendMessage (rec) {

View file

@ -3,14 +3,14 @@ const db = require('./db')
exports.up = function (next) { exports.up = function (next) {
var sql = [ var sql = [
`CREATE TABLE cashout_tx_trades ( `CREATE TABLE cashout_tx_trades (
cash_out_tx_id uuid REFERENCES cash_out_txs(id), tx_id uuid REFERENCES cash_out_txs(id),
trade_id serial REFERENCES trades(id), trade_id serial REFERENCES trades(id),
CONSTRAINT cashout_trade_pkey PRIMARY KEY (cash_out_tx_id,trade_id) CONSTRAINT cashout_trade_pkey PRIMARY KEY (tx_id,trade_id)
)`, )`,
`CREATE TABLE cashin_tx_trades ( `CREATE TABLE cashin_tx_trades (
cash_in_tx_id uuid REFERENCES cash_in_txs(id), tx_id uuid REFERENCES cash_in_txs(id),
trade_id serial REFERENCES trades(id), trade_id serial REFERENCES trades(id),
CONSTRAINT cashin_trade_pkey PRIMARY KEY (cash_in_tx_id,trade_id) CONSTRAINT cashin_trade_pkey PRIMARY KEY (tx_id,trade_id)
)` )`
] ]