Inventory
This commit is contained in:
parent
5ee70e765c
commit
a190789535
@ -34,7 +34,9 @@ export const uploadProducts = async (req, res) => {
|
||||
const data = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
|
||||
|
||||
if (data.length <= 1) {
|
||||
return res.status(400).json({ message: "Empty spreadsheet or no data found" });
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Empty spreadsheet or no data found" });
|
||||
}
|
||||
|
||||
const headers = data[0];
|
||||
@ -53,7 +55,9 @@ export const uploadProducts = async (req, res) => {
|
||||
const requiredHeaders = Object.keys(headerMapping);
|
||||
|
||||
if (!requiredHeaders.every((header) => headers.includes(header))) {
|
||||
return res.status(400).json({ message: "Missing required columns in spreadsheet" });
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Missing required columns in spreadsheet" });
|
||||
}
|
||||
|
||||
const errors = [];
|
||||
@ -66,7 +70,8 @@ export const uploadProducts = async (req, res) => {
|
||||
|
||||
headers.forEach((header, index) => {
|
||||
if (headerMapping[header]) {
|
||||
item[headerMapping[header]] = row[index] !== undefined ? row[index] : "";
|
||||
item[headerMapping[header]] =
|
||||
row[index] !== undefined ? row[index] : "";
|
||||
}
|
||||
});
|
||||
|
||||
@ -74,7 +79,15 @@ export const uploadProducts = async (req, res) => {
|
||||
const missingFields = new Set();
|
||||
const notFoundErrors = new Set();
|
||||
|
||||
let { SKU, name, category, price, GST, description, special_instructions } = item;
|
||||
let {
|
||||
SKU,
|
||||
name,
|
||||
category,
|
||||
price,
|
||||
GST,
|
||||
description,
|
||||
special_instructions,
|
||||
} = item;
|
||||
|
||||
// Trim leading and trailing spaces from product name and GST
|
||||
name = name ? name.trim() : "";
|
||||
@ -122,7 +135,9 @@ export const uploadProducts = async (req, res) => {
|
||||
// Combine all errors into a single message
|
||||
let errorMessage = "";
|
||||
if (missingFields.size > 0) {
|
||||
errorMessage += `Missing fields: ${Array.from(missingFields).join(", ")}. `;
|
||||
errorMessage += `Missing fields: ${Array.from(missingFields).join(
|
||||
", "
|
||||
)}. `;
|
||||
}
|
||||
if (notFoundErrors.size > 0) {
|
||||
errorMessage += `Not found: ${Array.from(notFoundErrors).join(", ")}.`;
|
||||
@ -143,7 +158,8 @@ export const uploadProducts = async (req, res) => {
|
||||
|
||||
// Ensure fields are set to empty strings if not provided
|
||||
description = description !== undefined ? description : "";
|
||||
special_instructions = special_instructions !== undefined ? special_instructions : "";
|
||||
special_instructions =
|
||||
special_instructions !== undefined ? special_instructions : "";
|
||||
|
||||
// Check for existing product by SKU
|
||||
let existingProduct = await Product.findOne({ SKU }).exec();
|
||||
@ -154,16 +170,27 @@ export const uploadProducts = async (req, res) => {
|
||||
let updatedProduct = { ...existingProduct._doc };
|
||||
|
||||
// Fetch existing category name and GST name
|
||||
const existingCategory = await CategoryModel.findById(existingProduct.category).exec();
|
||||
const existingCategory = await CategoryModel.findById(
|
||||
existingProduct.category
|
||||
).exec();
|
||||
const existingGST = await Tax.findById(existingProduct.GST).exec();
|
||||
|
||||
if (category && existingProduct.category.toString() !== item.category.toString()) {
|
||||
if (
|
||||
category &&
|
||||
existingProduct.category.toString() !== item.category.toString()
|
||||
) {
|
||||
updatedFields.push("category");
|
||||
updatedProduct.category = categoryName;
|
||||
} else {
|
||||
updatedProduct.category = existingCategory ? existingCategory.categoryName : "";
|
||||
updatedProduct.category = existingCategory
|
||||
? existingCategory.categoryName
|
||||
: "";
|
||||
}
|
||||
if (price !== undefined && price !== "" && existingProduct.price !== price) {
|
||||
if (
|
||||
price !== undefined &&
|
||||
price !== "" &&
|
||||
existingProduct.price !== price
|
||||
) {
|
||||
updatedFields.push("price");
|
||||
updatedProduct.price = price;
|
||||
}
|
||||
@ -190,11 +217,17 @@ export const uploadProducts = async (req, res) => {
|
||||
{
|
||||
$set: {
|
||||
category: item.category || existingProduct.category,
|
||||
price: price !== undefined && price !== "" ? price : existingProduct.price,
|
||||
price:
|
||||
price !== undefined && price !== ""
|
||||
? price
|
||||
: existingProduct.price,
|
||||
GST: item.GST || existingProduct.GST,
|
||||
description: description,
|
||||
special_instructions: special_instructions,
|
||||
product_Status: item.product_Status || existingProduct.product_Status || "Active",
|
||||
product_Status:
|
||||
item.product_Status ||
|
||||
existingProduct.product_Status ||
|
||||
"Active",
|
||||
},
|
||||
}
|
||||
);
|
||||
@ -258,7 +291,6 @@ export const uploadProducts = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const createProduct = async (req, res) => {
|
||||
try {
|
||||
let findProduct = "";
|
||||
@ -281,7 +313,9 @@ export const createProduct = async (req, res) => {
|
||||
}).exec();
|
||||
|
||||
if (existingProduct) {
|
||||
return res.status(400).json({ message: "Product with this SKU already exists!" });
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Product with this SKU already exists!" });
|
||||
}
|
||||
|
||||
// Add user ID to the request body
|
||||
@ -295,10 +329,14 @@ export const createProduct = async (req, res) => {
|
||||
}).exec();
|
||||
|
||||
if (existingProduct) {
|
||||
return res.status(400).json({ message: "Product with this SKU already exists!" });
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Product with this SKU already exists!" });
|
||||
}
|
||||
|
||||
product = await Product.findByIdAndUpdate(req.body.product_id, req.body, { new: true });
|
||||
product = await Product.findByIdAndUpdate(req.body.product_id, req.body, {
|
||||
new: true,
|
||||
});
|
||||
}
|
||||
|
||||
res.status(201).json({
|
||||
@ -312,7 +350,6 @@ export const createProduct = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
export const updateProduct = async (req, res) => {
|
||||
try {
|
||||
@ -441,19 +478,19 @@ export const getAllProductAdmin = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//get All Product User(website)
|
||||
export const getAllProductUser = async (req, res) => {
|
||||
try {
|
||||
// Set default values for pagination
|
||||
const PAGE_SIZE = parseInt(req.query?.show || "10");
|
||||
const page = parseInt(req.query?.page - 1 || "0");
|
||||
let obj = {};
|
||||
const page = parseInt(req.query?.page || "1") - 1;
|
||||
let filter = {};
|
||||
|
||||
// Filter by category
|
||||
// Filter by category name
|
||||
if (req.query?.category) {
|
||||
const category = await CategoryModel.findOne({ name: req.query.category });
|
||||
const category = await CategoryModel.findOne({ categoryName: req.query.category });
|
||||
if (category) {
|
||||
obj.category = category._id;
|
||||
filter.category = category._id;
|
||||
} else {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
@ -461,38 +498,43 @@ export const getAllProductUser = async (req, res) => {
|
||||
});
|
||||
}
|
||||
}
|
||||
// Filter by SKU or product name
|
||||
|
||||
// Filter by SKU
|
||||
if (req.query?.SKU) {
|
||||
obj.SKU = req.query.SKU;
|
||||
filter.SKU = req.query.SKU;
|
||||
}
|
||||
|
||||
// Filter by product name using regex for case-insensitive partial matching
|
||||
if (req.query?.name) {
|
||||
obj.name = {
|
||||
filter.name = {
|
||||
$regex: new RegExp(req.query.name, 'i'),
|
||||
};
|
||||
}
|
||||
|
||||
obj.product_Status = "Active";
|
||||
// Only get active products
|
||||
filter.product_Status = "Active";
|
||||
|
||||
// Get total count
|
||||
const total = await Product.countDocuments(obj);
|
||||
// Get total count of filtered products
|
||||
const total = await Product.countDocuments(filter);
|
||||
|
||||
// Get filtered products
|
||||
const product = await Product.find(obj)
|
||||
// Retrieve products with pagination, filtering, and sorting
|
||||
const products = await Product.find(filter)
|
||||
.populate({
|
||||
path: "category addedBy GST",
|
||||
select: "name categoryName tax",
|
||||
select: "categoryName name tax",
|
||||
})
|
||||
.limit(PAGE_SIZE)
|
||||
.skip(PAGE_SIZE * page)
|
||||
.sort({ createdAt: -1 })
|
||||
.exec();
|
||||
|
||||
if (product) {
|
||||
// Check if any products were found
|
||||
if (products && products.length > 0) {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
total_data: total,
|
||||
total_pages: Math.ceil(total / PAGE_SIZE),
|
||||
product,
|
||||
products,
|
||||
});
|
||||
} else {
|
||||
return res.status(404).json({
|
||||
@ -501,12 +543,13 @@ export const getAllProductUser = async (req, res) => {
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
msg: error.message || "Something went wrong!",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//Change Product status
|
||||
export const ChangeProductStatus = async (req, res) => {
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user