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.

133 lines (95 loc) 4.07 kB
const Transaction = require('./models/Transaction'); const Budget = require('./models/Budget'); class SakshBudgetManagement { constructor(budgetModel = Budget, transactionModel = Transaction) { this.Budget = budgetModel; this.Transaction = transactionModel; } async sakshSetBudget(userId, category, amount, period) { const startDate = new Date(); let endDate; if (period === 'daily') { endDate = new Date(startDate); endDate.setDate(startDate.getDate() + 1); } else if (period === 'weekly') { endDate = new Date(startDate); endDate.setDate(startDate.getDate() + 7); } else if (period === 'monthly') { endDate = new Date(startDate); endDate.setMonth(startDate.getMonth() + 1); } // Check for overlapping budgets const overlappingBudget = await this.Budget.findOne({ userId, category, period, $or: [ { startDate: { $lte: endDate }, endDate: { $gte: startDate } } ] }); if (overlappingBudget) { throw new Error('A budget for this category and period already exists.'); } const budget = new this.Budget({ userId, category, amount, period, startDate, endDate }); await budget.save(); } async sakshAdjustBudget(userId, category, newAmount) { const budget = await this.Budget.findOne({ userId, category }); if (budget) { budget.history.push({ amount: budget.amount, date: new Date() }); budget.amount = newAmount; await budget.save(); } } async sakshCheckBudget(userId, category, amount) { const user = await this.User.findOne({ userId }); if (user && user.budgetEnabled === false) { return; // Skip budget check if budget is disabled for the user } const budgets = await this.Budget.find({ userId, category }); for (const budget of budgets) { const spent = await this.Transaction.aggregate([ { $match: { userId, category, date: { $gte: budget.startDate, $lte: budget.endDate } } }, { $group: { _id: null, total: { $sum: '$amount' } } } ]); if (spent.length > 0 && spent[0].total + amount > budget.amount) { this.emit('budgetExceeded', { userId, category, amountSpent: spent[0].total + amount, budgetAmount: budget.amount }); throw new Error('Budget exceeded'); } } } async sakshGetBudgetStatus(userId, category) { const budgets = await this.Budget.find({ userId, category }); const budgetStatus = []; for (const budget of budgets) { const spent = await this.Transaction.aggregate([ { $match: { userId, category, date: { $gte: budget.startDate, $lte: budget.endDate } } }, { $group: { _id: null, total: { $sum: '$amount' } } } ]); const totalSpent = spent.length > 0 ? spent[0].total : 0; const availableBudget = budget.amount - totalSpent; budgetStatus.push({ category: budget.category, totalBudget: budget.amount, totalSpent, availableBudget, period: budget.period, startDate: budget.startDate, endDate: budget.endDate }); } return budgetStatus; } async sakshToggleBudget(userId, enable) { const user = await this.User.findOne({ userId }); if (user) { user.budgetEnabled = enable; await user.save(); } } } module.exports = SakshBudgetManagement;