Inventory
This commit is contained in:
parent
01e12dedaf
commit
5ee70e765c
@ -66,14 +66,62 @@ export const getDistributors = async (req, res) => {
|
|||||||
|
|
||||||
export const getAllInventories = async (req, res) => {
|
export const getAllInventories = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const inventories = await Inventory.find();
|
const {
|
||||||
|
page = 1,
|
||||||
|
show = 10,
|
||||||
|
startDate,
|
||||||
|
endDate
|
||||||
|
} = req.query;
|
||||||
|
|
||||||
|
// Build query
|
||||||
|
const query = {};
|
||||||
|
|
||||||
|
if (startDate && endDate) {
|
||||||
|
const start = new Date(startDate);
|
||||||
|
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)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} else if (startDate) {
|
||||||
|
// Only startDate is provided
|
||||||
|
query.createdAt = {
|
||||||
|
$gte: new Date(startDate),
|
||||||
|
$lte: new Date() // Up to today's 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
|
||||||
|
|
||||||
|
// Populate additional details
|
||||||
const populatedInventories = await Promise.all(inventories.map(async (inventory) => {
|
const populatedInventories = await Promise.all(inventories.map(async (inventory) => {
|
||||||
// Populate user details based on userType
|
// Populate user details based on userType
|
||||||
let user = null;
|
let user = null;
|
||||||
if (inventory.userType === 'TerritoryManager') {
|
if (inventory.userType === 'TerritoryManager') {
|
||||||
user = await TerritoryManager.findById(inventory.userId);
|
user = await TerritoryManager.findById(inventory.userId);
|
||||||
} else if (inventory.userType === 'SalesCoOrdinator') {
|
} else if (inventory.userType === 'SalesCoordinator') {
|
||||||
user = await SalesCoordinator.findById(inventory.userId);
|
user = await SalesCoordinator.findById(inventory.userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +137,6 @@ export const getAllInventories = async (req, res) => {
|
|||||||
} else if (inventory.addedFor === 'RetailDistributor') {
|
} else if (inventory.addedFor === 'RetailDistributor') {
|
||||||
addedForData = await KYC.findById(inventory.addedForId);
|
addedForData = await KYC.findById(inventory.addedForId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...inventory.toObject(),
|
...inventory.toObject(),
|
||||||
user,
|
user,
|
||||||
@ -97,7 +144,11 @@ export const getAllInventories = async (req, res) => {
|
|||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
res.status(200).json(populatedInventories);
|
// Send response with pagination info
|
||||||
|
res.status(200).json({
|
||||||
|
total_data,
|
||||||
|
inventories: populatedInventories
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ message: error.message });
|
res.status(500).json({ message: error.message });
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user