From 5826a29c530c08db88aab74001f176335348eb64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Wed, 18 Aug 2021 13:26:24 +0100 Subject: [PATCH] feat: simplified tx exports --- lib/new-admin/graphql/schema.js | 3 ++ lib/new-admin/transactions.js | 44 ++++++++++++++- .../src/components/LogsDownloaderPopper.js | 53 +++++++++++++++---- .../src/pages/Transactions/Transactions.js | 14 ++++- 4 files changed, 102 insertions(+), 12 deletions(-) diff --git a/lib/new-admin/graphql/schema.js b/lib/new-admin/graphql/schema.js index c57bae2f..20ea2dca 100644 --- a/lib/new-admin/graphql/schema.js +++ b/lib/new-admin/graphql/schema.js @@ -302,6 +302,7 @@ const typeDefs = gql` deviceId: ID ): [Transaction] transactionsCsv(from: Date, until: Date, limit: Int, offset: Int): String + simplifiedTransactionsCsv(from: Date, until: Date, limit: Int, offset: Int): String accounts: JSONObject config: JSONObject blacklist: [Blacklist] @@ -399,6 +400,8 @@ const resolvers = { 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), config: () => settingsLoader.loadLatestConfigOrNone(), accounts: () => settingsLoader.showAccounts(), blacklist: () => blacklist.getBlacklist(), diff --git a/lib/new-admin/transactions.js b/lib/new-admin/transactions.js index c7208961..208e13f5 100644 --- a/lib/new-admin/transactions.js +++ b/lib/new-admin/transactions.js @@ -6,6 +6,8 @@ const machineLoader = require('../machine-loader') const tx = require('../tx') const cashInTx = require('../cash-in/cash-in-tx') const { REDEEMABLE_AGE } = require('../cash-out/cash-out-helper') +const coinUtils = require('../coin-utils') +const BN = require('../bn') const NUM_RESULTS = 1000 @@ -76,6 +78,46 @@ function batch (from = new Date(0).toISOString(), until = new Date().toISOString ]).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) + }) +} + +const getCryptoAmount = it => { return { ...it, cryptoAmount: 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 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' } +} + +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' } +} + +const getStatus = it => { + if (it.txClass === 'cashOut') { + return getCashOutStatus(it) + } + return getCashInStatus(it) +} + function getCustomerTransactionsBatch (ids) { const packager = _.flow(it => { return it @@ -168,4 +210,4 @@ function cancel (txId) { .then(() => single(txId)) } -module.exports = { batch, single, cancel, getCustomerTransactionsBatch } +module.exports = { simplifiedBatch, batch, single, cancel, getCustomerTransactionsBatch } diff --git a/new-lamassu-admin/src/components/LogsDownloaderPopper.js b/new-lamassu-admin/src/components/LogsDownloaderPopper.js index 79ca89d5..69493677 100644 --- a/new-lamassu-admin/src/components/LogsDownloaderPopper.js +++ b/new-lamassu-admin/src/components/LogsDownloaderPopper.js @@ -128,15 +128,30 @@ const styles = { const useStyles = makeStyles(styles) const ALL = 'all' const RANGE = 'range' +const ADVANCED = 'advanced' +const SIMPLIFIED = 'simplified' -const LogsDownloaderPopover = ({ name, query, args, title, getLogs }) => { +const LogsDownloaderPopover = ({ + name, + queries, + args, + title, + getLogs, + getSimplifiedLogs +}) => { 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(query, { + const [fetchLogs] = useLazyQuery(queries[0], { onCompleted: data => createLogsFile(getLogs(data), range) }) + const [fetchSimplifiedLogs] = useLazyQuery(queries[1], { + onCompleted: data => createLogsFile(getSimplifiedLogs(data), range) + }) + const classes = useStyles() const dateRangePickerClasses = { @@ -150,6 +165,12 @@ const LogsDownloaderPopover = ({ name, query, args, title, getLogs }) => { if (selectedRadio === ALL) setRange({ from: null, until: null }) } + const handleAdvancedRadioButtons = evt => { + const selectedAdvancedRadio = R.path(['target', 'value'])(evt) + setSelectedAdvancedRadio(selectedAdvancedRadio) + if (selectedAdvancedRadio === ALL) setRange({ from: null, until: null }) + } + const handleRangeChange = useCallback( (from, until) => { setRange({ from, until }) @@ -157,9 +178,11 @@ const LogsDownloaderPopover = ({ name, query, args, title, getLogs }) => { [setRange] ) - const downloadLogs = (range, args, fetchLogs) => { + const downloadLogs = (range, args) => { + const fetch = + selectedAdvancedRadio === SIMPLIFIED ? fetchSimplifiedLogs : fetchLogs if (selectedRadio === ALL) { - fetchLogs({ + fetch({ variables: { ...args } @@ -170,7 +193,7 @@ const LogsDownloaderPopover = ({ name, query, args, title, getLogs }) => { if (range.from && !range.until) range.until = moment() if (selectedRadio === RANGE) { - fetchLogs({ + fetch({ variables: { ...args, from: range.from, @@ -209,7 +232,9 @@ const LogsDownloaderPopover = ({ name, query, args, title, getLogs }) => { const radioButtonOptions = [ { display: 'All logs', code: ALL }, - { display: 'Date range', code: RANGE } + { display: 'Date range', code: RANGE }, + { display: 'Advanced logs', code: ADVANCED }, + { display: 'Simplified logs', code: SIMPLIFIED } ] const open = Boolean(anchorEl) @@ -231,7 +256,7 @@ const LogsDownloaderPopover = ({ name, query, args, title, getLogs }) => { { /> )} +
+ +
- downloadLogs(range, args, fetchLogs)}> + 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 811a5c65..83e45dc8 100644 --- a/new-lamassu-admin/src/pages/Transactions/Transactions.js +++ b/new-lamassu-admin/src/pages/Transactions/Transactions.js @@ -29,6 +29,12 @@ const GET_TRANSACTIONS_CSV = gql` } ` +const GET_SIMPLIFIED_TRANSACTIONS_CSV = gql` + query transactions($limit: Int, $from: Date, $until: Date) { + simplifiedTransactionsCsv(limit: $limit, from: $from, until: $until) + } +` + const GET_TRANSACTIONS = gql` query transactions($limit: Int, $from: Date, $until: Date) { transactions(limit: $limit, from: $from, until: $until) { @@ -167,8 +173,14 @@ const Transactions = () => { R.path(['transactionsCsv'])(logs)} + getSimplifiedLogs={logs => + R.path(['simplifiedTransactionsCsv'])(logs) + } /> )}