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

Luis fix user profile projects delay #596

Merged
merged 11 commits into from
May 2, 2024
209 changes: 121 additions & 88 deletions src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
const mongoose = require('mongoose');
const mongoose = require("mongoose");

const bmMaterialsController = function (BuildingMaterial) {
const bmMaterialsList = async function _matsList(req, res) {
try {
BuildingMaterial.find()
.populate([
{
path: 'project',
select: '_id name',
},
{
path: 'itemType',
select: '_id name unit',
},
{
path: 'updateRecord',
populate: {
path: 'createdBy',
select: '_id firstName lastName',
.populate([
{
path: "project",
select: "_id name",
},
},
{
path: 'purchaseRecord',
populate: {
path: 'requestedBy',
select: '_id firstName lastName',
{
path: "itemType",
select: "_id name unit",
},
},
])
.exec()
.then(results => res.status(200).send(results))
.catch(error => res.status(500).send(error));
{
path: "updateRecord",
populate: {
path: "createdBy",
select: "_id firstName lastName",
},
},
{
path: "purchaseRecord",
populate: {
path: "requestedBy",
select: "_id firstName lastName",
},
},
])
.exec()
.then((results) => res.status(200).send(results))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand Down Expand Up @@ -64,27 +64,28 @@ const bmMaterialsController = function (BuildingMaterial) {
brandPref,
requestedBy: requestorId,
};
const doc = await BuildingMaterial.findOne({ project: projectId, itemType: matTypeId });
const doc = await BuildingMaterial.findOne({
project: projectId,
itemType: matTypeId,
});
if (!doc) {
const newDoc = {
itemType: matTypeId,
project: projectId,
purchaseRecord: [newPurchaseRecord],
};
BuildingMaterial
.create(newDoc)
.then(() => res.status(201).send())
.catch(error => res.status(500).send(error));
return;
BuildingMaterial.create(newDoc)
.then(() => res.status(201).send())
.catch((error) => res.status(500).send(error));
return;
}
BuildingMaterial
.findOneAndUpdate(
{ _id: mongoose.Types.ObjectId(doc._id) },
{ $push: { purchaseRecord: newPurchaseRecord } },
)
BuildingMaterial.findOneAndUpdate(
{ _id: mongoose.Types.ObjectId(doc._id) },
{ $push: { purchaseRecord: newPurchaseRecord } }
)
.exec()
.then(() => res.status(201).send())
.catch(error => res.status(500).send(error));
.catch((error) => res.status(500).send(error));
} catch (error) {
res.status(500).send(error);
}
Expand All @@ -95,45 +96,62 @@ const bmMaterialsController = function (BuildingMaterial) {
let quantityUsed = +req.body.quantityUsed;
let quantityWasted = +req.body.quantityWasted;
const { material } = req.body;
if (payload.QtyUsedLogUnit == 'percent' && quantityWasted >= 0) {
quantityUsed = +((+quantityUsed / 100) * material.stockAvailable).toFixed(4);
}
if (payload.QtyWastedLogUnit == 'percent' && quantityUsed >= 0) {
quantityWasted = +((+quantityWasted / 100) * material.stockAvailable).toFixed(4);
}
if (payload.QtyUsedLogUnit == "percent" && quantityWasted >= 0) {
quantityUsed = +((+quantityUsed / 100) * material.stockAvailable).toFixed(
4
);
}
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.');
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);
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
BuildingMaterial.updateOne(
let newStockUsed = +material.stockUsed + parseFloat(quantityUsed);
let newStockWasted = +material.stockWasted + parseFloat(quantityWasted);
let newAvailable =
+material.stockAvailable -
parseFloat(quantityUsed) -
parseFloat(quantityWasted);
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
BuildingMaterial.updateOne(
{ _id: req.body.material._id },

{
$set: {
{
$set: {
stockUsed: newStockUsed,
stockWasted: newStockWasted,
stockAvailable: newAvailable,
},
$push: {
updateRecord: {
date: req.body.date,
createdBy: req.body.requestor.requestorId,
quantityUsed,
quantityWasted,
},
$push: {
updateRecord: {
date: req.body.date,
createdBy: req.body.requestor.requestorId,
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) {
Expand All @@ -145,16 +163,25 @@ const bmMaterialsController = function (BuildingMaterial) {
let quantityUsed = +payload.quantityUsed;
let quantityWasted = +payload.quantityWasted;
const { material } = payload;
if (payload.QtyUsedLogUnit == 'percent' && quantityWasted >= 0) {
quantityUsed = +((+quantityUsed / 100) * material.stockAvailable).toFixed(4);
if (payload.QtyUsedLogUnit == "percent" && quantityWasted >= 0) {
quantityUsed = +(
(+quantityUsed / 100) *
material.stockAvailable
).toFixed(4);
}
if (payload.QtyWastedLogUnit == 'percent' && quantityUsed >= 0) {
quantityWasted = +((+quantityWasted / 100) * material.stockAvailable).toFixed(4);
if (payload.QtyWastedLogUnit == "percent" && quantityUsed >= 0) {
quantityWasted = +(
(+quantityWasted / 100) *
material.stockAvailable
).toFixed(4);
}

let newStockUsed = +material.stockUsed + parseFloat(quantityUsed);
let newStockWasted = +material.stockWasted + parseFloat(quantityWasted);
let newAvailable = +material.stockAvailable - parseFloat(quantityUsed) - parseFloat(quantityWasted);
let newAvailable =
+material.stockAvailable -
parseFloat(quantityUsed) -
parseFloat(quantityWasted);
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
Expand All @@ -175,26 +202,32 @@ const bmMaterialsController = function (BuildingMaterial) {
quantityWasted,
date: req.body.date,
},
});
}
});
}

try {
if (errorFlag) {
res.status(500).send('Stock quantities submitted seems to be invalid');
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 },
},
).exec());
Promise.all(updatePromises)
.then((results) => {
res.status(200).send({ result: `Successfully posted log for ${results.length} Material records.` });
})
.catch(error => res.status(500).send(error));
const updatePromises = updateRecordsToBeAdded.map((updateItem) =>
BuildingMaterial.updateOne(
{ _id: updateItem.updateId },
{
$set: updateItem.set,
$push: { updateRecord: updateItem.updateValue },
}
).exec()
);
Promise.all(updatePromises)
.then((results) => {
res
.status(200)
.send({
result: `Successfully posted log for ${results.length} Material records.`,
});
})
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
Expand All @@ -204,7 +237,7 @@ const bmMaterialsController = function (BuildingMaterial) {
bmPostMaterialUpdateRecord,
bmPostMaterialUpdateBulk,
bmPurchaseMaterials,
};
};
};

module.exports = bmMaterialsController;
Loading
Loading