product change
This commit is contained in:
parent
deea373f09
commit
2f1240de0d
@ -62,9 +62,6 @@ export const updateProduct = async (req, res) => {
|
||||
}
|
||||
|
||||
if (req.files) {
|
||||
// req.body.addedBy = req.user.id;
|
||||
// const image_file = req.files.image;
|
||||
// console.log("req.files", req.files);
|
||||
const getProduct = await Product.findById(req.params.id);
|
||||
|
||||
if (getProduct.image?.length > 0) {
|
||||
@ -116,19 +113,36 @@ export const updateProduct = async (req, res) => {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//get All Product
|
||||
export const getAllProduct = async (req, res) => {
|
||||
export const getAllProductAdmin = async (req, res) => {
|
||||
try {
|
||||
const product = await Product.find()
|
||||
const PAGE_SIZE = parseInt(req.query?.show || "10");
|
||||
const page = parseInt(req.query?.page - 1 || "0");
|
||||
let obj = {};
|
||||
if (req.query?.name)
|
||||
obj.name = {
|
||||
$regex: new RegExp(req.query.name),
|
||||
$options: "i",
|
||||
};
|
||||
if (req.query?.category) obj.category = req.query.category;
|
||||
const total = await Product.countDocuments(obj);
|
||||
const product = await Product.find(obj)
|
||||
.populate({
|
||||
path: "category addedBy variants.gst_Id",
|
||||
select: "name categoryName tax",
|
||||
})
|
||||
.limit(PAGE_SIZE)
|
||||
.skip(PAGE_SIZE * page)
|
||||
// .sort("name")
|
||||
.sort({
|
||||
createdAt: -1,
|
||||
});
|
||||
})
|
||||
.exec();
|
||||
|
||||
if (product) {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
total_data: total,
|
||||
total_pages: Math.ceil(total / PAGE_SIZE),
|
||||
product,
|
||||
});
|
||||
}
|
||||
@ -139,6 +153,83 @@ export const getAllProduct = async (req, res) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//get All Product User(website)
|
||||
export const getAllProductUser = async (req, res) => {
|
||||
try {
|
||||
const PAGE_SIZE = parseInt(req.query?.show || "10");
|
||||
const page = parseInt(req.query?.page - 1 || "0");
|
||||
let obj = {};
|
||||
if (req.query?.name)
|
||||
obj.name = {
|
||||
$regex: new RegExp(req.query.name),
|
||||
$options: "i",
|
||||
};
|
||||
if (req.query?.category) obj.category = req.query.category;
|
||||
obj.product_Status = "Active";
|
||||
const total = await Product.countDocuments(obj);
|
||||
const product = await Product.find(obj)
|
||||
.populate({
|
||||
path: "category addedBy variants.gst_Id",
|
||||
select: "name categoryName tax",
|
||||
})
|
||||
.limit(PAGE_SIZE)
|
||||
.skip(PAGE_SIZE * page)
|
||||
// .sort("name")
|
||||
.sort({
|
||||
createdAt: -1,
|
||||
})
|
||||
.exec();
|
||||
|
||||
if (product) {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
total_data: total,
|
||||
total_pages: Math.ceil(total / PAGE_SIZE),
|
||||
product,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: error.message ? error.message : "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
//Change Product status
|
||||
export const ChangeProductStatus = async (req, res) => {
|
||||
try {
|
||||
const data = await Product.findById(req.params.id);
|
||||
if (data) {
|
||||
if (data?.product_Status === "Active") {
|
||||
let product = await Product.findByIdAndUpdate(
|
||||
req.params.id,
|
||||
{ product_Status: "inActive" },
|
||||
{ new: true } // Return the updated document
|
||||
);
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
msg: "Changed status inActive",
|
||||
});
|
||||
} else {
|
||||
let product = await Product.findByIdAndUpdate(
|
||||
req.params.id,
|
||||
{ product_Status: "Active" },
|
||||
{ new: true } // Return the updated document
|
||||
);
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
msg: "Changed status Active",
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: error.message ? error.message : "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
//get One Product
|
||||
export const getOneProduct = async (req, res) => {
|
||||
try {
|
||||
@ -165,11 +256,9 @@ export const getOneProduct = async (req, res) => {
|
||||
export const getAllProductsDevicesFirst = async (req, res) => {
|
||||
try {
|
||||
// we want products with category name Device to be displayed first, so i have first found the products with category name Devices then made another request to find all products and filtered products with category devices , then merged both arrays so we get devices first then all other categories
|
||||
|
||||
const categoryName = "Devices";
|
||||
// Find the category object by name first
|
||||
const category = await CategoryModel.findOne({ categoryName });
|
||||
|
||||
if (!category) {
|
||||
throw new Error("Category not found");
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ const productSchema = new Schema(
|
||||
],
|
||||
product_Status: {
|
||||
type: String,
|
||||
enum: ["Active", "Inactive"],
|
||||
enum: ["Active", "inActive"],
|
||||
default: "Active",
|
||||
},
|
||||
addedBy: {
|
||||
|
@ -1,20 +1,29 @@
|
||||
import express from "express";
|
||||
import {
|
||||
createProduct,
|
||||
getAllProduct,
|
||||
getAllProductAdmin,
|
||||
updateProduct,
|
||||
deleteProduct,
|
||||
getOneProduct,
|
||||
deleteImageFromCloudinary,
|
||||
getProductsByCategory,
|
||||
getAllProductUser,
|
||||
getAllProductsDevicesFirst,
|
||||
ChangeProductStatus,
|
||||
} from "./ProductController.js";
|
||||
const router = express.Router();
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||
router
|
||||
.route("/product/create/")
|
||||
.post(isAuthenticatedUser, authorizeRoles("admin"), createProduct);
|
||||
router.route("/product/getAll/").get(getAllProduct);
|
||||
router
|
||||
.route("/product/getAll/admin/")
|
||||
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllProductAdmin);
|
||||
|
||||
//change Product status
|
||||
router.route("/product/admin/status/:id").patch(ChangeProductStatus);
|
||||
//get all product user
|
||||
router.route("/product/getAll/user/").get(getAllProductUser);
|
||||
router
|
||||
.route("/product/getAllProductsDevicesFrist/")
|
||||
.get(getAllProductsDevicesFirst);
|
||||
|
Loading…
Reference in New Issue
Block a user