Duplicate error status code fixed
This commit is contained in:
parent
5934130888
commit
dea439e4e2
@ -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: {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user