From f49d790e826355d892a276f8cf3d2ab45c5052d1 Mon Sep 17 00:00:00 2001 From: roshangarg28 Date: Fri, 5 Apr 2024 16:10:59 +0530 Subject: [PATCH] view of customer done --- .../ShippingAddressController.js | 23 +++++++++- .../ShippingAddresses/ShippingAddressRoute.js | 11 ++++- resources/user/userController.js | 46 ++++++++++++------- resources/user/userRoute.js | 1 + 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/resources/ShippingAddresses/ShippingAddressController.js b/resources/ShippingAddresses/ShippingAddressController.js index ae49f13..e9d35de 100644 --- a/resources/ShippingAddresses/ShippingAddressController.js +++ b/resources/ShippingAddresses/ShippingAddressController.js @@ -53,7 +53,7 @@ export const AddshippingAddress = async (req, res) => { }); } }; - +// For website export const getSingleUserSippingAddress = async (req, res) => { try { const UserShippingAddress = await shippingAddress @@ -74,6 +74,27 @@ export const getSingleUserSippingAddress = async (req, res) => { }); } }; +// For Admin +export const getSingleUserSippingAddressForAdmin = async (req, res) => { + try { + const UserShippingAddress = await shippingAddress + .find({ user: req.params._id }) + + .sort({ createdAt: -1 }); + if (UserShippingAddress) { + res.status(201).json({ + success: true, + UserShippingAddress, + message: "All User Shipping Address Fetched", + }); + } + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went Wrong", + }); + } +}; /// export const deleteSelfShippingAddress = async (req, res) => { diff --git a/resources/ShippingAddresses/ShippingAddressRoute.js b/resources/ShippingAddresses/ShippingAddressRoute.js index 9d2a1db..c23b271 100644 --- a/resources/ShippingAddresses/ShippingAddressRoute.js +++ b/resources/ShippingAddresses/ShippingAddressRoute.js @@ -3,8 +3,9 @@ import { AddshippingAddress, getSingleUserSippingAddress, deleteSelfShippingAddress, + getSingleUserSippingAddressForAdmin, } from "./ShippingAddressController.js"; -import { isAuthenticatedUser } from "../../middlewares/auth.js"; +import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js"; const router = express.Router(); router.route("/new").post(isAuthenticatedUser, AddshippingAddress); @@ -12,6 +13,14 @@ router .route("/user/address/") .get(isAuthenticatedUser, getSingleUserSippingAddress); +router + .route("/user/address/:_id") + .get( + isAuthenticatedUser, + authorizeRoles("admin"), + getSingleUserSippingAddressForAdmin + ); + router .route("/delete/:id") .delete(isAuthenticatedUser, deleteSelfShippingAddress); diff --git a/resources/user/userController.js b/resources/user/userController.js index 60161d4..040eaad 100644 --- a/resources/user/userController.js +++ b/resources/user/userController.js @@ -201,24 +201,36 @@ export const getUserDetails = catchAsyncErrors(async (req, res, next) => { user, }); }); -export const getAllUsers = catchAsyncErrors(async (req, res, next) => { - const users = await User.find().populate("orders"); // Assuming orders are stored in a separate collection and populated in the User model - // Process user data to calculate last purchase date and order count - const usersWithInfo = users.map((user) => { - const lastPurchase = - user.orders.length > 0 - ? user.orders[user.orders.length - 1].createdAt - : null; - const orderCount = user.orders.length; - return { ...user.toJSON(), lastPurchase, orderCount }; - }); +// export const getUserDetailsForAdmin = catchAsyncErrors( +// async (req, res, next) => { +// const user = await User.findById(req.params._id); - res.status(200).json({ - success: true, - users: usersWithInfo, - }); -}); +// res.status(200).json({ +// success: true, +// user, +// }); +// } +// ); + +// export const getAllUsers = catchAsyncErrors(async (req, res, next) => { +// const users = await User.find().populate("orders"); // Assuming orders are stored in a separate collection and populated in the User model + +// // Process user data to calculate last purchase date and order count +// const usersWithInfo = users.map((user) => { +// const lastPurchase = +// user.orders.length > 0 +// ? user.orders[user.orders.length - 1].createdAt +// : null; +// const orderCount = user.orders.length; +// return { ...user.toJSON(), lastPurchase, orderCount }; +// }); + +// res.status(200).json({ +// success: true, +// users: usersWithInfo, +// }); +// }); // 7.Get single user (admin) export const getSingleUser = catchAsyncErrors(async (req, res, next) => { @@ -244,7 +256,7 @@ export const getUserOrderForAdmin = async (req, res) => { try { const order = await Order.find({ user: id, - payment_status: "success", + // payment_status: "success", }).sort({ createdAt: -1 }); if (order) { diff --git a/resources/user/userRoute.js b/resources/user/userRoute.js index 78c4c2b..df3bec0 100644 --- a/resources/user/userRoute.js +++ b/resources/user/userRoute.js @@ -27,6 +27,7 @@ router.route("/user/password/reset/:token").put(resetPassword); router.route("/user/logout").get(logout); router.route("/user/details").get(isAuthenticatedUser, getUserDetails); + router .route("/admin/users") .get(isAuthenticatedUser, authorizeRoles("admin"), getAllUser);