Skip to content

Commit

Permalink
Merge pull request #978 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.76]
  • Loading branch information
one-community authored Jun 6, 2024
2 parents fd6c955 + 46b11be commit 88b804f
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 150 deletions.
33 changes: 30 additions & 3 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,39 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
}

const fetchToolTypes = async (req, res) => {

try {
ToolType.find()
ToolType
.find()
.populate([
{
path: 'available',
select: '_id code project',
populate: {
path: 'project',
select: '_id name'
}
},
{
path: 'using',
select: '_id code project',
populate: {
path: 'project',
select: '_id name'
}
}
])
.exec()
.then((result) => res.status(200).send(result))
.catch((error) => res.status(500).send(error));
.then(result => {
res.status(200).send(result);
})
.catch(error => {
console.error("fetchToolTypes error: ", error);
res.status(500).send(error);
});

} catch (err) {
console.log("error: ", err)
res.json(err);
}
};
Expand Down
85 changes: 84 additions & 1 deletion src/controllers/bmdashboard/bmToolController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');

const bmToolController = (BuildingTool) => {
const bmToolController = (BuildingTool, ToolType) => {

const fetchAllTools = (req, res) => {
const populateFields = [
Expand Down Expand Up @@ -156,10 +156,93 @@ const bmToolController = (BuildingTool) => {
}
};

const bmLogTools = async function (req, res) {
const requestor = req.body.requestor.requestorId;
const {typesArray, action, date} = req.body
const results = [];
const errors = [];

if(typesArray.length === 0 || typesArray === undefined){
errors.push({ message: 'Invalid request. No tools selected'})
return res.status(500).send({errors, results});
}

for (const type of typesArray) {
const toolName = type.toolName;
const toolCodes = type.toolCodes;
const codeMap = {};
toolCodes.forEach(obj => {
codeMap[obj.value] = obj.label;
})

try{
const toolTypeDoc = await ToolType.findOne({ _id: mongoose.Types.ObjectId(type.toolType) });
if(!toolTypeDoc) {
errors.push({ message: `Tool type ${toolName} with id ${type.toolType} was not found.`});
continue;
}
const availableItems = toolTypeDoc.available;
const usingItems = toolTypeDoc.using;

for(const toolItem of type.toolItems){
const buildingToolDoc = await BuildingTool.findOne({ _id: mongoose.Types.ObjectId(toolItem)});
if(!buildingToolDoc){
errors.push({ message: `${toolName} with id ${toolItem} was not found.`});
continue;
}

if(action === "Check Out" && availableItems.length > 0){
const foundIndex = availableItems.indexOf(toolItem);
if(foundIndex >= 0){
availableItems.splice(foundIndex, 1);
usingItems.push(toolItem);
}else{
errors.push({ message: `${toolName} with code ${codeMap[toolItem]} is not available for ${action}`});
continue;
}
}

if(action === "Check In" && usingItems.length > 0){
const foundIndex = usingItems.indexOf(toolItem);
if(foundIndex >= 0){
usingItems.splice(foundIndex, 1);
availableItems.push(toolItem);
}else{
errors.push({ message: `${toolName} ${codeMap[toolItem]} is not available for ${action}`});
continue;
}
}

const newRecord = {
date: date,
createdBy: requestor,
responsibleUser: buildingToolDoc.userResponsible,
type: action
}

buildingToolDoc.logRecord.push(newRecord);
buildingToolDoc.save();
results.push({message: `${action} successful for ${toolName} ${codeMap[toolItem]}`})
}

await toolTypeDoc.save();
}catch(error){
errors.push({message: `Error for tool type ${type}: ${error.message}` });
}
}

if (errors.length > 0) {
return res.status(404).send({ errors, results });
} else {
return res.status(200).send({ errors, results });
}
}

return {
fetchAllTools,
fetchSingleTool,
bmPurchaseTools,
bmLogTools
};
};

Expand Down
Loading

0 comments on commit 88b804f

Please sign in to comment.