From 2eccbc6e811226305565ba403dcc12c669d73cb7 Mon Sep 17 00:00:00 2001 From: Shereen Punnassery Date: Wed, 26 Jun 2024 13:57:37 -0700 Subject: [PATCH] Added Routing and Controller Function Added Routing and Controller Function for Bulk Reusables Update --- .../bmdashboard/bmReusableController.js | 153 +++++++++++++++++- src/routes/bmdashboard/bmReusableRouter.js | 6 + 2 files changed, 155 insertions(+), 4 deletions(-) diff --git a/src/controllers/bmdashboard/bmReusableController.js b/src/controllers/bmdashboard/bmReusableController.js index e4ae0574f..83cd19475 100644 --- a/src/controllers/bmdashboard/bmReusableController.js +++ b/src/controllers/bmdashboard/bmReusableController.js @@ -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) => { @@ -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, }; }; diff --git a/src/routes/bmdashboard/bmReusableRouter.js b/src/routes/bmdashboard/bmReusableRouter.js index a6235823c..2a8c7af52 100644 --- a/src/routes/bmdashboard/bmReusableRouter.js +++ b/src/routes/bmdashboard/bmReusableRouter.js @@ -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; };