Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kaikanes-Routes/Controllers for updating name and unit of measurement #630

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading