Skip to content

Commit

Permalink
Complete Update Features for Wallet, Planning, Category
Browse files Browse the repository at this point in the history
  • Loading branch information
vothanhthong committed Jan 11, 2024
1 parent ea49abc commit 9b756e0
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 45 deletions.
29 changes: 29 additions & 0 deletions controllers/categoryControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 104,32 @@ exports.deleteCategory = async (req, res, next) => {
next(new ErrorHandler(err.message, 404));
});
};

// Update Category
exports.updateCategory = async (req, res, next) => {
const categoryId = req.params.id;
const { name, type, color, icon, description, budget } = req.body;

try {
const categoryToUpdate = await Category.findById(categoryId);

if (!categoryToUpdate) {
return next(new ErrorHandler("Category not found", 404));
}

// Update the category properties
categoryToUpdate.name = name || categoryToUpdate.name;
categoryToUpdate.type = type || categoryToUpdate.type;
categoryToUpdate.color = color || categoryToUpdate.color;
categoryToUpdate.icon = icon || categoryToUpdate.icon;
categoryToUpdate.description = description || categoryToUpdate.description;
categoryToUpdate.budget =
budget === undefined ? categoryToUpdate.budget : budget;

await categoryToUpdate.save();

res.status(200).json("Category updated successfully");
} catch (err) {
next(new ErrorHandler(err.message, 404));
}
};
10 changes: 5 additions & 5 deletions controllers/savingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 68,10 @@ exports.viewGoal = async (req, res, next) => {
try {
const savingGoal = await SavingGoal.findById({ _id: goalId });
// Find the transactions related to the goal
const transactions = await NormalTransaction.find({ user: req.userID, saving: goalId });
const transactions = await NormalTransaction.find({
user: req.userID,
saving: goalId,
});
// Calculate the current total amount
res.status(200).json({ savingGoal, transactions });
} catch (err) {
Expand All @@ -84,9 87,7 @@ exports.deleteGoal = async (req, res, next) => {
await Promise.all(
transactions.map(async (transaction) => {
try {
await NormalTransaction.findByIdAndDelete(
transaction._id
);
await NormalTransaction.findByIdAndDelete(transaction._id);
// Handle the updated transaction as needed
} catch (err) {
// Handle errors
Expand Down Expand Up @@ -130,7 131,6 @@ exports.updateGoal = async (req, res, next) => {
dataToUpdate,
{ new: true }
);
// console.log(updatedGoal);
res.status(200).json("Saving goal updated successfully");
} catch (err) {
next(new ErrorHandler(err.message, 500));
Expand Down
100 changes: 66 additions & 34 deletions controllers/walletController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 3,31 @@ const ErrorHandler = require("../utils/ErrorHandler");
const NormalTransaction = require("../models/normalTransaction");

exports.createWallet = async (req, res, next) => {

const data = {
name: req?.body?.name,
amount: req?.body?.amount === undefined ? 0 : req?.body?.amount,
color: req?.body?.color,
icon: req?.body?.icon,
description: req?.body?.icon,
user: req.userID,
}
};

try {
await Wallet.create(data)
.then(() => {
res.status(200).json("Create wallet successfully")
res.status(200).json("Create wallet successfully");
})
.catch((err) => {
next(new ErrorHandler(err.message, 404))
})

next(new ErrorHandler(err.message, 404));
});
} catch (err) {
next(new (ErrorHandler(err.message, 404)))
next(new (ErrorHandler(err.message, 404))());
}
}
};

exports.getWallets = async (req, res, next) => {
try {
const wallets = await (Wallet.find({ user: req.userID }))
const wallets = await Wallet.find({ user: req.userID });
// const walletInfo = await Promise.all(wallets.map(async(wallet) => {
// const transactions = await NormalTransaction.find({user: req.userID, wallet: wallet})
// let amount = req.userID.baseCurrency === "VND" ? wallet.VND : wallet.USD;
Expand All @@ -53,43 51,77 @@ exports.getWallets = async (req, res, next) => {
// }))
res.status(200).json(wallets);
} catch (err) {
next(new ErrorHandler(err.message, 404))
next(new ErrorHandler(err.message, 404));
}
}
};

exports.getWallet = async (req, res, next) => {
const walletId = req.params.id;
try {
const wallet = await Wallet.findById(walletId);
const transactions = await NormalTransaction.find({ user: req.userID, wallet: wallet });
const transactions = await NormalTransaction.find({
user: req.userID,
wallet: wallet,
});
res.status(200).json({ wallet, transactions });
}
catch (err) {
} catch (err) {
next(new ErrorHandler(err.message, 500));
}
}
};

exports.deleteWallet = async (req, res, next) => {
const transactionList = await NormalTransaction.find({ wallet: req.params.id })
await Promise.all(transactionList.map(async (transaction) => {
try {
const updatedTransaction = await NormalTransaction.findByIdAndUpdate(
transaction._id,
{ wallet: null },
{ new: true }
);
// Handle the updated transaction as needed
// console.log(`Updated transaction with ID: ${updatedTransaction._id}`);
} catch (err) {
// Handle errors
next(new ErrorHandler(err.message, 404));
}
}));
const transactionList = await NormalTransaction.find({
wallet: req.params.id,
});
await Promise.all(
transactionList.map(async (transaction) => {
try {
const updatedTransaction = await NormalTransaction.findByIdAndUpdate(
transaction._id,
{ wallet: null },
{ new: true }
);
// Handle the updated transaction as needed
// console.log(`Updated transaction with ID: ${updatedTransaction._id}`);
} catch (err) {
// Handle errors
next(new ErrorHandler(err.message, 404));
}
})
);
await Wallet.findByIdAndDelete(req.params.id)
.then(() => {
res.status(200).json("Delete successfully")
res.status(200).json("Delete successfully");
})
.catch((err) => {
next(new ErrorHandler(err.message, 404))
})
}
next(new ErrorHandler(err.message, 404));
});
};

