require("dotenv").config(); const noblox = require('noblox.js'); const payoutPercentages = { "Honeywell Vista Device Pack": .6 } noblox.setCookie(process.env.COOKIE).then(() => { noblox.getGroupTransactions(process.env.GROUPID, "Sale").then((transactions) => { // Calculate the first and last days of the previous month const now = new Date(); const firstDayOfLastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1); const lastDayOfLastMonth = new Date(now.getFullYear(), now.getMonth(), 0); // Last day of the previous month // Filter transactions to those within the previous month const previousMonthTransactions = transactions.filter((transaction) => { const transactionDate = new Date(transaction.created); //console.log(transaction) return transactionDate >= firstDayOfLastMonth && transactionDate <= lastDayOfLastMonth && transaction.currency.amount > 0; }); console.log(previousMonthTransactions); // Calculate total revenue const totalRevenue = previousMonthTransactions.reduce((acc, transaction) => acc + transaction.currency.amount, 0); // Calculate total revenue per product const totalRevPerProduct = previousMonthTransactions.reduce((acc, transaction) => { if (!acc[transaction.details.name]) { acc[transaction.details.name] = 0; } acc[transaction.details.name] += transaction.currency.amount; return acc; }, {}); // Calculate percentage to pay out per product const payouts = Object.keys(totalRevPerProduct).reduce((acc, product) => { if (!payoutPercentages[product]) { acc[product] = 0; } else { acc[product] = Math.floor(totalRevPerProduct[product] * payoutPercentages[product]); } return acc; }, {}); // Get list of people that bought products const buyers = previousMonthTransactions.reduce((acc, transaction) => { if (!acc[transaction.agent.id]) { acc[transaction.agent.id] = { name: transaction.agent.name, total: 0, products: [] }; } acc[transaction.agent.id].total += transaction.currency.amount; acc[transaction.agent.id].products.push({ name: transaction.details.name, amount: transaction.currency.amount }); return acc; }, {}); console.log("Buyers"); console.log(JSON.stringify(buyers, null, 2)); console.log("Rev"); console.log(totalRevPerProduct); console.log("Payouts"); console.log(payouts); console.log(`Total Revenue: ${totalRevenue}`); // Print a single line for every transaction }); });