api/resources/Inventory/InventoryController.js
2024-08-14 11:24:51 +05:30

146 lines
4.7 KiB
JavaScript

import { Inventory } from "../Inventory/InventoryModel.js";
import User from "../user/userModel.js";
import { KYC } from "../KYC/KycModel.js";
import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js";
import TerritoryManager from "../TerritoryManagers/TerritoryManagerModel.js";
import SalesCoordinator from "../SalesCoOrdinators/SalesCoOrdinatorModel.js";
// Add inventory data
export const addInventory = async (req, res) => {
try {
const { products, addedFor, addedForId } = req.body;
const userId = req.user._id;
const userType = req.userType;
// console.log("req.user", req.user);
const newInventory = new Inventory({
userId,
userType,
addedFor,
addedForId,
products,
});
await newInventory.save();
res.status(201).json({
success: true,
message: "Inventory added successfully",
data: newInventory,
});
} catch (error) {
res.status(500).json({ success: false, message: "Server error", error });
}
};
// Get all distributors (PD or RD)
export const getDistributors = async (req, res) => {
try {
const { type } = req.params;
if (!["PrincipalDistributor", "RetailDistributor"].includes(type)) {
return res.status(400).json({ message: "Invalid distributor type" });
}
let distributors;
// console.log("type",type);
if (type === "PrincipalDistributor") {
// Fetch all PrincipalDistributors
const principalDistributors = await User.find({ role: "principal-Distributor" });
// console.log("principalDistributors",principalDistributors);
// Map each PrincipalDistributor to include their ShippingAddress
distributors = await Promise.all(principalDistributors.map(async (distributor) => {
const shippingAddress = await ShippingAddress.findOne({ user: distributor._id });
return {
...distributor.toObject(),
shippingAddress,
};
}));
} else {
// For RetailDistributor, fetch approved KYC documents
distributors = await KYC.find({ status: "approved" });
}
res.status(200).json(distributors);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
export const getAllInventories = async (req, res) => {
try {
const inventories = await Inventory.find();
const populatedInventories = await Promise.all(inventories.map(async (inventory) => {
// Populate user details based on userType
let user = null;
if (inventory.userType === 'TerritoryManager') {
user = await TerritoryManager.findById(inventory.userId);
} else if (inventory.userType === 'SalesCoOrdinator') {
user = await SalesCoordinator.findById(inventory.userId);
}
// Populate addedFor details based on addedFor
let addedForData = null;
if (inventory.addedFor === 'PrincipalDistributor') {
addedForData = await User.findById(inventory.addedForId);
const shippingAddress = await ShippingAddress.findOne({ user: addedForData._id });
addedForData = {
...addedForData.toObject(),
shippingAddress,
};
} else if (inventory.addedFor === 'RetailDistributor') {
addedForData = await KYC.findById(inventory.addedForId);
}
return {
...inventory.toObject(),
user,
addedForData,
};
}));
res.status(200).json(populatedInventories);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
export const getSingleInventory = async (req, res) => {
try {
const { id } = req.params;
const inventory = await Inventory.findById(id);
if (!inventory) {
return res.status(404).json({ message: "Inventory not found" });
}
// Populate user details based on userType
let user = null;
if (inventory.userType === 'TerritoryManager') {
user = await TerritoryManager.findById(inventory.userId);
} else if (inventory.userType === 'SalesCoOrdinator') {
user = await SalesCoordinator.findById(inventory.userId);
}
// Populate addedFor details based on addedFor
let addedForData = null;
if (inventory.addedFor === 'PrincipalDistributor') {
addedForData = await User.findById(inventory.addedForId);
const shippingAddress = await ShippingAddress.findOne({ user: addedForData._id });
addedForData = {
...addedForData.toObject(),
shippingAddress,
};
} else if (inventory.addedFor === 'RetailDistributor') {
addedForData = await KYC.findById(inventory.addedForId);
}
res.status(200).json({
...inventory.toObject(),
user,
addedForData,
});
} catch (error) {
res.status(500).json({ message: error.message });
}
};