Skip to content

Commit

Permalink
Fix dashboard statistic bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vntuananhbui committed Jan 13, 2024
1 parent 2713b0c commit 9001320
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
57 changes: 57 additions & 0 deletions controllers/compareStatisticController.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,60 @@ exports.getCompareExpanseIncomeTotal = async (req, res, next) => {
}
};

exports.getCompareExpanseIncomeThisMonth = async (req, res, next) => {
try {
const user = req.userID;
const baseCurrency = req.userID.baseCurrency;
const objectIdUserId = new mongoose.Types.ObjectId(user);

const today = new Date();
const startOfCurrentMonth = new Date(today.getFullYear(), today.getMonth(), 1);
const endOfCurrentMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);

const currencyField = baseCurrency === "VND" ? "VND" : "USD";

const totalExpensePerCurrentMonth = await NormalTransaction.aggregate([
{
$match: {
user: objectIdUserId,
date: { $gte: startOfCurrentMonth, $lte: endOfCurrentMonth },
type: "Expense",
},
},
{
$group: {
_id: null,
totalExpense: { $sum: `$${currencyField}` },
},
},
]);

const totalIncomePerCurrentMonth = await NormalTransaction.aggregate([
{
$match: {
user: objectIdUserId,
date: { $gte: startOfCurrentMonth, $lte: endOfCurrentMonth },
type: "Income",
},
},
{
$group: {
_id: null,
totalIncome: { $sum: `$${currencyField}` },
},
},
]);

const totalExpense =
totalExpensePerCurrentMonth.length > 0 ? totalExpensePerCurrentMonth[0].totalExpense : 0;
const totalIncome =
totalIncomePerCurrentMonth.length > 0 ? totalIncomePerCurrentMonth[0].totalIncome : 0;

res.status(200).json({
Expense: totalExpense,
Income: totalIncome,
});
} catch (err) {
next(new ErrorHandler(err.message, 500));
}
};
2 changes: 1 addition & 1 deletion controllers/transactionStatisticController.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ exports.getTransactionStatisticsThisMonth = async (req, res, next) => {
try {
const today = new Date();
const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);

console.log(firstDayOfMonth);
const currencyField = baseCurrency === "VND" ? "VND" : "USD";

const totalPerDay = await NormalTransaction.aggregate([
Expand Down
3 changes: 3 additions & 0 deletions routes/statisticRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ router.get('/catex/lastmonth', isAuthenticated, expenseStatistic.getTotalExpense
//Chart 5: Income/Expense Ratio
router.get('/exin/month', isAuthenticated, compareStatistic.getCompareExpanseIncomeByMonth);

router.get('/exin/thismonth', isAuthenticated, compareStatistic.getCompareExpanseIncomeThisMonth);


router.get('/exin/week', isAuthenticated, compareStatistic.getCompareExpanseIncomeByWeek);

router.get('/exin/total', isAuthenticated, compareStatistic.getCompareExpanseIncomeTotal);
Expand Down

0 comments on commit 9001320

Please sign in to comment.