diff --git a/public/uploads/CHEMINOVA_APP_PD_AS_ON_17_10_24_TEST_01.xlsx b/public/uploads/CHEMINOVA_APP_PD_AS_ON_17_10_24_TEST_01.xlsx deleted file mode 100644 index 39b50ef..0000000 Binary files a/public/uploads/CHEMINOVA_APP_PD_AS_ON_17_10_24_TEST_01.xlsx and /dev/null differ diff --git a/resources/Products/ProductController.js b/resources/Products/ProductController.js index 319a56a..df701e5 100644 --- a/resources/Products/ProductController.js +++ b/resources/Products/ProductController.js @@ -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" }); } }; diff --git a/resources/RetailDistributor/RetailDistributorController.js b/resources/RetailDistributor/RetailDistributorController.js index 488234d..bd3cbf4 100644 --- a/resources/RetailDistributor/RetailDistributorController.js +++ b/resources/RetailDistributor/RetailDistributorController.js @@ -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" }); } }; diff --git a/resources/SalesCoOrdinators/SalesCoOrdinatorController.js b/resources/SalesCoOrdinators/SalesCoOrdinatorController.js index 965df20..ee8fe0e 100644 --- a/resources/SalesCoOrdinators/SalesCoOrdinatorController.js +++ b/resources/SalesCoOrdinators/SalesCoOrdinatorController.js @@ -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" }); } }; diff --git a/resources/TerritoryManagers/TerritoryManagerController.js b/resources/TerritoryManagers/TerritoryManagerController.js index e083a5e..5b4cb28 100644 --- a/resources/TerritoryManagers/TerritoryManagerController.js +++ b/resources/TerritoryManagers/TerritoryManagerController.js @@ -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" }); } }; diff --git a/resources/user/userController.js b/resources/user/userController.js index 7b4f980..c886d3a 100644 --- a/resources/user/userController.js +++ b/resources/user/userController.js @@ -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" }); } };