fixing bug of add multiple product,pd,rd and address logic fixed
This commit is contained in:
parent
e712a5a6d1
commit
9a03c93164
Binary file not shown.
Binary file not shown.
@ -73,6 +73,10 @@ export const uploadProducts = async (req, res) => {
|
|||||||
|
|
||||||
for (let i = 1; i < data.length; i++) {
|
for (let i = 1; i < data.length; i++) {
|
||||||
const row = data[i];
|
const row = data[i];
|
||||||
|
// Skip the row if it's completely empty
|
||||||
|
if (row.every((cell) => cell === undefined || cell === "")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const item = {};
|
const item = {};
|
||||||
|
|
||||||
headers.forEach((header, index) => {
|
headers.forEach((header, index) => {
|
||||||
@ -83,7 +87,9 @@ export const uploadProducts = async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check if the row has meaningful data, skip if it's mostly empty
|
// Check if the row has meaningful data, skip if it's mostly empty
|
||||||
const hasValidData = Object.values(item).some((value) => value && value.trim());
|
const hasValidData = Object.values(item).some(
|
||||||
|
(value) => value && value.trim()
|
||||||
|
);
|
||||||
if (!hasValidData) {
|
if (!hasValidData) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,10 @@ export const uploadRetaildistributors = async (req, res) => {
|
|||||||
|
|
||||||
for (let i = 1; i < data.length; i++) {
|
for (let i = 1; i < data.length; i++) {
|
||||||
const row = data[i];
|
const row = data[i];
|
||||||
|
// Skip the row if it's completely empty
|
||||||
|
if (row.every((cell) => cell === undefined || cell === "")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const item = {};
|
const item = {};
|
||||||
|
|
||||||
headers.forEach((header, index) => {
|
headers.forEach((header, index) => {
|
||||||
|
@ -59,16 +59,17 @@ export const AddshippingAddressByAdmin = async (req, res) => {
|
|||||||
panNumber,
|
panNumber,
|
||||||
tradeName,
|
tradeName,
|
||||||
gstNumber,
|
gstNumber,
|
||||||
|
isDefault,
|
||||||
} = req.body;
|
} = req.body;
|
||||||
// console.log(req.body);
|
|
||||||
// Validate required fields
|
// Validate required fields
|
||||||
if (!street || !city || !state || !postalCode || !panNumber) {
|
if (!Name || !phoneNumber || !street || !city || !state || !postalCode || !panNumber) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
.json({ msg: "Please provide all required fields" });
|
.json({ msg: "Please provide all required fields: Name, phone number, street, city, state, postal code, and PAN number" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create shipping address object
|
// Create new shipping address
|
||||||
const newAddress = await ShippingAddress.create({
|
const newAddress = await ShippingAddress.create({
|
||||||
Name,
|
Name,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
@ -81,16 +82,24 @@ export const AddshippingAddressByAdmin = async (req, res) => {
|
|||||||
tradeName,
|
tradeName,
|
||||||
gstNumber,
|
gstNumber,
|
||||||
user: req.params._id, // Assuming req.params._id contains the correct user ID
|
user: req.params._id, // Assuming req.params._id contains the correct user ID
|
||||||
|
isDefault,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If this address is marked as default, set all other addresses for this user to isDefault: false
|
||||||
|
if (isDefault) {
|
||||||
|
await ShippingAddress.updateMany(
|
||||||
|
{ user: req.params._id, _id: { $ne: newAddress._id } }, // Exclude the new address
|
||||||
|
{ $set: { isDefault: false } }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Respond with success
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
address: newAddress,
|
address: newAddress,
|
||||||
message: "Shipping address added successfully",
|
message: "Shipping address added successfully",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.error("Error creating shipping address:", error.message);
|
|
||||||
|
|
||||||
// Check for validation errors
|
// Check for validation errors
|
||||||
if (error.name === "ValidationError") {
|
if (error.name === "ValidationError") {
|
||||||
const errorMessages = Object.values(error.errors).map(
|
const errorMessages = Object.values(error.errors).map(
|
||||||
@ -102,7 +111,7 @@ export const AddshippingAddressByAdmin = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// General error
|
// General error response
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: error.message || "Something went wrong",
|
message: error.message || "Something went wrong",
|
||||||
@ -203,6 +212,7 @@ export const updateShippingAddress = async (req, res) => {
|
|||||||
panNumber,
|
panNumber,
|
||||||
tradeName,
|
tradeName,
|
||||||
gstNumber,
|
gstNumber,
|
||||||
|
isDefault,
|
||||||
} = req.body;
|
} = req.body;
|
||||||
// console.log(req.body);
|
// console.log(req.body);
|
||||||
const _id = req.params.id;
|
const _id = req.params.id;
|
||||||
@ -210,7 +220,7 @@ export const updateShippingAddress = async (req, res) => {
|
|||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
.json({ message: "please Provide shipping Address Id" });
|
.json({ message: "please Provide shipping Address Id" });
|
||||||
const getselfAddress = await ShippingAddress.findById(req.params.id);
|
const getselfAddress = await ShippingAddress.findById(_id);
|
||||||
if (!getselfAddress) {
|
if (!getselfAddress) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
@ -250,6 +260,14 @@ export const updateShippingAddress = async (req, res) => {
|
|||||||
return res.status(404).json({ msg: "please provide gstNumber" });
|
return res.status(404).json({ msg: "please provide gstNumber" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// If the updated address is marked as default, update all other addresses to isDefault: false
|
||||||
|
if (isDefault) {
|
||||||
|
await ShippingAddress.updateMany(
|
||||||
|
{ user: getselfAddress.user, _id: { $ne: _id } }, // exclude current address
|
||||||
|
{ $set: { isDefault: false } }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const updateAddressData = {
|
const updateAddressData = {
|
||||||
Name,
|
Name,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
@ -261,6 +279,7 @@ export const updateShippingAddress = async (req, res) => {
|
|||||||
panNumber,
|
panNumber,
|
||||||
tradeName,
|
tradeName,
|
||||||
gstNumber,
|
gstNumber,
|
||||||
|
isDefault,
|
||||||
};
|
};
|
||||||
const updateShippingAddress = await ShippingAddress.findByIdAndUpdate(
|
const updateShippingAddress = await ShippingAddress.findByIdAndUpdate(
|
||||||
{ _id: _id },
|
{ _id: _id },
|
||||||
|
@ -5,7 +5,7 @@ import { RDStock } from "./RdStockModel.js";
|
|||||||
|
|
||||||
export const getProductsAndStockByPD = async (req, res) => {
|
export const getProductsAndStockByPD = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { userId } = req.params;
|
const userId = req.params.userId || req.user._id;
|
||||||
|
|
||||||
// Pagination parameters
|
// Pagination parameters
|
||||||
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
||||||
|
@ -10,7 +10,6 @@ const router = express.Router();
|
|||||||
router.get(
|
router.get(
|
||||||
"/pd/stock/:userId",
|
"/pd/stock/:userId",
|
||||||
isAuthenticatedUser,
|
isAuthenticatedUser,
|
||||||
authorizeRoles("admin"),
|
|
||||||
getProductsAndStockByPD
|
getProductsAndStockByPD
|
||||||
);
|
);
|
||||||
router.get(
|
router.get(
|
||||||
|
@ -394,6 +394,10 @@ export const uploadPrincipaldistributors = async (req, res) => {
|
|||||||
|
|
||||||
for (let i = 1; i < data.length; i++) {
|
for (let i = 1; i < data.length; i++) {
|
||||||
const row = data[i];
|
const row = data[i];
|
||||||
|
// Skip the row if it's completely empty
|
||||||
|
if (row.every((cell) => cell === undefined || cell === "")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const item = {};
|
const item = {};
|
||||||
|
|
||||||
headers.forEach((header, index) => {
|
headers.forEach((header, index) => {
|
||||||
@ -1084,7 +1088,7 @@ export const getAllPD = catchAsyncErrors(async (req, res, next) => {
|
|||||||
|
|
||||||
// Aggregate orders data for each user
|
// Aggregate orders data for each user
|
||||||
const orderStats = await PdOrder.aggregate([
|
const orderStats = await PdOrder.aggregate([
|
||||||
{ $match: { addedBy: { $in: users.map(user => user._id) } } }, // Match orders for the listed distributors
|
{ $match: { addedBy: { $in: users.map((user) => user._id) } } }, // Match orders for the listed distributors
|
||||||
{
|
{
|
||||||
$group: {
|
$group: {
|
||||||
_id: "$addedBy", // Group by addedBy (distributor ID)
|
_id: "$addedBy", // Group by addedBy (distributor ID)
|
||||||
@ -1095,8 +1099,10 @@ export const getAllPD = catchAsyncErrors(async (req, res, next) => {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
// Map orders data to corresponding users
|
// Map orders data to corresponding users
|
||||||
const usersWithOrderStats = users.map(user => {
|
const usersWithOrderStats = users.map((user) => {
|
||||||
const orderData = orderStats.find(order => order._id.toString() === user._id.toString());
|
const orderData = orderStats.find(
|
||||||
|
(order) => order._id.toString() === user._id.toString()
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
...user.toObject(),
|
...user.toObject(),
|
||||||
totalOrders: orderData ? orderData.totalOrders : 0,
|
totalOrders: orderData ? orderData.totalOrders : 0,
|
||||||
@ -1111,7 +1117,7 @@ export const getAllPD = catchAsyncErrors(async (req, res, next) => {
|
|||||||
total_pages: Math.ceil(totalUsers / limit),
|
total_pages: Math.ceil(totalUsers / limit),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching Principal Distributors:', error);
|
console.error("Error fetching Principal Distributors:", error);
|
||||||
res.status(500).json({ message: "Server Error", error });
|
res.status(500).json({ message: "Server Error", error });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user