diff --git a/src/controllers/bmdashboard/bmInventoryTypeController.js b/src/controllers/bmdashboard/bmInventoryTypeController.js index b9ced243e..5460f529c 100644 --- a/src/controllers/bmdashboard/bmInventoryTypeController.js +++ b/src/controllers/bmdashboard/bmInventoryTypeController.js @@ -10,10 +10,48 @@ const bmInventoryTypeController = function (InvType) { res.json(err); } }; + 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; };