Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrum 182 create chart logic for details page wallet category planning #27

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
update detail statistic saving
  • Loading branch information
vntuananhbui committed Jan 9, 2024
commit ea7d64ec3f6970a3573bba4fdaff91d7941663ea
102 changes: 102 additions & 0 deletions controllers/savingGoalStatisticController.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 44,105 @@ exports.getTotalAndTargetByDate = async (req, res, next) => {
next(new ErrorHandler(err.message, 500));
}
};

exports.getThisMonthSaving = async (req, res, next) => {
const user = req.userID;
const { savingID } = req.params;

try {
const currentDate = new Date();
const startOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
const endOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() 1, 0);

const transactionsThisMonth = await NormalTransaction.aggregate([
{
$match: {
user: new mongoose.Types.ObjectId(user),
saving: new mongoose.Types.ObjectId(savingID),
date: {
$gte: startOfMonth,
$lte: endOfMonth,
},
},
},
{
$group: {
_id: { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
total: { $sum: "$VND" },
},
},
]);

const savingGoal = await Saving.findById(savingID);
if (!savingGoal) {
return next(new ErrorHandler("Saving goal not found", 404));
}

const thisMonthTotal = transactionsThisMonth.map((transaction) => {
const { _id, total } = transaction;
const targetDate = _id;
const target = savingGoal.target || 0; // Assuming target is stored in the SavingGoal model
return {
date: targetDate,
target: target,
total: total,
};
});

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


exports.getLastMonthSaving = async (req, res, next) => {
const user = req.userID;
const { savingID } = req.params;

try {
const currentDate = new Date();
const startOfLastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1);
const endOfLastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0);

const transactionsLastMonth = await NormalTransaction.aggregate([
{
$match: {
user: new mongoose.Types.ObjectId(user),
saving: new mongoose.Types.ObjectId(savingID),
date: {
$gte: startOfLastMonth,
$lte: endOfLastMonth,
},
},
},
{
$group: {
_id: { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
total: { $sum: "$VND" },
},
},
]);

const savingGoal = await Saving.findById(savingID);
if (!savingGoal) {
return next(new ErrorHandler("Saving goal not found", 404));
}

const lastMonthTotal = transactionsLastMonth.map((transaction) => {
const { _id, total } = transaction;
const targetDate = _id;
const target = savingGoal.target || 0; // Assuming target is stored in the SavingGoal model
return {
date: targetDate,
target: target,
total: total,
};
});

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

2 changes: 2 additions & 0 deletions routes/statisticDetailRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 16,7 @@ router.get('/wallet/lastmonth/:walletID',isAuthenticated, statisticWalletDetail.

//Saving goal
router.get('/savinggoal/:savingID',isAuthenticated, statisticSavingGoal.getTotalAndTargetByDate);
router.get('/savinggoal/thismonth/:savingID',isAuthenticated, statisticSavingGoal.getThisMonthSaving);
router.get('/savinggoal/lastmonth/:savingID',isAuthenticated, statisticSavingGoal.getLastMonthSaving);

module.exports = router;
You are viewing a condensed version of this merge commit. You can view the full changes here.