refactor: remove extra conditions and fields renaming
This commit is contained in:
parent
b0bbb2983f
commit
0c0133d01c
2 changed files with 21 additions and 31 deletions
|
|
@ -432,7 +432,8 @@ function plugins (settings, deviceId) {
|
|||
|
||||
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
|
||||
.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 consolidatedTrade = {
|
||||
internalTxIdList,
|
||||
cashInTxs,
|
||||
cashOutTxs,
|
||||
fiatCode,
|
||||
cryptoAtoms,
|
||||
cryptoCode,
|
||||
|
|
@ -511,28 +513,18 @@ function plugins (settings, deviceId) {
|
|||
})
|
||||
}
|
||||
|
||||
function recordTradeAndTx (tradeId, internalTxIdList, dbTx) {
|
||||
const columnSetCashin = new pgp.helpers.ColumnSet(['cash_in_tx_id', 'trade_id'], { table: 'cashin_tx_trades' })
|
||||
const columnSetCashout = new pgp.helpers.ColumnSet(['cash_out_tx_id', 'trade_id'], { table: 'cashout_tx_trades' })
|
||||
let cashinTxs = []
|
||||
let cashoutTxs = []
|
||||
let queries = []
|
||||
_.map(internalTxId => {
|
||||
let entry = { trade_id: tradeId }
|
||||
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)
|
||||
function recordTradeAndTx (tradeId, { cashInTxs, cashOutTxs }, dbTx) {
|
||||
const columnSetCashIn = new pgp.helpers.ColumnSet(['tx_id', 'trade_id'], { table: 'cashin_tx_trades' })
|
||||
const columnSetCashOut = new pgp.helpers.ColumnSet(['tx_id', 'trade_id'], { table: 'cashout_tx_trades' })
|
||||
const mapToEntry = _.map(tx => ({ tx_id: tx.internalTxId, trade_id: tradeId }))
|
||||
const queries = []
|
||||
|
||||
if (!_.isEmpty(cashInTxs)) {
|
||||
const query = pgp.helpers.insert(mapToEntry(cashInTxs), columnSetCashIn)
|
||||
queries.push(dbTx.none(query))
|
||||
}
|
||||
if (!_.isEmpty(cashoutTxs)) {
|
||||
const query = pgp.helpers.insert(cashoutTxs, columnSetCashout)
|
||||
if (!_.isEmpty(cashOutTxs)) {
|
||||
const query = pgp.helpers.insert(mapToEntry(cashOutTxs), columnSetCashOut)
|
||||
queries.push(dbTx.none(query))
|
||||
}
|
||||
return Promise.all(queries)
|
||||
|
|
@ -568,12 +560,10 @@ function plugins (settings, deviceId) {
|
|||
)
|
||||
const tradeEntry = massage(_tradeEntry, error)
|
||||
const sql = pgp.helpers.insert(tradeEntry, null, 'trades') + 'RETURNING *'
|
||||
db.tx(async t => {
|
||||
const newTrade = await t.oneOrNone(sql)
|
||||
return recordTradeAndTx(newTrade.id, _tradeEntry.internalTxIdList, t)
|
||||
db.tx(t => {
|
||||
return t.oneOrNone(sql)
|
||||
.then(newTrade => recordTradeAndTx(newTrade.id, _tradeEntry, t))
|
||||
})
|
||||
.then(data => data)
|
||||
.catch(error => error)
|
||||
}
|
||||
|
||||
function sendMessage (rec) {
|
||||
|
|
|
|||
|
|
@ -3,14 +3,14 @@ const db = require('./db')
|
|||
exports.up = function (next) {
|
||||
var sql = [
|
||||
`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),
|
||||
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 (
|
||||
cash_in_tx_id uuid REFERENCES cash_in_txs(id),
|
||||
tx_id uuid REFERENCES cash_in_txs(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)
|
||||
)`
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue