From dea439e4e2f06555416ea0e8e506b5097964cd24 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 11 Dec 2025 16:21:49 +0530 Subject: [PATCH] Duplicate error status code fixed --- app/controllers/unitMasterController.js | 32 +++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/app/controllers/unitMasterController.js b/app/controllers/unitMasterController.js index f4d7c50..84d33fb 100644 --- a/app/controllers/unitMasterController.js +++ b/app/controllers/unitMasterController.js @@ -425,23 +425,31 @@ exports.uploadUnitMasterFromCSV = async (req, res) => { } fs.unlinkSync(filePath); - - // ------------------------------------------------------------------- - // FINAL RESPONSE - // ------------------------------------------------------------------- - let finalStatus = "success"; let message = `${inserted.length} units inserted successfully.`; + let httpCode = 200; - if (errors.length > 0 || duplicates.length > 0) { - finalStatus = inserted.length > 0 ? "partial_success" : "failed"; - message = - finalStatus === "partial_success" - ? `${inserted.length} inserted, ${duplicates.length} duplicates skipped, ${errors.length} errors found.` - : `No units imported. ${duplicates.length} duplicates and ${errors.length} errors found.`; + // Case 1: all good + if (errors.length === 0 && duplicates.length === 0) { + finalStatus = "success"; + httpCode = 200; } - return res.status(200).send({ + // Case 2: partial success + else if (inserted.length > 0 && (duplicates.length > 0 || errors.length > 0)) { + finalStatus = "partial_success"; + message = `${inserted.length} inserted, ${duplicates.length} duplicates skipped, ${errors.length} errors found.`; + httpCode = 206; // Partial Content + } + + // Case 3: failed (no imports) + else if (inserted.length === 0) { + finalStatus = "failed"; + message = `No units imported. ${duplicates.length} duplicates and ${errors.length} errors found.`; + httpCode = 400; + } + + return res.status(httpCode).send({ status: finalStatus, message, summary: {