view of customer done
This commit is contained in:
parent
e7d068798d
commit
f49d790e82
@ -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) => {
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user