Skip to content

Commit

Permalink
Merge pull request #1023 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.84]
  • Loading branch information
one-community authored Jul 9, 2024
2 parents ffd9ecb + 6dfab38 commit 46981d7
Show file tree
Hide file tree
Showing 18 changed files with 3,516 additions and 3,139 deletions.
5,722 changes: 2,788 additions & 2,934 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"eslint-import-resolver-babel-module": "^5.3.1",
"eslint-plugin-import": "^2.28.0",
"husky": "^8.0.1",
"jest": "^26.6.0",
"jest": "^29.7.0",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-react": "^7.33.1",
"eslint-plugin-react-hooks": "^4.6.0",
Expand Down Expand Up @@ -69,7 +69,7 @@
"moment": "^2.29.4",
"moment-timezone": "^0.5.35",
"mongodb": "^3.7.3",
"mongoose": "^5.13.15",
"mongoose": "^5.13.20",
"mongoose-validator": "^2.1.0",
"node-cache": "^5.1.2",
"node-datetime": "^2.0.3",
Expand All @@ -78,7 +78,7 @@
"sanitize-html": "^2.13.0",
"supertest": "^6.3.4",
"uuid": "^3.4.0",
"ws": "^8.8.1"
"ws": "^8.17.1"
},
"nodemonConfig": {
"watch": [
Expand Down
40 changes: 40 additions & 0 deletions src/controllers/bmdashboard/bmEquipmentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,45 @@ const bmEquipmentController = (BuildingEquipment) => {
}
};

const fetchBMEquipments = async (req, res) => {
try {
BuildingEquipment
.find()
.populate([
{
path: 'project',
select: '_id name',
},
{
path: 'itemType',
select: '_id name',
},
{
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);
}
};


const bmPurchaseEquipments = async function (req, res) {
const {
projectId,
Expand Down Expand Up @@ -103,6 +142,7 @@ const bmEquipmentController = (BuildingEquipment) => {
return {
fetchSingleEquipment,
bmPurchaseEquipments,
fetchBMEquipments,
};
};

Expand Down
83 changes: 83 additions & 0 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,75 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
}
}

async function addToolType(req, res) {
const {
name,
description,
invoice,
purchaseRental,
fromDate,
toDate,
condition,
phoneNumber,
quantity,
currency,
unitPrice,
shippingFee,
taxes,
totalPriceWithShipping,
images,
link,
requestor: { requestorId },
} = req.body;

try {
ToolType.find({ name })
.then((result) => {
if (result.length) {
res.status(409).send('Oops!! Tool already exists!');
} else {
const newDoc = {
category: 'Tool',
name,
description,
invoice,
purchaseRental,
fromDate,
toDate,
condition,
phoneNumber,
quantity,
currency,
unitPrice,
shippingFee,
taxes,
totalPriceWithShipping,
images,
link,
createdBy: requestorId,
};
ToolType.create(newDoc)
.then((results) => {
res.status(201).send(results);
})
.catch((error) => {
if (error._message.includes('validation failed')) {
res.status(400).send(error.errors.unit.message);
} else {
res.status(500).send(error);
}
});
}
})
.catch((error) => {
res.status(500).send(error);
});
} catch (error) {
res.status(500).send(error);
}
}


async function fetchInventoryByType(req, res) {
const { type } = req.params;
let SelectedType = InvType;
Expand Down Expand Up @@ -272,6 +341,18 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
res.status(500).send(error);
}
}

async function fetchEquipmentTypes(req, res) {
try {
EquipType.find()
.exec()
.then((result) => res.status(200).send(result))
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
}

const fetchSingleInventoryType = async (req, res) => {
const { invtypeId } = req.params;
try {
Expand Down Expand Up @@ -317,10 +398,12 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
fetchReusableTypes,
fetchToolTypes,
addEquipmentType,
fetchEquipmentTypes,
fetchSingleInventoryType,
updateNameAndUnit,
addMaterialType,
addConsumableType,
addToolType,
fetchInvUnitsFromJson,
fetchInventoryByType,
};
Expand Down
153 changes: 149 additions & 4 deletions src/controllers/bmdashboard/bmReusableController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const {
reusableType: ReusableType,
} = require('../../models/bmdashboard/buildingInventoryType');

function isValidDate(dateString) {
const date = new Date(dateString);
return !isNaN(date.getTime());
}
// function isValidDate(dateString) {
// const date = new Date(dateString);
// return !isNaN(date.getTime());
// }

const bmReusableController = function (BuildingReusable) {
const fetchBMReusables = async (req, res) => {
Expand Down Expand Up @@ -103,9 +103,154 @@ const bmReusableController = function (BuildingReusable) {
}
};

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

if (
quantityUsed > reusable.stockAvailable ||
quantityWasted > reusable.stockAvailable ||
quantityUsed + quantityWasted > reusable.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 = +reusable.stockUsed + parseFloat(quantityUsed);
let newStockWasted = +reusable.stockWasted + parseFloat(quantityWasted);
let newAvailable =
+reusable.stockAvailable -
parseFloat(quantityUsed) -
parseFloat(quantityWasted);
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
BuildingReusable.updateOne(
{ _id: req.body.reusable._id },

{
$set: {
stockUsed: newStockUsed,
stockWasted: newStockWasted,
stockAvailable: newAvailable,
},
$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 }));
}
};

const bmPostReusableUpdateBulk = function (req, res) {
const reusableUpdates = req.body.upadateReusables;
let errorFlag = false;
const updateRecordsToBeAdded = [];
for (let i = 0; i < reusableUpdates.length; i+=1) {
const payload = reusableUpdates[i];
let quantityUsed = +payload.quantityUsed;
let quantityWasted = +payload.quantityWasted;
const { reusable } = payload;
if (payload.QtyUsedLogUnit === "percent" && quantityWasted >= 0) {
quantityUsed = +(
(+quantityUsed / 100) *
reusable.stockAvailable
).toFixed(4);
}
if (payload.QtyWastedLogUnit === "percent" && quantityUsed >= 0) {
quantityWasted = +(
(+quantityWasted / 100) *
reusable.stockAvailable
).toFixed(4);
}

let newStockUsed = +reusable.stockUsed + parseFloat(quantityUsed);
let newStockWasted = +reusable.stockWasted + parseFloat(quantityWasted);
let newAvailable =
+reusable.stockAvailable -
parseFloat(quantityUsed) -
parseFloat(quantityWasted);
newStockUsed = parseFloat(newStockUsed.toFixed(4));
newStockWasted = parseFloat(newStockWasted.toFixed(4));
newAvailable = parseFloat(newAvailable.toFixed(4));
if (newAvailable < 0) {
errorFlag = true;
break;
}
updateRecordsToBeAdded.push({
updateId: reusable._id,
set: {
stockUsed: newStockUsed,
stockWasted: newStockWasted,
stockAvailable: newAvailable,
},
updateValue: {
createdBy: req.body.requestor.requestorId,
quantityUsed,
quantityWasted,
date: req.body.date,
},
});
}

try {
if (errorFlag) {
res.status(500).send("Stock quantities submitted seems to be invalid");
return;
}
const updatePromises = updateRecordsToBeAdded.map((updateItem) =>
BuildingReusable.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} Reusable records.`,
});
})
.catch((error) => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};




return {
fetchBMReusables,
purchaseReusable,
bmPostReusableUpdateRecord,
bmPostReusableUpdateBulk,
};
};

Expand Down
Loading

0 comments on commit 46981d7

Please sign in to comment.