update inventory
This commit is contained in:
commit
6e620d22b3
@ -322,3 +322,61 @@ export const getStockPD = async (req, res) => {
|
||||
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",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -19,4 +19,10 @@ router.get(
|
||||
getProductsAndStockByRD
|
||||
);
|
||||
router.get("/stock", isAuthenticatedRD, getProductsAndStockByRD);
|
||||
router.get(
|
||||
"/allpd/stock",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
getAllUsersWithStock
|
||||
);
|
||||
export default router;
|
||||
|
Loading…
Reference in New Issue
Block a user