Skip to content
Open
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
54 changes: 39 additions & 15 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,35 +359,58 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
}
};

const updateNameAndUnit = async (req, res) => {
// update only the name field by ObjectId
const updateInventoryName = async (req, res) => {
try {
const { invtypeId } = req.params;
const { name, unit } = req.body;
const { name } = req.body;

const updateData = {};
if (!name) {
return res.status(400).json({ error: 'name is required' });
}

const updatedInvType = await InvType.findByIdAndUpdate(
invtypeId,
{ name },
{ new: true, runValidators: true },
);

if (name) {
updateData.name = name;
if (!updatedInvType) {
return res.status(404).json({ error: 'Inventory type not found, check Id' });
}

if (unit) {
updateData.unit = unit;
return res.status(200).json(updatedInvType);
} catch (error) {
return res.status(500).send(error);
}
};

// update only the unit field by ObjectId
const updateInventoryUnit = async (req, res) => {
try {
const { invtypeId } = req.params;
const { unit } = req.body;

if (!unit) {
return res.status(400).json({ error: 'unit is required' });
}

const updatedInvType = await InvType.findByIdAndUpdate(invtypeId, updateData, {
new: true,
runValidators: true,
});
const updatedInvType = await InvType.findByIdAndUpdate(
invtypeId,
{ unit },
{ new: true, runValidators: true },
);

if (!updatedInvType) {
return res.status(404).json({ error: 'invType Material not found check Id' });
return res.status(404).json({ error: 'Inventory type not found, check Id' });
}

res.status(200).json(updatedInvType);
return res.status(200).json(updatedInvType);
} catch (error) {
res.status(500).send(error);
return res.status(500).send(error);
}
};

return {
fetchMaterialTypes,
fetchConsumableTypes,
Expand All @@ -396,7 +419,8 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
addEquipmentType,
fetchEquipmentTypes,
fetchSingleInventoryType,
updateNameAndUnit,
updateInventoryName,
updateInventoryUnit,
addMaterialType,
addConsumableType,
addToolType,
Expand Down
31 changes: 31 additions & 0 deletions src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,44 @@ const bmMaterialsController = function (BuildingMaterial) {
}
};

const bmUpdateItemTypeName = async function (req, res) {
try {
const { itemTypeId, newName } = req.body;

if (!itemTypeId || !newName) {
return res.status(400).json({ message: 'itemTypeId and newName are required.' });
}

// Load ItemType model
const ItemType = mongoose.model('MaterialType');

const updated = await ItemType.findByIdAndUpdate(
itemTypeId,
{ $set: { name: newName } },
{ new: true },
);

if (!updated) {
return res.status(404).json({ message: 'ItemType not found.' });
}

res.status(200).json({
message: 'Material type name updated successfully.',
updated,
});
} catch (error) {
res.status(500).json({ error: error.message });
}
};

return {
bmMaterialsList,
bmPostMaterialUpdateRecord,
bmPostMaterialUpdateBulk,
bmPurchaseMaterials,
bmupdatePurchaseStatus,
bmGetMaterialSummaryByProject,
bmUpdateItemTypeName,
};
};

Expand Down
18 changes: 16 additions & 2 deletions src/routes/bmdashboard/bmInventoryTypeRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,25 @@ const routes = function (baseInvType, matType, consType, reusType, toolType, equ
// 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);
.get(controller.fetchSingleInventoryType);

inventoryTypeRouter.route('/inventoryUnits').get(controller.fetchInvUnitsFromJson);

// get a single inventory type (material) by id
inventoryTypeRouter
.route('/invtypes/material/:invtypeId')
.get(controller.fetchSingleInventoryType);

// update only the name by id
inventoryTypeRouter
.route('/invtypes/material/:invtypeId/name')
.put(controller.updateInventoryName);

// update only the unit by id
inventoryTypeRouter
.route('/invtypes/material/:invtypeId/unit')
.put(controller.updateInventoryUnit);

return inventoryTypeRouter;
};

Expand Down
2 changes: 2 additions & 0 deletions src/routes/bmdashboard/bmMaterialsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const routes = function (buildingMaterial) {
.get(controller.bmMaterialsList)
.post(controller.bmPurchaseMaterials);

materialsRouter.route('/itemType/updateName').patch(controller.bmUpdateItemTypeName);

materialsRouter.route('/updateMaterialRecord').post(controller.bmPostMaterialUpdateRecord);

materialsRouter.route('/updateMaterialRecordBulk').post(controller.bmPostMaterialUpdateBulk);
Expand Down
Loading