Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const lc = require('./log_converters.js')
- /* Returns a list of objects that contains one field for the store name and the number of times Coopay has been used for the given store */
- async function getNumberOfPaymentsByStore (numberOfResults) {
- const tmp = await lc.getPaymentResults()
- const payments = tmp.success
- const resultDataStructure = await lc.structureResultData(payments, 0)
- let result = resultDataStructure.result
- let stores = resultDataStructure.stores
- for (let i = 0; i < result.length; i++) {
- let storeName = result[i].storeName
- stores.forEach(e => { if (e === storeName) result[i].data++ })
- }
- result.sort((obj1, obj2) => { return obj2.data - obj1.data })
- return result.slice(0, numberOfResults)
- }
- /* Returns a list of objects that contains one field for the store name and the biggest sum payed with Coopay in that given store */
- async function getBiggestShoppingSumByStore (numberOfStores) {
- const tmp = await lc.getPaymentResults()
- const payments = tmp.success
- const removeComma = input => input.replace(/,/g, '')
- const storesWithResults = payments.map(root => root.confirmation || root).reduce((aggregate, transaction) => {
- const storeNumber = parseInt(transaction.storeNumber)
- if (!aggregate.hasOwnProperty(storeNumber)) {
- aggregate[storeNumber] = {
- storeName: transaction.storeName.replace(/,/g, ' '),
- storeNumber: storeNumber,
- largestPayment: transaction.totalSum,
- timestamp: transaction.dateTime
- }
- }
- if (parseFloat(removeComma(aggregate[storeNumber].largestPayment)) < parseFloat(removeComma(transaction.totalSum))) {
- aggregate[storeNumber].largestPayment = transaction.totalSum
- aggregate[storeNumber].timestamp = transaction.dateTime
- }
- return aggregate
- }, {})
- return Object.values(storesWithResults)
- .sort((a, b) => parseFloat(removeComma(a.largestPayment)) < parseFloat(removeComma(b.largestPayment)) ? 1 : -1)
- .slice(0, numberOfStores)
- }
- /* Returns a numeric value representing number of payments done with Coopay the last hour */
- async function getNumberOfPaymentsLastHour () {
- const tmp = await lc.getPaymentResults()
- const currentHour = () => {
- return new Date().getHours() < 10 ? "0" + new Date().getHours().toString() : new Date().getHours().toString()
- }
- const numberOfPaymentsLastHour = []
- tmp.success.forEach(payment => {
- if (payment.dateTime.split(',').pop().substring(0, 2) === currentHour()) numberOfPaymentsLastHour.push(payment)
- })
- return numberOfPaymentsLastHour.length
- }
- /* Returns a numeric value representing the number of failed payments today */
- async function getNumberOfFailedTransactionsToday () {
- const result = await lc.getPaymentResults()
- let count = 0
- result.failed.forEach(e => {
- if (e.result === 'TRANSACTION_FAILED') count++
- })
- return count
- }
- module.exports = {
- getNumberOfPaymentsByStore,
- getNumberOfPaymentsLastHour,
- getBiggestShoppingSumByStore,
- getNumberOfFailedTransactionsToday
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement