Skip to content

Commit

Permalink
Merge pull request #970 from OneCommunityGlobal/Shereen_Add_Tool
Browse files Browse the repository at this point in the history
Shereen_Created Routing and controller function for Add Tool
  • Loading branch information
one-community authored Jul 7, 2024
2 parents ce96b31 + 4279ea7 commit 931c78b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 11 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 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 @@ -334,6 +403,7 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp
updateNameAndUnit,
addMaterialType,
addConsumableType,
addToolType,
fetchInvUnitsFromJson,
fetchInventoryByType,
};
Expand Down
25 changes: 17 additions & 8 deletions src/models/bmdashboard/buildingInventoryType.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,23 @@ const reusableType = invTypeBase.discriminator('reusable_type', new mongoose.Sch

const toolType = invTypeBase.discriminator('tool_type', new mongoose.Schema({
category: { type: String, enum: ['Tool'] },
isPowered: { type: Boolean, required: true },
powerSource: { type: String, required: function() {
return this.isPowered; // required if isPowered = true
},
},
available: [{type: mongoose.SchemaTypes.ObjectId, ref: 'tool_item'}],
using: [{ type: mongoose.SchemaTypes.ObjectId, ref: 'tool_item' }],
//add a date last updated field?
invoice: String,
purchaseRental: String,
fromDate: Date,
toDate:Date,
condition: String,
phoneNumber: String,
quantity: Number,
currency: String,
unitPrice: Number,
shippingFee: Number,
taxes: Number,
totalPriceWithShipping: Number,
images: String,
link: String,

// isPowered: { type: Boolean, required: true },
// powerSource: { type: String, required: () => this.isPowered }, // required if isPowered = true (syntax?)
}));

//---------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/routes/bmdashboard/bmInventoryTypeRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const routes = function (baseInvType, matType, consType, reusType, toolType, equ

inventoryTypeRouter.route('/consumables').post(controller.addConsumableType);

inventoryTypeRouter.route('/tools').post(controller.addToolType);

inventoryTypeRouter.route('/invtypes/tools').get(controller.fetchToolTypes);

inventoryTypeRouter.route('/invtypes/equipment').post(controller.addEquipmentType);
Expand Down

0 comments on commit 931c78b

Please sign in to comment.