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);
|
fs.unlinkSync(filePath);
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// FINAL RESPONSE
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
|
|
||||||
let finalStatus = "success";
|
let finalStatus = "success";
|
||||||
let message = `${inserted.length} units inserted successfully.`;
|
let message = `${inserted.length} units inserted successfully.`;
|
||||||
|
let httpCode = 200;
|
||||||
|
|
||||||
if (errors.length > 0 || duplicates.length > 0) {
|
// Case 1: all good
|
||||||
finalStatus = inserted.length > 0 ? "partial_success" : "failed";
|
if (errors.length === 0 && duplicates.length === 0) {
|
||||||
message =
|
finalStatus = "success";
|
||||||
finalStatus === "partial_success"
|
httpCode = 200;
|
||||||
? `${inserted.length} inserted, ${duplicates.length} duplicates skipped, ${errors.length} errors found.`
|
|
||||||
: `No units imported. ${duplicates.length} duplicates and ${errors.length} errors found.`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
status: finalStatus,
|
||||||
message,
|
message,
|
||||||
summary: {
|
summary: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user