UNPKG

saksh-wallet

Version:

A Node.js library for managing user wallets, including functionalities for crediting, debiting, and converting currencies, as well as retrieving balances and transaction reports.

205 lines (186 loc) 7.93 kB
const Transaction = require('./models/Transaction'); const WalletUser = require('./models/WalletUser'); class SakshReporting { constructor(userModel = WalletUser, transactionModel = Transaction) { this.User = userModel; this.Transaction = transactionModel; } /** * Get balance summary for a user. * @param {String} userId - The ID of the user. * @returns {Object} - The balance summary of the user. */ async sakshGetBalanceSummary(userId) { let user = await this.User.findOne({ userId }); if (!user) { user = new this.User({ userId }); } return user.balances; } /** * Get monthly transaction report for a user. * @param {String} userId - The ID of the user. * @param {Number} year - The year of the report. * @param {Number} month - The month of the report. * @returns {Array} - The list of transactions for the specified month. */ async sakshGetMonthlyTransactionReport(userId, year, month) { const start = new Date(year, month - 1, 1); const end = new Date(year, month, 0, 23, 59, 59, 999); return await this.Transaction.find({ userId, date: { $gte: start, $lte: end } }); } /** * Get daily transaction report for a user. * @param {String} userId - The ID of the user. * @param {Date} date - The date of the report. * @returns {Array} - The list of transactions for the specified date. */ async sakshGetDailyTransactionReport(userId, date) { const start = new Date(date.setHours(0, 0, 0, 0)); const end = new Date(date.setHours(23, 59, 59, 999)); return await this.Transaction.find({ userId, date: { $gte: start, $lte: end } }); } /** * Get yearly transaction report for a user. * @param {String} userId - The ID of the user. * @param {Number} year - The year of the report. * @returns {Array} - The list of transactions for the specified year. */ async sakshGetYearlyTransactionReport(userId, year) { const start = new Date(year, 0, 1); const end = new Date(year, 11, 31, 23, 59, 59, 999); return await this.Transaction.find({ userId, date: { $gte: start, $lte: end } }); } /** * Get transactions by category for a user. * @param {String} userId - The ID of the user. * @param {String} category - The category of the transactions. * @returns {Array} - The list of transactions for the specified category. */ async sakshGetTransactionsByCategory(userId, category) { return await this.Transaction.find({ userId, category }); } /** * Get spending summary for a user over a specified period. * @param {String} userId - The ID of the user. * @param {Date} startDate - The start date of the period. * @param {Date} endDate - The end date of the period. * @returns {Object} - The spending summary for the specified period. */ async sakshGetSpendingSummary(userId, startDate, endDate) { const transactions = await this.Transaction.find({ userId, date: { $gte: startDate, $lte: endDate } }); const summary = transactions.reduce((acc, tx) => { acc.total += tx.amount; acc[tx.currency] = (acc[tx.currency] || 0) + tx.amount; return acc; }, { total: 0 }); return summary; } /** * Get transaction count for a user. * @param {String} userId - The ID of the user. * @returns {Number} - The total number of transactions. */ async sakshGetTransactionCount(userId) { return await this.Transaction.countDocuments({ userId }); } /** * Get average transaction amount for a user over a specified period. * @param {String} userId - The ID of the user. * @param {Date} startDate - The start date of the period. * @param {Date} endDate - The end date of the period. * @returns {Number} - The average transaction amount. */ async sakshGetAverageTransactionAmount(userId, startDate, endDate) { const transactions = await this.Transaction.find({ userId, date: { $gte: startDate, $lte: endDate } }); const totalAmount = transactions.reduce((acc, tx) => acc + tx.amount, 0); return totalAmount / transactions.length; } /** * Get the largest transaction for a user. * @param {String} userId - The ID of the user. * @returns {Object} - The largest transaction. */ async sakshGetLargestTransaction(userId) { return await this.Transaction.findOne({ userId }).sort({ amount: -1 }); } /** * Get the smallest transaction for a user. * @param {String} userId - The ID of the user. * @returns {Object} - The smallest transaction. */ async sakshGetSmallestTransaction(userId) { return await this.Transaction.findOne({ userId }).sort({ amount: 1 }); } /** * Get transactions by payment method for a user. * @param {String} userId - The ID of the user. * @param {String} paymentMethod - The payment method of the transactions. * @returns {Array} - The list of transactions for the specified payment method. */ async sakshGetTransactionsByPaymentMethod(userId, paymentMethod) { return await this.Transaction.find({ userId, paymentMethod }); } /** * Get balance summary for a user by currency. * @param {String} userId - The ID of the user. * @returns {Object} - The balance summary of the user by currency. */ async sakshGetBalanceByCurrency(userId) { let user = await this.User.findOne({ userId }); if (!user) { user = new this.User({ userId }); } const balancesByCurrency = user.balances.reduce((acc, balance) => { acc[balance.currency] = (acc[balance.currency] || 0) + balance.amount; return acc; }, {}); return balancesByCurrency; } /** * Get balance summary for all users by currency. * @returns {Object} - The balance summary of all users by currency. */ async sakshGetAllUsersBalanceByCurrency() { const users = await this.User.find({}); const balancesByCurrency = users.reduce((acc, user) => { user.balances.forEach(balance => { acc[balance.currency] = (acc[balance.currency] || 0) + balance.amount; }); return acc; }, {}); return balancesByCurrency; } /** * Get balance summary for all users by category. * @returns {Object} - The balance summary of all users by category. */ async sakshGetAllUsersBalanceByCategory() { const users = await this.User.find({}); const balancesByCategory = users.reduce((acc, user) => { user.balances.forEach(balance => { acc[balance.category] = (acc[balance.category] || 0) + balance.amount; }); return acc; }, {}); return balancesByCategory; } /** * Get balance summary for all users at a given date. * @param {Date} date - The date for which to get the balance summary. * @returns {Object} - The balance summary of all users at the given date. */ async sakshGetAllUsersBalanceAtDate(date) { const users = await this.User.find({}); const balancesAtDate = users.reduce((acc, user) => { user.balances.forEach(balance => { if (new Date(balance.date) <= date) { acc[balance.currency] = (acc[balance.currency] || 0) + balance.amount; } }); return acc; }, {}); return balancesAtDate; } } module.exports = SakshReporting;