Skip to content

Commit

Permalink
Add monthly saving in dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
vntuananhbui committed Jan 10, 2024
1 parent 4d519c1 commit 0679ef8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions controllers/savingGoalStatisticController.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,55 @@ exports.getLastMonthSaving = async (req, res, next) => {
}
};


exports.getTotalMonthlySaving = async (req, res, next) => {
try {
const baseCurrency = req.userID.baseCurrency;
const user = req.userID;
const objectIdUserId = new mongoose.Types.ObjectId(user);
const currencyField = baseCurrency === "VND" ? "VND" : "USD";

const date = new Date(); // Current date
const startOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
const endOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);

const totalMonthlySaving = await NormalTransaction.aggregate([
{
$match: {
user: objectIdUserId,
type: "Saving",
date: { $gte: startOfMonth, $lte: endOfMonth },
},
},
{
$group: {
_id: null,
totalVND: {
$sum: { $cond: [{ $eq: ["$currency", "VND"] }, "$VND", 0] },
},
totalUSD: {
$sum: { $cond: [{ $eq: ["$currency", "USD"] }, "$USD", 0] },
},
},
},
{
$project: {
_id: 0,
totalMonthlySaving: {
$cond: [{ $eq: [baseCurrency, "VND"] }, "$totalVND", "$totalUSD"],
},
},
},
]);

const total = totalMonthlySaving.length > 0 ? totalMonthlySaving[0].totalMonthlySaving : 0;

res.status(200).json({
totalMonthlySaving: total,
});
} catch (err) {
next(new ErrorHandler(err.message, 500));
}
};


1 change: 1 addition & 0 deletions routes/statisticDetailRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ router.get('/wallet/lastmonth/:walletID',isAuthenticated, statisticWalletDetail.
router.get('/savinggoal/:savingID',isAuthenticated, statisticSavingGoal.getTotalAndTargetByDate);
router.get('/savinggoal/thismonth/:savingID',isAuthenticated, statisticSavingGoal.getThisMonthSaving);
router.get('/savinggoal/lastmonth/:savingID',isAuthenticated, statisticSavingGoal.getLastMonthSaving);
router.get('/totalsaving',isAuthenticated, statisticSavingGoal.getTotalMonthlySaving);

module.exports = router;

0 comments on commit 0679ef8

Please sign in to comment.