exports.updateWallet = async (req, res, next) => {
const walletId = req.params.id;

const updateData = {
name: req?.body?.name,
amount: req?.body?.amount === undefined ? 0 : req?.body?.amount,
color: req?.body?.color,
icon: req?.body?.icon,
description: req?.body?.description,
};

try {
const updatedWallet = await Wallet.findByIdAndUpdate(walletId, updateData, {
new: true,
runValidators: true,
});

if (!updatedWallet) {
// If the wallet with the given ID is not found
return next(new ErrorHandler("Wallet not found", 404));
}

res.status(200).json(updatedWallet);
} catch (err) {
next(new ErrorHandler(err.message, 500));
}
};
14 changes: 8 additions & 6 deletions routes/categoryRoute.js
Original file line number Diff line number Diff line change
@@ -1,14 1,16 @@
const express = require('express')
const express = require("express");
const router = express.Router();
const category = require("../controllers/categoryControllers");
const { isAuthenticated } = require("../middlewares/auth");

router.post("/create", isAuthenticated, category.createCategory)
router.post("/create", isAuthenticated, category.createCategory);

router.get("/view", isAuthenticated, category.viewCategories)
router.get("/view", isAuthenticated, category.viewCategories);

router.get("/view/:id", isAuthenticated, category.viewCategory)
router.get("/view/:id", isAuthenticated, category.viewCategory);

router.delete("/delete/:id", isAuthenticated, category.deleteCategory)
router.put("/update/:id", isAuthenticated, category.updateCategory);

module.exports = router;
router.delete("/delete/:id", isAuthenticated, category.deleteCategory);

module.exports = router;
2 changes: 2 additions & 0 deletions routes/walletRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 12,6 @@ router.get("/view/:id", isAuthenticated, wallet.getWallet);

router.delete("/delete/:id", isAuthenticated, wallet.deleteWallet);

router.put("/update/:id", isAuthenticated, wallet.updateWallet);

module.exports = router;

0 comments on commit 9b756e0

Please sign in to comment.