diff --git a/lib/new-admin/graphql/schema.js b/lib/new-admin/graphql/schema.js index 20ea2dca..f4756ab9 100644 --- a/lib/new-admin/graphql/schema.js +++ b/lib/new-admin/graphql/schema.js @@ -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(), diff --git a/lib/new-admin/transactions.js b/lib/new-admin/transactions.js index 208e13f5..917ca0c1 100644 --- a/lib/new-admin/transactions.js +++ b/lib/new-admin/transactions.js @@ -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 => { diff --git a/new-lamassu-admin/src/components/LogsDownloaderPopper.js b/new-lamassu-admin/src/components/LogsDownloaderPopper.js index 69493677..1b6d6b3a 100644 --- a/new-lamassu-admin/src/components/LogsDownloaderPopper.js +++ b/new-lamassu-admin/src/components/LogsDownloaderPopper.js @@ -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 = ({ )} -
- -
+ {simplified && ( +
+ +
+ )}
downloadLogs(range, args)}> Download diff --git a/new-lamassu-admin/src/pages/Transactions/Transactions.js b/new-lamassu-admin/src/pages/Transactions/Transactions.js index 83e45dc8..a5caf18c 100644 --- a/new-lamassu-admin/src/pages/Transactions/Transactions.js +++ b/new-lamassu-admin/src/pages/Transactions/Transactions.js @@ -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 = () => { R.path(['transactionsCsv'])(logs)} - getSimplifiedLogs={logs => - R.path(['simplifiedTransactionsCsv'])(logs) - } + simplified />
)}