This commit is contained in:
Sibunnayak 2024-10-18 17:09:43 +05:30
parent 91f0c75bdf
commit 5a232800e9
6 changed files with 49 additions and 20 deletions

View File

@ -86,14 +86,6 @@ export const uploadProducts = async (req, res) => {
}
});
// Check if the row has meaningful data, skip if it's mostly empty
const hasValidData = Object.values(item).some(
(value) => value && value.trim()
);
if (!hasValidData) {
continue;
}
// Initialize error tracking for each item
const missingFields = new Set();
@ -102,8 +94,10 @@ export const uploadProducts = async (req, res) => {
// Trim leading and trailing spaces and apply case formatting
name = name ? name.trim() : "";
category = category ? capitalizeWords(category) : "";
brand = brand ? capitalizeWords(brand) : "";
// category = category ? capitalizeWords(category) : "";
// brand = brand ? capitalizeWords(brand) : "";
category = category ? category.trim() : "";
brand = brand ? brand.trim() : "";
// Validate required fields
if (!SKU) missingFields.add("SKU");
@ -296,7 +290,10 @@ export const uploadProducts = async (req, res) => {
}
}
fs.unlinkSync(filePath); // Clean up uploaded file
// Clean up uploaded file
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(201).json({
message:
@ -309,6 +306,10 @@ export const uploadProducts = async (req, res) => {
});
} catch (error) {
console.error("Error uploading products:", error);
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(500).json({ message: "Server error" });
}
};

View File

@ -314,7 +314,10 @@ export const uploadRetaildistributors = async (req, res) => {
newlyCreated.push({ Kyc });
}
}
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(200).json({
message: "File processed successfully",
newlyCreated,
@ -323,6 +326,10 @@ export const uploadRetaildistributors = async (req, res) => {
});
} catch (error) {
console.error(error);
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(500).json({ message: "Internal Server Error" });
}
};

View File

@ -240,7 +240,10 @@ export const uploadSalesCoordinators = async (req, res) => {
newlyCreated.push({ salesCoordinator });
}
}
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(200).json({
message: "File processed successfully",
newlyCreated,
@ -249,6 +252,10 @@ export const uploadSalesCoordinators = async (req, res) => {
});
} catch (error) {
console.error(error);
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(500).json({ message: "Internal Server Error" });
}
};

View File

@ -238,7 +238,10 @@ export const uploadTerritoryManagers = async (req, res) => {
newlyCreated.push({ territoryManager });
}
}
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(200).json({
message: "File processed successfully",
newlyCreated,
@ -247,6 +250,10 @@ export const uploadTerritoryManagers = async (req, res) => {
});
} catch (error) {
console.error(error);
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(500).json({ message: "Internal Server Error" });
}
};

View File

@ -234,7 +234,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
// Update user
if (userUpdated) {
await distributor.save();
await distributorbyid.save();
}
// Check for changes in address details
@ -264,7 +264,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
if (addressUpdated) {
await ShippingAddress.updateOne(
{ user: distributor._id },
{ user: distributorbyid._id },
addressData
);
if (addressUpdates.length > 0) {
@ -281,7 +281,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
// Add to updatedDistributors only if there are updated fields
if (updatedFields.length > 0) {
updatedDistributors.push({
...distributor._doc,
...distributorbyid._doc,
updatedFields: updatedFields.join(", "),
});
}
@ -295,7 +295,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
phone: item.phone || "N/A",
panNumber: item.panNumber || "N/A",
gstNumber: item.gstNumber || "N/A",
message: ` Employee Code (${distributorbyid.uniqueId}) is refer to (${distributorbyid.name}) and Email ID (${distributorbymail.mobileNumber}) refer to (${distributorbymail.name}) Principal Distributor. Please provide the correct employee code or Email ID.`,
message: ` Employee Code (${distributorbyid.uniqueId}) is refer to (${distributorbyid.name}) and Email ID (${distributorbymail.email}) refer to (${distributorbymail.name}) Principal Distributor. Please provide the correct employee code or Email ID.`,
});
}
} else if (distributorbyid) {
@ -303,7 +303,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
distributorbyid.email = item.email;
await distributorbyid.save();
updatedDistributors.push({
...distributor._doc,
...distributorbyid._doc,
updatedFields: "Email",
});
} else if (distributorbymail) {
@ -320,7 +320,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
});
} else {
// Create a new user
distributor = new User({
const distributor = new User({
name: item.name,
SBU: item.SBU,
email: item.email,
@ -368,6 +368,9 @@ export const uploadPrincipaldistributors = async (req, res) => {
}
}
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(200).json({
message: "File processed successfully",
newlyCreated,
@ -376,6 +379,10 @@ export const uploadPrincipaldistributors = async (req, res) => {
});
} catch (error) {
console.error(error);
// Clean up uploaded file if any error occurs
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(500).json({ message: "Internal Server Error" });
}
};