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 => {

View file

@ -133,25 +133,21 @@ const SIMPLIFIED = 'simplified'
const LogsDownloaderPopover = ({
name,
queries,
query,
args,
title,
getLogs,
getSimplifiedLogs
simplified
}) => {
const [selectedRadio, setSelectedRadio] = useState(ALL)
const [selectedAdvancedRadio, setSelectedAdvancedRadio] = useState(ADVANCED)
const [range, setRange] = useState({ from: null, until: null })
const [anchorEl, setAnchorEl] = useState(null)
const [fetchLogs] = useLazyQuery(queries[0], {
const [fetchLogs] = useLazyQuery(query, {
onCompleted: data => createLogsFile(getLogs(data), range)
})
const [fetchSimplifiedLogs] = useLazyQuery(queries[1], {
onCompleted: data => createLogsFile(getSimplifiedLogs(data), range)
})
const classes = useStyles()
const dateRangePickerClasses = {
@ -168,7 +164,6 @@ const LogsDownloaderPopover = ({
const handleAdvancedRadioButtons = evt => {
const selectedAdvancedRadio = R.path(['target', 'value'])(evt)
setSelectedAdvancedRadio(selectedAdvancedRadio)
if (selectedAdvancedRadio === ALL) setRange({ from: null, until: null })
}
const handleRangeChange = useCallback(
@ -179,12 +174,11 @@ const LogsDownloaderPopover = ({
)
const downloadLogs = (range, args) => {
const fetch =
selectedAdvancedRadio === SIMPLIFIED ? fetchSimplifiedLogs : fetchLogs
if (selectedRadio === ALL) {
fetch({
fetchLogs({
variables: {
...args
...args,
simplified: selectedAdvancedRadio === SIMPLIFIED
}
})
}
@ -193,11 +187,12 @@ const LogsDownloaderPopover = ({
if (range.from && !range.until) range.until = moment()
if (selectedRadio === RANGE) {
fetch({
fetchLogs({
variables: {
...args,
from: range.from,
until: range.until
until: range.until,
simplified: selectedAdvancedRadio === SIMPLIFIED
}
})
}
@ -232,7 +227,10 @@ const LogsDownloaderPopover = ({
const radioButtonOptions = [
{ display: 'All logs', code: ALL },
{ display: 'Date range', code: RANGE },
{ display: 'Date range', code: RANGE }
]
const advancedRadioButtonOptions = [
{ display: 'Advanced logs', code: ADVANCED },
{ display: 'Simplified logs', code: SIMPLIFIED }
]
@ -256,7 +254,7 @@ const LogsDownloaderPopover = ({
<RadioGroup
name="logs-select"
value={selectedRadio}
options={radioButtonOptions.slice(0, 2)}
options={radioButtonOptions}
ariaLabel="logs-select"
onChange={handleRadioButtons}
className={classes.radioButtons}
@ -281,16 +279,18 @@ const LogsDownloaderPopover = ({
/>
</div>
)}
{simplified && (
<div className={classes.radioButtonsContainer}>
<RadioGroup
name="simplified-tx-logs"
value={selectedAdvancedRadio}
options={radioButtonOptions.slice(2, 4)}
options={advancedRadioButtonOptions}
ariaLabel="simplified-tx-logs"
onChange={handleAdvancedRadioButtons}
className={classes.radioButtons}
/>
</div>
)}
<div className={classes.download}>
<Link color="primary" onClick={() => downloadLogs(range, args)}>
Download

View file

@ -24,14 +24,18 @@ const useStyles = makeStyles(mainStyles)
const NUM_LOG_RESULTS = 1000
const GET_TRANSACTIONS_CSV = gql`
query transactions($limit: Int, $from: Date, $until: Date) {
transactionsCsv(limit: $limit, from: $from, until: $until)
}
`
const GET_SIMPLIFIED_TRANSACTIONS_CSV = gql`
query transactions($limit: Int, $from: Date, $until: Date) {
simplifiedTransactionsCsv(limit: $limit, from: $from, until: $until)
query transactions(
$simplified: Boolean
$limit: Int
$from: Date
$until: Date
) {
transactionsCsv(
simplified: $simplified
limit: $limit
from: $from
until: $until
)
}
`
@ -173,14 +177,9 @@ const Transactions = () => {
<LogsDowloaderPopover
title="Download logs"
name="transactions"
queries={[
GET_TRANSACTIONS_CSV,
GET_SIMPLIFIED_TRANSACTIONS_CSV
]}
query={GET_TRANSACTIONS_CSV}
getLogs={logs => R.path(['transactionsCsv'])(logs)}
getSimplifiedLogs={logs =>
R.path(['simplifiedTransactionsCsv'])(logs)
}
simplified
/>
</div>
)}