refactor: reuse tx to csv gql query and overall function simplification

This commit is contained in:
José Oliveira 2021-08-26 13:23:52 +01:00 committed by Josh Harvey
parent 5826a29c53
commit fe518b21d7
4 changed files with 74 additions and 69 deletions

View file

@ -301,8 +301,7 @@ const typeDefs = gql`
offset: Int
deviceId: ID
): [Transaction]
transactionsCsv(from: Date, until: Date, limit: Int, offset: Int): String
simplifiedTransactionsCsv(from: Date, until: Date, limit: Int, offset: Int): String
transactionsCsv(from: Date, until: Date, limit: Int, offset: Int, simplified: Boolean): String
accounts: JSONObject
config: JSONObject
blacklist: [Blacklist]
@ -398,10 +397,8 @@ const resolvers = {
serverLogs.getServerLogs(from, until, limit, offset).then(parseAsync),
transactions: (...[, { from, until, limit, offset, deviceId }]) =>
transactions.batch(from, until, limit, offset, deviceId),
transactionsCsv: (...[, { from, until, limit, offset }]) =>
transactions.batch(from, until, limit, offset).then(parseAsync),
simplifiedTransactionsCsv: (...[, { from, until, limit, offset }]) =>
transactions.simplifiedBatch(from, until, limit, offset).then(parseAsync),
transactionsCsv: (...[, { from, until, limit, offset, simplified }]) =>
transactions.batch(from, until, limit, offset, null, simplified).then(parseAsync),
config: () => settingsLoader.loadLatestConfigOrNone(),
accounts: () => settingsLoader.showAccounts(),
blacklist: () => blacklist.getBlacklist(),

View file

@ -26,7 +26,7 @@ function addNames (txs) {
const camelize = _.mapKeys(_.camelCase)
function batch (from = new Date(0).toISOString(), until = new Date().toISOString(), limit = null, offset = 0, id = null) {
function batch (from = new Date(0).toISOString(), until = new Date().toISOString(), limit = null, offset = 0, id = null, simplified = false) {
const packager = _.flow(_.flatten, _.orderBy(_.property('created'), ['desc']), _.map(camelize), addNames)
const cashInSql = `select 'cashIn' as tx_class, txs.*,
@ -76,39 +76,48 @@ function batch (from = new Date(0).toISOString(), until = new Date().toISOString
]),
db.any(cashOutSql, [REDEEMABLE_AGE, from, until, limit, offset, id])
]).then(packager)
}
function simplifiedBatch (from = new Date(0).toISOString(), until = new Date().toISOString(), limit = null, offset = 0) {
return batch(from, until, limit, offset)
.then(res => {
const fields = ['txClass', 'id', 'created', 'machineName',
'cryptoCode', 'fiat', 'fiatCode', 'phone', 'toAddress',
'txHash', 'dispense', 'error', 'status', 'fiatProfit', 'cryptoAmount']
return _.map(_.compose(_.pick(fields), getStatus, getProfit, getCryptoAmount))(res)
if (simplified) return simplifiedBatch(res)
else return res
})
}
const getCryptoAmount = it => { return { ...it, cryptoAmount: coinUtils.toUnit(BN(it.cryptoAtoms), it.cryptoCode).toString() } }
function simplifiedBatch (data) {
const fields = ['txClass', 'id', 'created', 'machineName',
'cryptoCode', 'fiat', 'fiatCode', 'phone', 'toAddress',
'txHash', 'dispense', 'error', 'status', 'fiatProfit', 'cryptoAmount']
const addSimplifiedFields = _.map(it => ({
...it,
status: getStatus(it),
fiatProfit: getProfit(it).toString(),
cryptoAmount: getCryptoAmount(it)
}))
return _.compose(_.map(_.pick(fields)), addSimplifiedFields)(data)
}
const getCryptoAmount = it => coinUtils.toUnit(BN(it.cryptoAtoms), it.cryptoCode).toString()
const getProfit = it => {
const getCommissionFee = it => Number.parseFloat(it.commissionPercentage) * Number.parseFloat(it.fiat)
if (!it.cashInFee) return { ...it, fiatProfit: getCommissionFee(it) }
return { ...it, fiatProfit: getCommissionFee(it) + Number.parseFloat(it.cashInFee) }
const getCommissionFee = it => BN(it.commissionPercentage).mul(BN(it.fiat))
if (!it.cashInFee) return getCommissionFee(it)
return getCommissionFee(it).add(BN(it.cashInFee))
}
const getCashOutStatus = it => {
if (it.hasError) return { ...it, status: 'Error' }
if (it.dispense) return { ...it, status: 'Success' }
if (it.expired) return { ...it, status: 'Expired' }
return { ...it, status: 'Pending' }
if (it.hasError) return 'Error'
if (it.dispense) return 'Success'
if (it.expired) return 'Expired'
return 'Pending'
}
const getCashInStatus = it => {
if (it.operatorCompleted) return { ...it, status: 'Cancelled' }
if (it.hasError) return { ...it, status: 'Error' }
if (it.sendConfirmed) return { ...it, status: 'Sent' }
if (it.expired) return { ...it, status: 'Expired' }
return { ...it, status: 'Pending' }
if (it.operatorCompleted) return 'Cancelled'
if (it.hasError) return 'Error'
if (it.sendConfirmed) return 'Sent'
if (it.expired) return 'Expired'
return 'Pending'
}
const getStatus = it => {