Skip to content

Commit

Permalink
Merge pull request #691 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.30]
  • Loading branch information
one-community authored Jan 9, 2024
2 parents 06616fd + 6fc4379 commit 4096c2c
Show file tree
Hide file tree
Showing 28 changed files with 1,406 additions and 791 deletions.
106 changes: 53 additions & 53 deletions package-lock.json

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions src/controllers/bmdashboard/bmConsumableController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const bmConsumableController = function (BuildingConsumable) {
const fetchBMConsumables = async (req, res) => {
try {
BuildingConsumable
.find()
.populate([
{
path: 'project',
select: '_id name',
},
{
path: 'itemType',
select: '_id name unit',
},
{
path: 'updateRecord',
populate: {
path: 'createdBy',
select: '_id firstName lastName',
},
},
{
path: 'purchaseRecord',
populate: {
path: 'requestedBy',
select: '_id firstName lastName',
},
},
])
.exec()
.then(result => {
res.status(200).send(result);
})
.catch(error => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};

return {
fetchBMConsumables,
};
};

module.exports = bmConsumableController;

91 changes: 86 additions & 5 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,100 @@
const bmInventoryTypeController = function (InvType) {
const fetchMaterialTypes = async (req, res) => {
function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolType, EquipType) {
async function fetchMaterialTypes(req, res) {
try {
InvType
MatType
.find()
.exec()
.then(result => res.status(200).send(result))
.catch(error => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};
}

async function addEquipmentType(req, res) {
const {
name,
desc: description,
fuel: fuelType,
requestor: { requestorId },
} = req.body;
try {
EquipType
.find({ name })
.then((result) => {
if (result.length) {
res.status(409).send();
} else {
const newDoc = {
category: 'Equipment',
name,
description,
fuelType,
createdBy: requestorId,
};
EquipType
.create(newDoc)
.then(() => res.status(201).send())
.catch((error) => {
if (error._message.includes('validation failed')) {
res.status(400).send(error);
} else {
res.status(500).send(error);
}
});
}
})
.catch(error => res.status(500).send(error));
} 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 = {};

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,
addEquipmentType,
fetchSingleInventoryType,
updateNameAndUnit,
};
};
}

module.exports = bmInventoryTypeController;
90 changes: 40 additions & 50 deletions src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');

const bmMaterialsController = function (ItemMaterial,BuildingMaterial) {
const bmMaterialsController = function (ItemMaterial, BuildingMaterial) {
const bmMaterialsList = async function _matsList(req, res) {
try {
BuildingMaterial.find()
Expand Down Expand Up @@ -91,25 +91,20 @@ const bmMaterialsController = function (ItemMaterial,BuildingMaterial) {
};

const bmPostMaterialUpdateRecord = function (req, res) {
let payload = req.body;
const payload = req.body;
let quantityUsed = +req.body.quantityUsed;
let quantityWasted = +req.body.quantityWasted;
let material = req.body.material;
if(payload.QtyUsedLogUnit=='percent' && quantityWasted>=0)
{
const { material } = req.body;
if (payload.QtyUsedLogUnit == 'percent' && quantityWasted >= 0) {
quantityUsed = +((+quantityUsed / 100) * material.stockAvailable).toFixed(4);
}
if(payload.QtyWastedLogUnit=='percent' && quantityUsed>=0)
{
if (payload.QtyWastedLogUnit == 'percent' && quantityUsed >= 0) {
quantityWasted = +((+quantityWasted / 100) * material.stockAvailable).toFixed(4);
}

if(quantityUsed>material.stockAvailable || quantityWasted>material.stockAvailable || (quantityUsed+quantityWasted)>material.stockAvailable)
{
res.status(500).send('Please check the used and wasted stock values. Either individual values or their sum exceeds the total stock available.')
}
else
{
if (quantityUsed > material.stockAvailable || quantityWasted > material.stockAvailable || (quantityUsed + quantityWasted) > material.stockAvailable) {
res.status(500).send('Please check the used and wasted stock values. Either individual values or their sum exceeds the total stock available.');
} else {
let newStockUsed = +material.stockUsed + parseFloat(quantityUsed);
let newStockWasted = +material.stockWasted + parseFloat(quantityWasted);
let newAvailable = +material.stockAvailable - parseFloat(quantityUsed) - parseFloat(quantityWasted);
Expand All @@ -118,45 +113,42 @@ const bmMaterialsController = function (ItemMaterial,BuildingMaterial) {
newAvailable = parseFloat(newAvailable.toFixed(4));
BuildingMaterial.updateOne(
{ _id: req.body.material._id },

{
$set: {
'stockUsed': newStockUsed,
'stockWasted': newStockWasted,
'stockAvailable': newAvailable
stockUsed: newStockUsed,
stockWasted: newStockWasted,
stockAvailable: newAvailable,
},
$push: {
updateRecord: {
date: req.body.date,
createdBy: req.body.requestor.requestorId,
quantityUsed: quantityUsed,
quantityWasted: quantityWasted
quantityUsed,
quantityWasted,
},
}
}
},
},

)
.then(results => {res.status(200).send(results)})
.catch(error => res.status(500).send({'message':error}));
.then((results) => { res.status(200).send(results); })
.catch(error => res.status(500).send({ message: error }));
}
};

const bmPostMaterialUpdateBulk = function (req, res) {
const materialUpdates= req.body.upadateMaterials;
const materialUpdates = req.body.upadateMaterials;
let errorFlag = false;
const updateRecordsToBeAdded = [];
for(let i=0;i<materialUpdates.length;i++)
{
let payload = materialUpdates[i];
for (let i = 0; i < materialUpdates.length; i++) {
const payload = materialUpdates[i];
let quantityUsed = +payload.quantityUsed;
let quantityWasted = +payload.quantityWasted;
let material = payload.material;
if(payload.QtyUsedLogUnit=='percent' && quantityWasted>=0)
{
const { material } = payload;
if (payload.QtyUsedLogUnit == 'percent' && quantityWasted >= 0) {
quantityUsed = +((+quantityUsed / 100) * material.stockAvailable).toFixed(4);
}
if(payload.QtyWastedLogUnit=='percent' && quantityUsed>=0)
{
if (payload.QtyWastedLogUnit == 'percent' && quantityUsed >= 0) {
quantityWasted = +((+quantityWasted / 100) * material.stockAvailable).toFixed(4);
}

Expand All @@ -166,38 +158,36 @@ const bmMaterialsController = function (ItemMaterial,BuildingMaterial) {
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
if(newAvailable<0)
{
if (newAvailable < 0) {
errorFlag = true;
break;
}
updateRecordsToBeAdded.push ({
updateRecordsToBeAdded.push({
updateId: material._id,
set: {
'stockUsed': newStockUsed,
'stockWasted': newStockWasted,
'stockAvailable': newAvailable
stockUsed: newStockUsed,
stockWasted: newStockWasted,
stockAvailable: newAvailable,
},
updateValue: {
createdBy: req.body.requestor.requestorId,
quantityUsed: quantityUsed,
quantityWasted: quantityWasted,
quantityUsed,
quantityWasted,
date: req.body.date,
}});
},
});
}

try {
if(errorFlag)
{
res.status(500).send('Stock quantities submitted seems to be invalid')
if (errorFlag) {
res.status(500).send('Stock quantities submitted seems to be invalid');
return;
}
const updatePromises = updateRecordsToBeAdded.map(updateItem => BuildingMaterial.updateOne(
{ _id: updateItem.updateId },
{
$set : updateItem.set,
$push: { updateRecord: updateItem.updateValue }
{
$set: updateItem.set,
$push: { updateRecord: updateItem.updateValue },
},
).exec());
Promise.all(updatePromises)
Expand All @@ -213,7 +203,7 @@ const bmMaterialsController = function (ItemMaterial,BuildingMaterial) {
bmMaterialsList,
bmPostMaterialUpdateRecord,
bmPostMaterialUpdateBulk,
bmPurchaseMaterials
bmPurchaseMaterials,
};
};

Expand Down
52 changes: 52 additions & 0 deletions src/controllers/bmdashboard/bmToolController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const bmToolController = (BuildingTool) => {
const fetchSingleTool = async (req, res) => {
const { toolId } = req.params;
try {
BuildingTool
.findById(toolId)
.populate([
{
path: 'itemType',
select: '_id name description unit imageUrl category',
},
{
path: 'userResponsible',
select: '_id firstName lastName',
},
{
path: 'purchaseRecord',
populate: {
path: 'requestedBy',
select: '_id firstName lastName',
},
},
{
path: 'updateRecord',
populate: {
path: 'createdBy',
select: '_id firstName lastName',
},
},
{
path: 'logRecord',
populate: [{
path: 'createdBy',
select: '_id firstName lastName',
},
{
path: 'responsibleUser',
select: '_id firstName lastName',
}],
},
])
.exec()
.then(tool => res.status(200).send(tool))
.catch(error => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};
return { fetchSingleTool };
};

module.exports = bmToolController;
Loading

0 comments on commit 4096c2c

Please sign in to comment.