From c1294d44413f8b67abbadbb0ac8ab1e953ab967d Mon Sep 17 00:00:00 2001 From: lacnoskillz Date: Fri, 1 Dec 2023 12:14:46 -0600 Subject: [PATCH 1/2] remake branch without package-lock.json modifications --- .../bmdashboard/bmInventoryTypeController.js | 53 +++++++++++++++---- .../bmdashboard/bmInventoryTypeRouter.js | 5 ++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/controllers/bmdashboard/bmInventoryTypeController.js b/src/controllers/bmdashboard/bmInventoryTypeController.js index b9ced243e..f7e964f86 100644 --- a/src/controllers/bmdashboard/bmInventoryTypeController.js +++ b/src/controllers/bmdashboard/bmInventoryTypeController.js @@ -1,19 +1,54 @@ const bmInventoryTypeController = function (InvType) { const fetchMaterialTypes = async (req, res) => { try { - InvType - .find() - .exec() - .then(result => res.status(200).send(result)) - .catch(error => res.status(500).send(error)); - } catch (err) { - res.json(err); + const result = await InvType.find().exec(); + res.status(200).send(result); + } catch (error) { + res.status(500).send(error); } }; + const fetchSingleInventoryType = async (req, res) => { + const { invtypeId } = req.params; + try { + const result = await InvType.findById(invtypeId).exec(); + res.status(200).send(result); + } catch (error) { + res.status(500).send(error); + } + }; + + const updateNameAndUnit = async (req, res) => { + try { + const { invtypeId } = req.params; + const { name, unit } = req.body; + + const updateData = {}; - return { - fetchMaterialTypes, + if (name) { + updateData.name = name; + } + + if (unit) { + updateData.unit = unit; + } + + const updatedInvType = await InvType.findByIdAndUpdate( + invtypeId, + updateData, + { new: true, runValidators: true }, + ); + + if (!updatedInvType) { + return res.status(404).json({ error: 'invType Material not found check Id' }); + } + + res.status(200).json(updatedInvType); + } catch (error) { + res.status(500).send(error); + } }; + + return { fetchMaterialTypes, fetchSingleInventoryType, updateNameAndUnit }; }; module.exports = bmInventoryTypeController; diff --git a/src/routes/bmdashboard/bmInventoryTypeRouter.js b/src/routes/bmdashboard/bmInventoryTypeRouter.js index ceae439dc..e62e71c07 100644 --- a/src/routes/bmdashboard/bmInventoryTypeRouter.js +++ b/src/routes/bmdashboard/bmInventoryTypeRouter.js @@ -4,9 +4,14 @@ const routes = function (invType) { const inventoryTypeRouter = express.Router(); const controller = require('../../controllers/bmdashboard/bmInventoryTypeController')(invType); + // Route for fetching all material types inventoryTypeRouter.route('/invtypes/materials') .get(controller.fetchMaterialTypes); + // Combined routes for getting a single inventory type and updating its name and unit of measurement + inventoryTypeRouter.route('/invtypes/material/:invtypeId') + .get(controller.fetchSingleInventoryType) + .put(controller.updateNameAndUnit); return inventoryTypeRouter; }; From 28badf296ee9d88dd2c50b0d0b9c9e9e858cc2fe Mon Sep 17 00:00:00 2001 From: lacnoskillz Date: Fri, 1 Dec 2023 12:34:55 -0600 Subject: [PATCH 2/2] changed fetchMaterialTypes back to how it was before --- .../bmdashboard/bmInventoryTypeController.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/controllers/bmdashboard/bmInventoryTypeController.js b/src/controllers/bmdashboard/bmInventoryTypeController.js index f7e964f86..5460f529c 100644 --- a/src/controllers/bmdashboard/bmInventoryTypeController.js +++ b/src/controllers/bmdashboard/bmInventoryTypeController.js @@ -1,10 +1,13 @@ const bmInventoryTypeController = function (InvType) { const fetchMaterialTypes = async (req, res) => { try { - const result = await InvType.find().exec(); - res.status(200).send(result); - } catch (error) { - res.status(500).send(error); + InvType + .find() + .exec() + .then(result => res.status(200).send(result)) + .catch(error => res.status(500).send(error)); + } catch (err) { + res.json(err); } }; const fetchSingleInventoryType = async (req, res) => {