Skip to content

Commit

Permalink
Merge pull request #630 from OneCommunityGlobal/kaikane-updateNameAnd…
Browse files Browse the repository at this point in the history
…UnitOfMeasurement-ControllerAndRoutes

Kaikanes-Routes/Controllers for updating name and unit of measurement
  • Loading branch information
tdkent authored Jan 6, 2024
2 parents 27c2361 + 28badf2 commit 3003610
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions src/routes/bmdashboard/bmInventoryTypeRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit 3003610

Please sign in to comment.