Inventory
This commit is contained in:
parent
7d83288deb
commit
96c46a8726
@ -78,9 +78,9 @@ export const getDistributors = async (req, res) => {
|
||||
|
||||
export const getAllInventories = async (req, res) => {
|
||||
try {
|
||||
const { page = 1, show = 10, startDate, endDate } = req.query;
|
||||
const { page = 1, show = 10, startDate, endDate, name } = req.query;
|
||||
|
||||
// Build query
|
||||
// Build query for date filtering
|
||||
const query = {};
|
||||
|
||||
if (startDate && endDate) {
|
||||
@ -88,44 +88,33 @@ export const getAllInventories = async (req, res) => {
|
||||
const end = new Date(endDate);
|
||||
|
||||
if (start.toDateString() === end.toDateString()) {
|
||||
// If startDate and endDate are the same, fetch records for that day
|
||||
query.createdAt = {
|
||||
$gte: new Date(startDate),
|
||||
$lt: new Date(startDate).setDate(new Date(startDate).getDate() + 1), // Until the end of that day
|
||||
};
|
||||
} else {
|
||||
// If startDate and endDate are different, fetch records between these dates
|
||||
query.createdAt = {
|
||||
$gte: start,
|
||||
$lte: new Date(end).setDate(new Date(end).getDate() + 1),
|
||||
$lt: new Date(start).setDate(start.getDate() + 1),
|
||||
};
|
||||
} else {
|
||||
query.createdAt = {
|
||||
$gte: start,
|
||||
$lte: new Date(end).setDate(end.getDate() + 1),
|
||||
};
|
||||
}
|
||||
} else if (startDate) {
|
||||
// Only startDate is provided
|
||||
query.createdAt = {
|
||||
$gte: new Date(startDate),
|
||||
$lte: new Date(), // Up to today's date
|
||||
$lte: new Date(),
|
||||
};
|
||||
} else if (endDate) {
|
||||
// Only endDate is provided
|
||||
query.createdAt = {
|
||||
$lte: new Date(endDate),
|
||||
};
|
||||
}
|
||||
|
||||
// Fetch total count of documents matching the query
|
||||
const total_data = await Inventory.countDocuments(query);
|
||||
|
||||
// Fetch inventory data with pagination
|
||||
const inventories = await Inventory.find(query)
|
||||
.skip((page - 1) * show)
|
||||
.limit(Number(show))
|
||||
.sort({ createdAt: -1 }); // Optional: Sort by createdAt date in descending order
|
||||
// Fetch all matching documents (without pagination) to calculate total data
|
||||
const allInventories = await Inventory.find(query).sort({ createdAt: -1 });
|
||||
|
||||
// Populate additional details
|
||||
const populatedInventories = await Promise.all(
|
||||
inventories.map(async (inventory) => {
|
||||
// Populate user details based on userType
|
||||
allInventories.map(async (inventory) => {
|
||||
let user = null;
|
||||
if (inventory.userType === "TerritoryManager") {
|
||||
user = await TerritoryManager.findById(inventory.userId);
|
||||
@ -133,8 +122,9 @@ export const getAllInventories = async (req, res) => {
|
||||
user = await SalesCoordinator.findById(inventory.userId);
|
||||
}
|
||||
|
||||
// Populate addedFor details based on addedFor
|
||||
let addedForData = null;
|
||||
let tradeName = null;
|
||||
|
||||
if (inventory.addedFor === "PrincipalDistributor") {
|
||||
addedForData = await User.findById(inventory.addedForId);
|
||||
const shippingAddress = await ShippingAddress.findOne({
|
||||
@ -144,27 +134,52 @@ export const getAllInventories = async (req, res) => {
|
||||
...addedForData.toObject(),
|
||||
shippingAddress,
|
||||
};
|
||||
tradeName = addedForData.shippingAddress?.tradeName?.toLowerCase() || "";
|
||||
} else if (inventory.addedFor === "RetailDistributor") {
|
||||
addedForData = await KYC.findById(inventory.addedForId);
|
||||
tradeName = addedForData?.trade_name?.toLowerCase() || "";
|
||||
}
|
||||
|
||||
return {
|
||||
...inventory.toObject(),
|
||||
user,
|
||||
addedForData,
|
||||
tradeName,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
// Apply name filter
|
||||
let filteredInventories = populatedInventories;
|
||||
if (name) {
|
||||
filteredInventories = filteredInventories.filter(
|
||||
(inventory) => inventory.tradeName && inventory.tradeName.includes(name.toLowerCase())
|
||||
);
|
||||
}
|
||||
|
||||
// Calculate total count of filtered data
|
||||
const total_data = filteredInventories.length;
|
||||
|
||||
// Apply pagination after filtering
|
||||
const paginatedInventories = filteredInventories
|
||||
.slice((page - 1) * show, page * show);
|
||||
|
||||
// Calculate total pages
|
||||
const total_pages = Math.ceil(total_data / show);
|
||||
|
||||
// Send response with pagination info
|
||||
res.status(200).json({
|
||||
total_data,
|
||||
inventories: populatedInventories,
|
||||
total_pages,
|
||||
current_page: page,
|
||||
inventories: paginatedInventories,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Get single inventory
|
||||
export const getSingleInventory = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
Loading…
Reference in New Issue
Block a user