diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js new file mode 100644 index 000000000..a31ed460e --- /dev/null +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -0,0 +1,48 @@ +const mongoose = require('mongoose') + +const bmMaterialsController = function (ItemMaterial) { + const bmMaterialsList = async function _matsList(req, res) { + try { + ItemMaterial.find() + .populate([ + { + path: 'project', + select: '_id projectName' + }, + { + path: 'inventoryItemType', + select: '_id name uom totalStock totalAvailable' + }, + { + path: 'usageRecord', + populate: { + path: 'createdBy', + select: '_id firstName lastName' + } + }, + { + path: 'updateRecord', + populate: { + path: 'createdBy', + select: '_id firstName lastName' + } + }, + { + path: 'purchaseRecord', + populate: { + path: 'createdBy', + select: '_id firstName lastName' + } + } + ]) + .exec() + .then(results => res.status(200).send(results)) + .catch(error => res.status(500).send(error)) + } catch (err) { + res.json(err); + } + }; + return { bmMaterialsList }; +}; + +module.exports = bmMaterialsController; \ No newline at end of file diff --git a/src/models/inventoryItemMaterial.js b/src/models/inventoryItemMaterial.js new file mode 100644 index 000000000..e6153af45 --- /dev/null +++ b/src/models/inventoryItemMaterial.js @@ -0,0 +1,40 @@ +const mongoose = require('mongoose'); + +const { Schema } = mongoose; + +const InventoryItemMaterial = new Schema({ + inventoryItemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'inventoryItemType', required: true }, + project: { type: mongoose.SchemaTypes.ObjectId, ref: 'project', required: true }, + stockBought: { type: Number, required: true }, // amount bought for project, affects total stock + stockUsed: { type: Number, required: true }, + stockAvailable: { type: Number, required: true }, + stockHeld: { type: Number, required: true }, + stockWasted: { type: Number, required: true }, + usageRecord: [{ // daily log of amount inventory item used at job site + date: { type: Date, required: true, default: Date.now() }, + createdBy: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile', required: true }, + quantityUsed: { type: Number, required: true }, + }], + updateRecord: [{ // incident report affecting quantity/status of inventory item + date: { type: Date, required: true, default: Date.now() }, + createdBy: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile', required: true }, + action: { type: String, required: true }, // ex: Add, Reduce, Hold (updates stock quantities) + cause: { type: String, required: true }, // ex: Used, Lost, Wasted, Transfer (reason for update) + quantity: { type: Number, required: true }, // amount of material affected + description: { type: String, required: true, maxLength: 150 }, + }], + purchaseRecord: [{ + date: { type: Date, required: true, default: Date.now() }, + createdBy: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile', required: true }, + poId: { type: String, required: true }, + sellerId: { type: String, required: true }, + quantity: { type: Number, required: true }, // adds to stockBought + unitPrice: {type: Number, required: true}, + currency: { type: String, required: true }, + subtotal: { type: Number, required: true }, + tax: { type: Number, required: true }, + shipping: { type: Number, required: true }, + }] +}) + +module.exports = mongoose.model('inventoryItemMaterial', InventoryItemMaterial, 'inventoryMaterial'); diff --git a/src/models/inventoryItemType.js b/src/models/inventoryItemType.js index 80e5e0d7b..321038a84 100644 --- a/src/models/inventoryItemType.js +++ b/src/models/inventoryItemType.js @@ -2,11 +2,18 @@ const mongoose = require('mongoose'); const { Schema } = mongoose; -const InventoryItemType = new Schema({ +const InventoryItemType = new Schema({ // creates an item, tracks total amount in organization's stock + type: { type: String, required: true }, // ie Material, Equipment, Tool name: { type: String, required: true }, - description: { type: String }, + description: { type: String, required: true, maxLength: 150 }, + uom: { type: String, required: true }, // unit of measurement + totalStock: { type: Number, required: true }, // total amount of all stock acquired + totalAvailable: { type: Number, required: true }, + projectsUsing: [ {type: mongoose.SchemaTypes.ObjectId, ref: 'project'} ], imageUrl: { type: String }, - quantifier: { type: String, default: 'each' }, + link: { type: String} }); module.exports = mongoose.model('inventoryItemType', InventoryItemType, 'inventoryItemType'); + + diff --git a/src/routes/bmdashboard/bmMaterialsRouter.js b/src/routes/bmdashboard/bmMaterialsRouter.js new file mode 100644 index 000000000..ab8a67388 --- /dev/null +++ b/src/routes/bmdashboard/bmMaterialsRouter.js @@ -0,0 +1,13 @@ +const express = require('express'); + +const routes = function (itemMaterial) { +const materialsRouter = express.Router(); +const controller = require('../../controllers/bmdashboard/bmMaterialsController')(itemMaterial); + +materialsRouter.route('/materials') + .get(controller.bmMaterialsList); + + return materialsRouter; +} + +module.exports = routes; \ No newline at end of file diff --git a/src/startup/routes.js b/src/startup/routes.js index 296b10b69..a8d16ae6d 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -20,6 +20,7 @@ const ownerStandardMessage = require('../models/ownerStandardMessage'); const profileInitialSetuptoken = require('../models/profileInitialSetupToken'); const reason = require('../models/reason'); const mouseoverText = require('../models/mouseoverText'); +const inventoryItemMaterial = require('../models/inventoryItemMaterial'); const userProfileRouter = require('../routes/userProfileRouter')(userProfile); const badgeRouter = require('../routes/badgeRouter')(badge); @@ -57,7 +58,7 @@ const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverTe // bm dashboard const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); - +const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial); module.exports = function (app) { app.use('/api', forgotPwdRouter); @@ -91,4 +92,5 @@ module.exports = function (app) { app.use('/api', isEmailExistsRouter); // bm dashboard app.use('/api/bm', bmLoginRouter); + app.use('/api/bm', bmMaterialsRouter); };