Skip to content

Commit

Permalink
Merge pull request #1005 from OneCommunityGlobal/Shereen_Reusables_Bu…
Browse files Browse the repository at this point in the history
…lk_Update

Shereen_Bulk Reusables Update
  • Loading branch information
one-community authored Jul 8, 2024
2 parents 931c78b + 2eccbc6 commit 6dfab38
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 4 deletions.
153 changes: 149 additions & 4 deletions src/controllers/bmdashboard/bmReusableController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const {
reusableType: ReusableType,
} = require('../../models/bmdashboard/buildingInventoryType');

function isValidDate(dateString) {
const date = new Date(dateString);
return !isNaN(date.getTime());
}
// function isValidDate(dateString) {
// const date = new Date(dateString);
// return !isNaN(date.getTime());
// }

const bmReusableController = function (BuildingReusable) {
const fetchBMReusables = async (req, res) => {
Expand Down Expand Up @@ -103,9 +103,154 @@ const bmReusableController = function (BuildingReusable) {
}
};

const bmPostReusableUpdateRecord = function (req, res) {
const payload = req.body;
let quantityUsed = +req.body.quantityUsed;
let quantityWasted = +req.body.quantityWasted;
const { reusable } = req.body;
if (payload.QtyUsedLogUnit === "percent" && quantityWasted >= 0) {
quantityUsed = +((+quantityUsed / 100) * reusable.stockAvailable).toFixed(
4
);
}
if (payload.QtyWastedLogUnit === "percent" && quantityUsed >= 0) {
quantityWasted = +(
(+quantityWasted / 100) *
reusable.stockAvailable
).toFixed(4);
}

if (
quantityUsed > reusable.stockAvailable ||
quantityWasted > reusable.stockAvailable ||
quantityUsed + quantityWasted > reusable.stockAvailable
) {
res
.status(500)
.send(
"Please check the used and wasted stock values. Either individual values or their sum exceeds the total stock available."
);
} else {
let newStockUsed = +reusable.stockUsed + parseFloat(quantityUsed);
let newStockWasted = +reusable.stockWasted + parseFloat(quantityWasted);
let newAvailable =
+reusable.stockAvailable -
parseFloat(quantityUsed) -
parseFloat(quantityWasted);
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
BuildingReusable.updateOne(
{ _id: req.body.reusable._id },

{
$set: {
stockUsed: newStockUsed,
stockWasted: newStockWasted,
stockAvailable: newAvailable,
},
$push: {
updateRecord: {
date: req.body.date,
createdBy: req.body.requestor.requestorId,
quantityUsed,
quantityWasted,
},
},
}
)
.then((results) => {
res.status(200).send(results);
})
.catch((error) => res.status(500).send({ message: error }));
}
};

const bmPostReusableUpdateBulk = function (req, res) {
const reusableUpdates = req.body.upadateReusables;
let errorFlag = false;
const updateRecordsToBeAdded = [];
for (let i = 0; i < reusableUpdates.length; i+=1) {
const payload = reusableUpdates[i];
let quantityUsed = +payload.quantityUsed;
let quantityWasted = +payload.quantityWasted;
const { reusable } = payload;
if (payload.QtyUsedLogUnit === "percent" && quantityWasted >= 0) {
quantityUsed = +(
(+quantityUsed / 100) *
reusable.stockAvailable
).toFixed(4);
}
if (payload.QtyWastedLogUnit === "percent" && quantityUsed >= 0) {
quantityWasted = +(
(+quantityWasted / 100) *
reusable.stockAvailable
).toFixed(4);
}

let newStockUsed = +reusable.stockUsed + parseFloat(quantityUsed);
let newStockWasted = +reusable.stockWasted + parseFloat(quantityWasted);
let newAvailable =
+reusable.stockAvailable -
parseFloat(quantityUsed) -
parseFloat(quantityWasted);
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
if (newAvailable < 0) {
errorFlag = true;
break;
}
updateRecordsToBeAdded.push({
updateId: reusable._id,
set: {
stockUsed: newStockUsed,
stockWasted: newStockWasted,
stockAvailable: newAvailable,
},
updateValue: {
createdBy: req.body.requestor.requestorId,
quantityUsed,
quantityWasted,
date: req.body.date,
},
});
}

try {
if (errorFlag) {
res.status(500).send("Stock quantities submitted seems to be invalid");
return;
}
const updatePromises = updateRecordsToBeAdded.map((updateItem) =>
BuildingReusable.updateOne(
{ _id: updateItem.updateId },
{
$set: updateItem.set,
$push: { updateRecord: updateItem.updateValue },
}
).exec()
);
Promise.all(updatePromises)
.then((results) => {
res.status(200).send({
result: `Successfully posted log for ${results.length} Reusable records.`,
});
})
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};




return {
fetchBMReusables,
purchaseReusable,
bmPostReusableUpdateRecord,
bmPostReusableUpdateBulk,
};
};

Expand Down
6 changes: 6 additions & 0 deletions src/routes/bmdashboard/bmReusableRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const routes = function (BuildingReusable) {
BuildingReusableController.route('/reusables/purchase')
.post(controller.purchaseReusable);

BuildingReusableController.route('/updateReusableRecord')
.post(controller.bmPostReusableUpdateRecord);

BuildingReusableController.route('/updateReusableRecordBulk')
.post(controller.bmPostReusableUpdateBulk);

return BuildingReusableController;
};

Expand Down

0 comments on commit 6dfab38

Please sign in to comment.