diff --git a/app/controllers/products.controller.js b/app/controllers/products.controller.js index 2c50573..f337f82 100644 --- a/app/controllers/products.controller.js +++ b/app/controllers/products.controller.js @@ -270,7 +270,7 @@ const filePath = uploadedPath; let hsCode = row.hs_code?.replace(/[-\s/]/g, "").trim(); const productName = row.product_name?.replace(/\s+/g, " ").trim(); const unit = row.unit?.trim().toLowerCase(); - const description = row.description?.trim().toLowerCase(); + const description = row.description?.trim().toLowerCase() || ""; if (!hsCode || !productName) { errors.push({ row: index + 1, error: "Missing required HS Code or Product Name" }); @@ -356,17 +356,25 @@ const filePath = uploadedPath; if (fs.existsSync(filePath)) fs.unlinkSync(filePath); let finalStatus = "success"; - let message = `${inserted.length} products inserted successfully.`; + 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} validation errors.` - : `No products imported. ${duplicates.length} duplicates and ${errors.length} validation errors.`; + if (errors.length === 0 && duplicates.length === 0) { + finalStatus = "success"; + httpCode = 200; + } + else if (duplicates.length > 0 || errors.length > 0) { + finalStatus = "failed"; + message = `No units imported. ${duplicates.length} duplicates and ${errors.length} errors found.`; + httpCode = 422; + } + else if (inserted.length === 0) { + finalStatus = "failed"; + message = `No units imported. ${duplicates.length} duplicates and ${errors.length} errors found.`; + httpCode = 400; } - return res.status(200).send({ + return res.status(httpCode).send({ status: finalStatus, message, summary: {