This commit is contained in:
Sibunnayak 2024-04-02 14:59:28 +05:30
commit bf9278145c
4 changed files with 70 additions and 27 deletions

9
package-lock.json generated
View File

@ -5417,8 +5417,7 @@
"cloudinary-core": {
"version": "2.12.3",
"resolved": "https://registry.npmjs.org/cloudinary-core/-/cloudinary-core-2.12.3.tgz",
"integrity": "sha512-Ll4eDzcrIVn4zCttMh3Mdi+KNz07p5EEjBT2PQSRx8Eok1lKPt3uBBenOk/w88RKK3B8SFIWcEe/mN4BHQ0p8A==",
"requires": {}
"integrity": "sha512-Ll4eDzcrIVn4zCttMh3Mdi+KNz07p5EEjBT2PQSRx8Eok1lKPt3uBBenOk/w88RKK3B8SFIWcEe/mN4BHQ0p8A=="
},
"color-convert": {
"version": "2.0.1",
@ -6661,8 +6660,7 @@
"multer-storage-cloudinary": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/multer-storage-cloudinary/-/multer-storage-cloudinary-4.0.0.tgz",
"integrity": "sha512-25lm9R6o5dWrHLqLvygNX+kBOxprzpmZdnVKH4+r68WcfCt8XV6xfQaMuAg+kUE5Xmr8mJNA4gE0AcBj9FJyWA==",
"requires": {}
"integrity": "sha512-25lm9R6o5dWrHLqLvygNX+kBOxprzpmZdnVKH4+r68WcfCt8XV6xfQaMuAg+kUE5Xmr8mJNA4gE0AcBj9FJyWA=="
},
"mute-stream": {
"version": "0.0.8",
@ -7895,8 +7893,7 @@
"ws": {
"version": "7.4.6",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.6.tgz",
"integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==",
"requires": {}
"integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A=="
},
"xregexp": {
"version": "2.0.0",

View File

@ -115,6 +115,7 @@ export const getUserSelf = async (req, res) => {
});
}
};
export const deleteOneOrder = async (req, res) => {
try {
if (!req?.user) return res.status(400).json({ message: "please login !" });

View File

@ -6,6 +6,7 @@ import sendEmail from "../../Utils/sendEmail.js";
import crypto from "crypto";
import cloudinary from "cloudinary";
import password from "secure-random-password";
import { Order } from "../Orders/orderModel.js";
// 1.Register a User
export const registerUser = async (req, res) => {
try {
@ -200,6 +201,24 @@ 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 };
});
res.status(200).json({
success: true,
users: usersWithInfo,
});
});
// 7.Get single user (admin)
export const getSingleUser = catchAsyncErrors(async (req, res, next) => {
@ -219,6 +238,29 @@ export const getSingleUser = catchAsyncErrors(async (req, res, next) => {
user,
});
});
export const getUserOrderForAdmin = async (req, res) => {
const id = req.params.id;
// console.log(id);
try {
const order = await Order.find({
user: id,
payment_status: "success",
}).sort({ createdAt: -1 });
if (order) {
return res.status(200).json({
success: true,
order,
message: "self Order fetched",
});
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went Wrong",
});
}
};
// 8.update User password
export const updatePassword = catchAsyncErrors(async (req, res, next) => {
const user = await User.findById(req.user.id).select("+password");
@ -284,7 +326,8 @@ export const updateProfile = catchAsyncErrors(async (req, res, next) => {
// 9.Get all users(admin)
export const getAllUser = catchAsyncErrors(async (req, res, next) => {
const users = await User.find(); //.select('-role');
// Assuming your User model is imported as 'User'
const users = await User.find({ role: "user" });
res.status(200).json({
success: true,

View File

@ -1,17 +1,18 @@
import express from "express"
import express from "express";
import {
registerUser,
loginUser,
logout,
forgotPassword,
resetPassword,
getUserDetails,
updatePassword,
updateProfile,
getSingleUser,
getAllUser
} from "./userController.js"
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"
registerUser,
loginUser,
logout,
forgotPassword,
resetPassword,
getUserDetails,
updatePassword,
updateProfile,
getSingleUser,
getAllUser,
getUserOrderForAdmin,
} from "./userController.js";
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
const router = express.Router();
@ -27,17 +28,18 @@ router.route("/user/logout").get(logout);
router.route("/user/details").get(isAuthenticatedUser, getUserDetails);
router
.route("/admin/users")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllUser);
.route("/admin/users")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllUser);
router
.route("/admin/user/:id")
.get(isAuthenticatedUser, authorizeRoles("admin"), getSingleUser);
.route("/admin/users/orders/:id")
.get(isAuthenticatedUser, authorizeRoles("admin"), getUserOrderForAdmin);
router
.route("/admin/user/:id")
.get(isAuthenticatedUser, authorizeRoles("admin"), getSingleUser);
router.route("/user/password/update").put(isAuthenticatedUser, updatePassword);
router.route("/user/update/profile").put(isAuthenticatedUser, updateProfile);
export default router;
export default router;