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