api for getting the order of each user and latest purchase

This commit is contained in:
roshangarg28 2024-04-02 14:31:55 +05:30
parent 939b3ed996
commit e1cbd1a30b
3 changed files with 67 additions and 21 deletions

View File

@ -89,6 +89,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;