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) => {
|
export const getAllInventories = async (req, res) => {
|
||||||
try {
|
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 = {};
|
const query = {};
|
||||||
|
|
||||||
if (startDate && endDate) {
|
if (startDate && endDate) {
|
||||||
@ -88,44 +88,33 @@ export const getAllInventories = async (req, res) => {
|
|||||||
const end = new Date(endDate);
|
const end = new Date(endDate);
|
||||||
|
|
||||||
if (start.toDateString() === end.toDateString()) {
|
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 = {
|
query.createdAt = {
|
||||||
$gte: start,
|
$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) {
|
} else if (startDate) {
|
||||||
// Only startDate is provided
|
|
||||||
query.createdAt = {
|
query.createdAt = {
|
||||||
$gte: new Date(startDate),
|
$gte: new Date(startDate),
|
||||||
$lte: new Date(), // Up to today's date
|
$lte: new Date(),
|
||||||
};
|
};
|
||||||
} else if (endDate) {
|
} else if (endDate) {
|
||||||
// Only endDate is provided
|
|
||||||
query.createdAt = {
|
query.createdAt = {
|
||||||
$lte: new Date(endDate),
|
$lte: new Date(endDate),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch total count of documents matching the query
|
// Fetch all matching documents (without pagination) to calculate total data
|
||||||
const total_data = await Inventory.countDocuments(query);
|
const allInventories = await Inventory.find(query).sort({ createdAt: -1 });
|
||||||
|
|
||||||
// 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
|
// Populate additional details
|
||||||
const populatedInventories = await Promise.all(
|
const populatedInventories = await Promise.all(
|
||||||
inventories.map(async (inventory) => {
|
allInventories.map(async (inventory) => {
|
||||||
// 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);
|
||||||
@ -133,8 +122,9 @@ export const getAllInventories = async (req, res) => {
|
|||||||
user = await SalesCoordinator.findById(inventory.userId);
|
user = await SalesCoordinator.findById(inventory.userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate addedFor details based on addedFor
|
|
||||||
let addedForData = null;
|
let addedForData = null;
|
||||||
|
let tradeName = null;
|
||||||
|
|
||||||
if (inventory.addedFor === "PrincipalDistributor") {
|
if (inventory.addedFor === "PrincipalDistributor") {
|
||||||
addedForData = await User.findById(inventory.addedForId);
|
addedForData = await User.findById(inventory.addedForId);
|
||||||
const shippingAddress = await ShippingAddress.findOne({
|
const shippingAddress = await ShippingAddress.findOne({
|
||||||
@ -144,27 +134,52 @@ export const getAllInventories = async (req, res) => {
|
|||||||
...addedForData.toObject(),
|
...addedForData.toObject(),
|
||||||
shippingAddress,
|
shippingAddress,
|
||||||
};
|
};
|
||||||
|
tradeName = addedForData.shippingAddress?.tradeName?.toLowerCase() || "";
|
||||||
} else if (inventory.addedFor === "RetailDistributor") {
|
} else if (inventory.addedFor === "RetailDistributor") {
|
||||||
addedForData = await KYC.findById(inventory.addedForId);
|
addedForData = await KYC.findById(inventory.addedForId);
|
||||||
|
tradeName = addedForData?.trade_name?.toLowerCase() || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...inventory.toObject(),
|
...inventory.toObject(),
|
||||||
user,
|
user,
|
||||||
addedForData,
|
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
|
// Send response with pagination info
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
total_data,
|
total_data,
|
||||||
inventories: populatedInventories,
|
total_pages,
|
||||||
|
current_page: page,
|
||||||
|
inventories: paginatedInventories,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ message: error.message });
|
res.status(500).json({ message: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get single inventory
|
||||||
export const getSingleInventory = async (req, res) => {
|
export const getSingleInventory = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
Loading…
Reference in New Issue
Block a user