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 // Initialize error tracking for each item
const missingFields = new Set(); const missingFields = new Set();
@ -102,8 +94,10 @@ export const uploadProducts = async (req, res) => {
// Trim leading and trailing spaces and apply case formatting // Trim leading and trailing spaces and apply case formatting
name = name ? name.trim() : ""; name = name ? name.trim() : "";
category = category ? capitalizeWords(category) : ""; // category = category ? capitalizeWords(category) : "";
brand = brand ? capitalizeWords(brand) : ""; // brand = brand ? capitalizeWords(brand) : "";
category = category ? category.trim() : "";
brand = brand ? brand.trim() : "";
// Validate required fields // Validate required fields
if (!SKU) missingFields.add("SKU"); 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({ res.status(201).json({
message: message:
@ -309,6 +306,10 @@ export const uploadProducts = async (req, res) => {
}); });
} catch (error) { } catch (error) {
console.error("Error uploading products:", 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" }); res.status(500).json({ message: "Server error" });
} }
}; };

View File

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

View File

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

View File

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

View File

@ -234,7 +234,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
// Update user // Update user
if (userUpdated) { if (userUpdated) {
await distributor.save(); await distributorbyid.save();
} }
// Check for changes in address details // Check for changes in address details
@ -264,7 +264,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
if (addressUpdated) { if (addressUpdated) {
await ShippingAddress.updateOne( await ShippingAddress.updateOne(
{ user: distributor._id }, { user: distributorbyid._id },
addressData addressData
); );
if (addressUpdates.length > 0) { if (addressUpdates.length > 0) {
@ -281,7 +281,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
// Add to updatedDistributors only if there are updated fields // Add to updatedDistributors only if there are updated fields
if (updatedFields.length > 0) { if (updatedFields.length > 0) {
updatedDistributors.push({ updatedDistributors.push({
...distributor._doc, ...distributorbyid._doc,
updatedFields: updatedFields.join(", "), updatedFields: updatedFields.join(", "),
}); });
} }
@ -295,7 +295,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
phone: item.phone || "N/A", phone: item.phone || "N/A",
panNumber: item.panNumber || "N/A", panNumber: item.panNumber || "N/A",
gstNumber: item.gstNumber || "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) { } else if (distributorbyid) {
@ -303,7 +303,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
distributorbyid.email = item.email; distributorbyid.email = item.email;
await distributorbyid.save(); await distributorbyid.save();
updatedDistributors.push({ updatedDistributors.push({
...distributor._doc, ...distributorbyid._doc,
updatedFields: "Email", updatedFields: "Email",
}); });
} else if (distributorbymail) { } else if (distributorbymail) {
@ -320,7 +320,7 @@ export const uploadPrincipaldistributors = async (req, res) => {
}); });
} else { } else {
// Create a new user // Create a new user
distributor = new User({ const distributor = new User({
name: item.name, name: item.name,
SBU: item.SBU, SBU: item.SBU,
email: item.email, email: item.email,
@ -368,6 +368,9 @@ export const uploadPrincipaldistributors = async (req, res) => {
} }
} }
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
res.status(200).json({ res.status(200).json({
message: "File processed successfully", message: "File processed successfully",
newlyCreated, newlyCreated,
@ -376,6 +379,10 @@ export const uploadPrincipaldistributors = async (req, res) => {
}); });
} catch (error) { } catch (error) {
console.error(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" }); res.status(500).json({ message: "Internal Server Error" });
} }
}; };