From f7203751905e7dd6b62d0132fa13abe2e67a0d1a Mon Sep 17 00:00:00 2001 From: Shereen Punnassery Date: Thu, 30 May 2024 21:58:58 -0700 Subject: [PATCH] Created Routing and controller function for Add Tool Created Routing and controller function for Add Tool request. --- .../bmdashboard/bmInventoryTypeController.js | 70 +++++++++++++++++++ .../bmdashboard/buildingInventoryType.js | 19 ++++- .../bmdashboard/bmInventoryTypeRouter.js | 2 + 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/controllers/bmdashboard/bmInventoryTypeController.js b/src/controllers/bmdashboard/bmInventoryTypeController.js index f4cd6cf99..c7aaa6bdd 100644 --- a/src/controllers/bmdashboard/bmInventoryTypeController.js +++ b/src/controllers/bmdashboard/bmInventoryTypeController.js @@ -174,6 +174,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; @@ -294,6 +363,7 @@ function bmInventoryTypeController(InvType, MatType, ConsType, ReusType, ToolTyp updateNameAndUnit, addMaterialType, addConsumableType, + addToolType, fetchInvUnitsFromJson, fetchInventoryByType, }; diff --git a/src/models/bmdashboard/buildingInventoryType.js b/src/models/bmdashboard/buildingInventoryType.js index 9173bf5cc..9bb98f915 100644 --- a/src/models/bmdashboard/buildingInventoryType.js +++ b/src/models/bmdashboard/buildingInventoryType.js @@ -59,8 +59,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: () => this.isPowered }, // required if isPowered = true (syntax?) + 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?) })); //--------------------------- diff --git a/src/routes/bmdashboard/bmInventoryTypeRouter.js b/src/routes/bmdashboard/bmInventoryTypeRouter.js index 3d940ac61..57061b12c 100644 --- a/src/routes/bmdashboard/bmInventoryTypeRouter.js +++ b/src/routes/bmdashboard/bmInventoryTypeRouter.js @@ -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);