Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions backend/controllers/transactionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,25 @@ const getChartData = async (req, res) => {
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);

// Optional date range from query for category aggregations
const { startDate, endDate } = req.query;
let dateMatch = {};
if (startDate || endDate) {
dateMatch.addedOn = {};
if (startDate) dateMatch.addedOn.$gte = new Date(startDate);
if (endDate) dateMatch.addedOn.$lte = new Date(endDate);
}

// Data for Expenses by Category (Pie Chart)
const expensesByCategory = await IncomeExpense.aggregate([
{ $match: { user: userId, isIncome: false, isDeleted: false } },
{ $match: { user: userId, isIncome: false, isDeleted: false, ...(dateMatch.addedOn ? { addedOn: dateMatch.addedOn } : {}) } },
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spread operator pattern ...(dateMatch.addedOn ? { addedOn: dateMatch.addedOn } : {}) adds complexity and could be simplified. Consider restructuring to build the match object conditionally before the aggregation, which improves readability and maintainability.

Copilot uses AI. Check for mistakes.
{ $group: { _id: '$category', total: { $sum: '$cost' } } },
{ $project: { name: '$_id', total: 1, _id: 0 } }
]);

// Data for Income by Category (Pie Chart)
const incomeByCategory = await IncomeExpense.aggregate([
{ $match: { user: userId, isIncome: true, isDeleted: false, ...(dateMatch.addedOn ? { addedOn: dateMatch.addedOn } : {}) } },
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spread operator pattern ...(dateMatch.addedOn ? { addedOn: dateMatch.addedOn } : {}) adds complexity and could be simplified. Consider restructuring to build the match object conditionally before the aggregation, which improves readability and maintainability.

Copilot uses AI. Check for mistakes.
{ $group: { _id: '$category', total: { $sum: '$cost' } } },
{ $project: { name: '$_id', total: 1, _id: 0 } }
]);
Expand Down Expand Up @@ -234,7 +250,7 @@ const getChartData = async (req, res) => {
{ $project: { date: '$_id', total: 1, _id: 0 } }
]);

res.json({ expensesByCategory, expensesOverTime, incomeOverTime });
res.json({ expensesByCategory, incomeByCategory, expensesOverTime, incomeOverTime });
} catch (error) {
// Also log the error to the backend console for easier debugging
console.error('Error in getChartData:', error);
Expand Down
6 changes: 6 additions & 0 deletions backend/models/IncomeExpense.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ const incomeExpenseSchema = new mongoose.Schema({
timestamps: true,
});

// Helpful indexes for common filters and aggregations
// Queries often filter by user, isIncome, isDeleted, date, and group by category
incomeExpenseSchema.index({ user: 1, isDeleted: 1, isIncome: 1, addedOn: -1 });
incomeExpenseSchema.index({ user: 1, isDeleted: 1, category: 1 });
incomeExpenseSchema.index({ user: 1, isDeleted: 1, addedOn: -1 });

module.exports = mongoose.model('IncomeExpense', incomeExpenseSchema);
Loading
Loading