diff --git a/src/controllers/bmdashboard/bmConsumableController.js b/src/controllers/bmdashboard/bmConsumableController.js new file mode 100644 index 000000000..23ea6e5cd --- /dev/null +++ b/src/controllers/bmdashboard/bmConsumableController.js @@ -0,0 +1,46 @@ +const bmConsumableController = function (BuildingConsumable) { + const fetchBMConsumables = async (req, res) => { + try { + BuildingConsumable + .find() + .populate([ + { + path: 'project', + select: '_id name', + }, + { + path: 'itemType', + select: '_id name unit', + }, + { + 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); + } + }; + + return { + fetchBMConsumables, + }; + }; + +module.exports = bmConsumableController; + \ No newline at end of file diff --git a/src/models/bmdashboard/buildingInventoryItem.js b/src/models/bmdashboard/buildingInventoryItem.js index 5e8b24916..dfaa8c73a 100644 --- a/src/models/bmdashboard/buildingInventoryItem.js +++ b/src/models/bmdashboard/buildingInventoryItem.js @@ -82,7 +82,7 @@ const largeItemBase = mongoose.model('largeItemBase', largeItemBaseSchema, 'buil // each document derived from this schema includes key field { __t: "material" } // ex: sand, stone, bricks, lumber, insulation -const buildingMaterial = smallItemBase.discriminator('material', new mongoose.Schema({ +const buildingMaterial = smallItemBase.discriminator('material_item', new mongoose.Schema({ stockUsed: { type: Number, default: 0 }, // stock that has been used up and cannot be reused stockWasted: { type: Number, default: 0 }, // ruined or destroyed stock })); @@ -95,7 +95,7 @@ const buildingMaterial = smallItemBase.discriminator('material', new mongoose.Sc // each document derived from this schema includes key field { __t: "consumable" } // ex: screws, nails, staples -const buildingConsumable = smallItemBase.discriminator('consumable', new mongoose.Schema({ +const buildingConsumable = smallItemBase.discriminator('consumable_item', new mongoose.Schema({ stockUsed: { type: Number, default: 0 }, // stock that has been used up and cannot be reused stockWasted: { type: Number, default: 0 }, // ruined or destroyed stock })); @@ -108,7 +108,7 @@ const buildingConsumable = smallItemBase.discriminator('consumable', new mongoos // each document derived from this schema includes key field { __t: "reusable" } // ex: hammers, screwdrivers, mallets, brushes, gloves -const buildingReusable = smallItemBase.discriminator('reusable', new mongoose.Schema({ +const buildingReusable = smallItemBase.discriminator('reusable_item', new mongoose.Schema({ stockDestroyed: { type: Number, default: 0 }, })); @@ -120,7 +120,7 @@ const buildingReusable = smallItemBase.discriminator('reusable', new mongoose.Sc // each document derived from this schema includes key field { __t: "tool" } // ex: power drills, wheelbarrows, shovels, jackhammers -const buildingTool = largeItemBase.discriminator('tool', new mongoose.Schema({ +const buildingTool = largeItemBase.discriminator('tool_item', new mongoose.Schema({ code: { type: Number, required: true }, // TODO: add function to create simple numeric code for on-site tool tracking })); @@ -134,7 +134,7 @@ const buildingTool = largeItemBase.discriminator('tool', new mongoose.Schema({ // items in this category are assumed to be rented // ex: tractors, excavators, bulldozers -const buildingEquipment = largeItemBase.discriminator('equipment', new mongoose.Schema({ +const buildingEquipment = largeItemBase.discriminator('equipment_item', new mongoose.Schema({ isTracked: { type: Boolean, required: true }, // has asset tracker assetTracker: { type: String, required: () => this.isTracked }, // required if isTracked = true (syntax?) })); diff --git a/src/models/bmdashboard/buildingInventoryType.js b/src/models/bmdashboard/buildingInventoryType.js index edd5b155c..bd125dfd3 100644 --- a/src/models/bmdashboard/buildingInventoryType.js +++ b/src/models/bmdashboard/buildingInventoryType.js @@ -24,7 +24,7 @@ const invTypeBase = mongoose.model('invTypeBase', invTypeBaseSchema, 'buildingIn // ex: sand, stone, brick, lumber -const materialType = invTypeBase.discriminator('material', new mongoose.Schema({ +const materialType = invTypeBase.discriminator('material_type', new mongoose.Schema({ category: { type: String, enum: ['Material'] }, unit: { type: String, required: true }, // unit of measurement })); @@ -35,7 +35,7 @@ const materialType = invTypeBase.discriminator('material', new mongoose.Schema({ // ex: screws, nails, staples -const consumableType = invTypeBase.discriminator('consumable', new mongoose.Schema({ +const consumableType = invTypeBase.discriminator('consumable_type', new mongoose.Schema({ category: { type: String, enum: ['Consumable'] }, size: { type: String, required: true }, })); @@ -46,7 +46,7 @@ const consumableType = invTypeBase.discriminator('consumable', new mongoose.Sche // ex: gloves, brushes, hammers, screwdrivers -const reusableType = invTypeBase.discriminator('reusable', new mongoose.Schema({ +const reusableType = invTypeBase.discriminator('reusable_type', new mongoose.Schema({ category: { type: String, enum: ['Reusable'] }, })); @@ -56,7 +56,7 @@ const reusableType = invTypeBase.discriminator('reusable', new mongoose.Schema({ // ex: shovels, wheelbarrows, power drills, jackhammers -const toolType = invTypeBase.discriminator('tool', new mongoose.Schema({ +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?) @@ -68,7 +68,7 @@ const toolType = invTypeBase.discriminator('tool', new mongoose.Schema({ // ex: tractors, excavators -const equipmentType = invTypeBase.discriminator('equipment', new mongoose.Schema({ +const equipmentType = invTypeBase.discriminator('equipment_type', new mongoose.Schema({ category: { type: String, enum: ['Equipment'] }, fuelType: { type: String, enum: ['Diesel', 'Biodiesel', 'Gasoline', 'Natural Gas', 'Ethanol'], required: true }, })); @@ -80,4 +80,4 @@ module.exports = { reusableType, toolType, equipmentType, -}; +}; \ No newline at end of file diff --git a/src/routes/bmdashboard/bmConsumablesRouter.js b/src/routes/bmdashboard/bmConsumablesRouter.js new file mode 100644 index 000000000..51126e3a3 --- /dev/null +++ b/src/routes/bmdashboard/bmConsumablesRouter.js @@ -0,0 +1,13 @@ +const express = require('express'); + +const routes = function (BuildingConsumable) { + const BuildingConsumableController = express.Router(); + const controller = require('../../controllers/bmdashboard/bmConsumableController')(BuildingConsumable); + + BuildingConsumableController.route('/consumables') + .get(controller.fetchBMConsumables); + + return BuildingConsumableController; +}; + +module.exports = routes; diff --git a/src/startup/routes.js b/src/startup/routes.js index ba2a608b6..1ac1d669b 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -33,6 +33,9 @@ const { toolType, equipmentType, } = require('../models/bmdashboard/buildingInventoryType'); +const { + buildingConsumable, +} = require('../models/bmdashboard/buildingInventoryItem'); const buildingTool = require('../models/bmdashboard/buildingTool'); const userProfileRouter = require('../routes/userProfileRouter')(userProfile); @@ -74,6 +77,7 @@ const mapLocationRouter = require('../routes/mapLocationsRouter')(mapLocations); const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial, buildingMaterial); const bmProjectRouter = require('../routes/bmdashboard/bmProjectRouter')(buildingProject); +const bmConsumablesRouter = require('../routes/bmdashboard/bmConsumablesRouter')(buildingConsumable); const bmInventoryTypeRouter = require('../routes/bmdashboard/bmInventoryTypeRouter')(invTypeBase, materialType, consumableType, reusableType, toolType, equipmentType); const bmToolRouter = require('../routes/bmdashboard/bmToolRouter')(buildingTool); @@ -113,4 +117,5 @@ module.exports = function (app) { app.use('/api/bm', bmProjectRouter); app.use('/api/bm', bmInventoryTypeRouter); app.use('/api/bm', bmToolRouter); -}; + app.use('/api/bm', bmConsumablesRouter); +}; \ No newline at end of file