fixing
This commit is contained in:
parent
5fe1da6c98
commit
632d05f656
@ -30,7 +30,6 @@ const InventorySchema = new mongoose.Schema(
|
|||||||
{
|
{
|
||||||
uniqueId: {
|
uniqueId: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
|
||||||
unique: true,
|
unique: true,
|
||||||
},
|
},
|
||||||
userId: {
|
userId: {
|
||||||
|
@ -104,7 +104,7 @@ export const getProductsAndStockByPD = async (req, res) => {
|
|||||||
|
|
||||||
export const getProductsAndStockByRD = async (req, res) => {
|
export const getProductsAndStockByRD = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { userId } = req.params;
|
const userId = req.params.userId || req.user._id;
|
||||||
|
|
||||||
// Pagination parameters
|
// Pagination parameters
|
||||||
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
||||||
@ -200,93 +200,3 @@ export const getProductsAndStockByRD = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getProductsAndStockForRD = async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { userId } = req.user._id;
|
|
||||||
|
|
||||||
// Filtering criteria
|
|
||||||
const filter = {};
|
|
||||||
if (req.query.name) {
|
|
||||||
filter.name = {
|
|
||||||
$regex: new RegExp(req.query.name, "i"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (req.query.category) {
|
|
||||||
filter.category = mongoose.Types.ObjectId(req.query.category);
|
|
||||||
}
|
|
||||||
if (req.query.brand) {
|
|
||||||
filter.brand = mongoose.Types.ObjectId(req.query.brand);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch user's RDStock data and products concurrently
|
|
||||||
const [userStock, products] = await Promise.all([
|
|
||||||
RDStock.findOne({ userId: mongoose.Types.ObjectId(userId) }),
|
|
||||||
Product.aggregate([
|
|
||||||
{ $match: filter },
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: "categorymodels",
|
|
||||||
localField: "category",
|
|
||||||
foreignField: "_id",
|
|
||||||
as: "categoryDetails",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: "brandmodels",
|
|
||||||
localField: "brand",
|
|
||||||
foreignField: "_id",
|
|
||||||
as: "brandDetails",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
category: { $arrayElemAt: ["$categoryDetails.categoryName", 0] },
|
|
||||||
brand: { $arrayElemAt: ["$brandDetails.brandName", 0] },
|
|
||||||
GST: 1,
|
|
||||||
HSN_Code: 1,
|
|
||||||
SKU: 1,
|
|
||||||
addedBy: 1,
|
|
||||||
createdAt: 1,
|
|
||||||
description: 1,
|
|
||||||
image: 1,
|
|
||||||
name: 1,
|
|
||||||
price: 1,
|
|
||||||
product_Status: 1,
|
|
||||||
updatedAt: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]),
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Create a stock map for easy lookup
|
|
||||||
const stockMap = {};
|
|
||||||
if (userStock && userStock.products) {
|
|
||||||
userStock.products.forEach((product) => {
|
|
||||||
stockMap[product.productid.toString()] = product.Stock;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Combine products with their respective stock
|
|
||||||
const productsWithStock = products.map((product) => ({
|
|
||||||
...product,
|
|
||||||
stock: stockMap[product._id.toString()] || 0,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Get total count for pagination purposes
|
|
||||||
const total = await Product.countDocuments(filter);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
totalProducts: total,
|
|
||||||
products: productsWithStock,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching products with stock:", error);
|
|
||||||
return res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
message: "Error fetching products and stock",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
@ -2,7 +2,6 @@ import express from "express";
|
|||||||
import {
|
import {
|
||||||
getProductsAndStockByPD,
|
getProductsAndStockByPD,
|
||||||
getProductsAndStockByRD,
|
getProductsAndStockByRD,
|
||||||
getProductsAndStockForRD,
|
|
||||||
} from "./StockController.js";
|
} from "./StockController.js";
|
||||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||||
import {isAuthenticatedRD} from "../../middlewares/rdAuth.js";
|
import {isAuthenticatedRD} from "../../middlewares/rdAuth.js";
|
||||||
@ -20,5 +19,5 @@ router.get(
|
|||||||
authorizeRoles("admin"),
|
authorizeRoles("admin"),
|
||||||
getProductsAndStockByRD
|
getProductsAndStockByRD
|
||||||
);
|
);
|
||||||
router.get("/stock", isAuthenticatedRD, getProductsAndStockForRD);
|
router.get("/stock", isAuthenticatedRD, getProductsAndStockByRD);
|
||||||
export default router;
|
export default router;
|
||||||
|
Loading…
Reference in New Issue
Block a user