opening inventory for pd
This commit is contained in:
parent
6b4cd0e76d
commit
2745d0b330
@ -17,7 +17,7 @@ const ProductRecordSchema = new mongoose.Schema({
|
|||||||
const StockSchema = new mongoose.Schema({
|
const StockSchema = new mongoose.Schema({
|
||||||
userId: {
|
userId: {
|
||||||
type: mongoose.Schema.Types.ObjectId,
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
refPath: 'User',
|
ref: 'User',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
products: [ProductRecordSchema],
|
products: [ProductRecordSchema],
|
||||||
|
@ -294,3 +294,61 @@ export const getStockPD = async (req, res) => {
|
|||||||
res.status(500).json({ message: "Server error", error });
|
res.status(500).json({ message: "Server error", error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
export const getAllUsersWithStock = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Destructure query parameters for pagination and filtering
|
||||||
|
const { page = 1, show = 10, name = "", SBU = "" } = req.query;
|
||||||
|
|
||||||
|
// Convert page and show to numbers
|
||||||
|
const currentPage = parseInt(page, 10);
|
||||||
|
const itemsPerPage = parseInt(show, 10);
|
||||||
|
|
||||||
|
// Create filters for user based on name and SBU
|
||||||
|
const userFilters = {};
|
||||||
|
if (name) {
|
||||||
|
userFilters.name = { $regex: name, $options: "i" }; // Case-insensitive regex for name
|
||||||
|
}
|
||||||
|
if (SBU) {
|
||||||
|
userFilters.SBU = { $regex: SBU, $options: "i" }; // Case-insensitive regex for SBU
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find stock records and populate user details
|
||||||
|
const stockRecords = await PDStock.find()
|
||||||
|
.populate({
|
||||||
|
path: "userId",
|
||||||
|
model: "User", // Ensure the correct model is used
|
||||||
|
select: "uniqueId SBU name email phone", // Select specific fields
|
||||||
|
match: userFilters, // Apply user filters for name and SBU
|
||||||
|
})
|
||||||
|
.skip((currentPage - 1) * itemsPerPage) // Pagination
|
||||||
|
.select("-products")
|
||||||
|
.limit(itemsPerPage);
|
||||||
|
// Filter out stock records where userId is null (no match found)
|
||||||
|
const filteredStockRecords = stockRecords.filter(
|
||||||
|
(record) => record.userId !== null
|
||||||
|
);
|
||||||
|
|
||||||
|
// Count total records after filtering
|
||||||
|
const totalRecords = await PDStock.countDocuments({
|
||||||
|
userId: { $exists: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return the filtered stock records with pagination info
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
data: filteredStockRecords,
|
||||||
|
pagination: {
|
||||||
|
currentPage,
|
||||||
|
itemsPerPage,
|
||||||
|
totalRecords,
|
||||||
|
totalPages: Math.ceil(totalRecords / itemsPerPage),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Error fetching users and stock records",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import {
|
import {
|
||||||
createOrUpdateStock,
|
createOrUpdateStock,
|
||||||
|
getAllUsersWithStock,
|
||||||
getProductsAndStockByPD,
|
getProductsAndStockByPD,
|
||||||
getProductsAndStockByRD,
|
getProductsAndStockByRD,
|
||||||
getStockPD,
|
getStockPD,
|
||||||
@ -19,4 +20,10 @@ router.get(
|
|||||||
getProductsAndStockByRD
|
getProductsAndStockByRD
|
||||||
);
|
);
|
||||||
router.get("/stock", isAuthenticatedRD, getProductsAndStockByRD);
|
router.get("/stock", isAuthenticatedRD, getProductsAndStockByRD);
|
||||||
|
router.get(
|
||||||
|
"/allpd/stock",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
getAllUsersWithStock
|
||||||
|
);
|
||||||
export default router;
|
export default router;
|
||||||
|
Loading…
Reference in New Issue
Block a user