Skip to content

Commit

Permalink
Merge pull request #596 from OneCommunityGlobal/luis-fix-user-profile…
Browse files Browse the repository at this point in the history
…-projects-delay

Luis fix user profile projects delay
  • Loading branch information
one-community authored May 2, 2024
2 parents 960b6a3 + 2dd23c7 commit 5adcae8
Show file tree
Hide file tree
Showing 12 changed files with 788 additions and 635 deletions.
117 changes: 74 additions & 43 deletions src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
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: "project",
select: "_id name",
},
{
path: 'itemType',
select: '_id name unit',
path: "itemType",
select: "_id name unit",
},
{
path: 'updateRecord',
path: "updateRecord",
populate: {
path: 'createdBy',
select: '_id firstName lastName',
path: "createdBy",
select: "_id firstName lastName",
},
},
{
path: 'purchaseRecord',
path: "purchaseRecord",
populate: {
path: 'requestedBy',
select: '_id firstName lastName',
path: "requestedBy",
select: "_id firstName lastName",
},
},
])
Expand Down Expand Up @@ -65,24 +65,25 @@ 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)
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));
Expand All @@ -96,19 +97,35 @@ 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.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);
}

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);
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 @@ -129,10 +146,11 @@ const bmMaterialsController = function (BuildingMaterial) {
quantityWasted,
},
},
},

}
)
.then((results) => { res.status(200).send(results); })
.then((results) => {
res.status(200).send(results);
})
.catch((error) => res.status(500).send({ message: error }));
}
};
Expand All @@ -146,16 +164,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 @@ -181,19 +208,23 @@ const bmMaterialsController = function (BuildingMaterial) {

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());
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.` });
res.status(200).send({
result: `Successfully posted log for ${results.length} Material records.`,
});
})
.catch((error) => res.status(500).send(error));
} catch (err) {
Expand Down
24 changes: 11 additions & 13 deletions src/controllers/dashBoardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const dashboardcontroller = function () {
return User.findOneAndUpdate(
{ _id: req.params.userId },
{ copiedAiPrompt: Date.now() },
{ new: true },
{ new: true }
)
.then((user) => {
if (user) {
Expand Down Expand Up @@ -51,7 +51,7 @@ const dashboardcontroller = function () {
...req.body,
aIPromptText: req.body.aIPromptText,
modifiedDatetime: Date.now(),
},
}
)
.then(() => {
res.status(200).send("Successfully saved AI prompt.");
Expand Down Expand Up @@ -90,7 +90,7 @@ const dashboardcontroller = function () {
const laborthismonth = dashboardhelper.laborthismonth(
userId,
req.params.fromDate,
req.params.toDate,
req.params.toDate
);
laborthismonth.then((results) => {
if (!results || results.length === 0) {
Expand All @@ -112,7 +112,7 @@ const dashboardcontroller = function () {
const laborthisweek = dashboardhelper.laborthisweek(
userId,
req.params.fromDate,
req.params.toDate,
req.params.toDate
);
laborthisweek.then((results) => {
res.status(200).send(results);
Expand Down Expand Up @@ -155,7 +155,7 @@ const dashboardcontroller = function () {
expected,
actual,
visual,
severity,
severity
) {
const text = `New Bug Report From <b>${firstName} ${lastName}</b>:
<p>[Feature Name] Bug Title:</p>
Expand Down Expand Up @@ -200,15 +200,15 @@ const dashboardcontroller = function () {
expected,
actual,
visual,
severity,
severity
);

try {
emailSender(
"[email protected]",
`Bug Rport from ${firstName} ${lastName}`,
emailBody,
email,
email
);
res.status(200).send("Success");
} catch {
Expand Down Expand Up @@ -263,15 +263,13 @@ const dashboardcontroller = function () {

// send suggestion email
const sendMakeSuggestion = async (req, res) => {
const {
suggestioncate, suggestion, confirm, email, ...rest
} = req.body;
const { suggestioncate, suggestion, confirm, email, ...rest } = req.body;
const emailBody = await getsuggestionEmailBody(
suggestioncate,
suggestion,
confirm,
rest,
email,
email
);
try {
emailSender(
Expand All @@ -281,7 +279,7 @@ const dashboardcontroller = function () {
null,
null,
email,
null,
null
);
res.status(200).send("Success");
} catch {
Expand Down Expand Up @@ -310,7 +308,7 @@ const dashboardcontroller = function () {
}
if (req.body.action === "delete") {
suggestionData.suggestion = suggestionData.suggestion.filter(
(item, index) => index + 1 !== +req.body.newField,
(item, index) => index + 1 !== +req.body.newField
);
}
} else {
Expand Down
Loading

0 comments on commit 5adcae8

Please sign in to comment.