From 165fc73cb11a963c8afa360a628e4a39a6428517 Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Fri, 29 Sep 2023 13:35:30 -0700 Subject: [PATCH 1/7] add material schema, update item type schema --- src/models/inventoryItemType.js | 21 ++++++++++++--- src/models/inventoryMaterial.js | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 src/models/inventoryMaterial.js diff --git a/src/models/inventoryItemType.js b/src/models/inventoryItemType.js index 80e5e0d7b..92139f27d 100644 --- a/src/models/inventoryItemType.js +++ b/src/models/inventoryItemType.js @@ -2,11 +2,24 @@ const mongoose = require('mongoose'); const { Schema } = mongoose; -const InventoryItemType = new Schema({ +// const InventoryItemType = new Schema({ +// name: { type: String, required: true }, +// description: { type: String }, +// imageUrl: { type: String }, +// quantifier: { type: String, default: 'each' }, +// }); + +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 }, - imageUrl: { type: String }, - quantifier: { type: String, default: 'each' }, + 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 }, // amount of item available to be checked out + projectsUsing: [String], // ids of projects using the item + imageUrl: { type: String } }); module.exports = mongoose.model('inventoryItemType', InventoryItemType, 'inventoryItemType'); + + diff --git a/src/models/inventoryMaterial.js b/src/models/inventoryMaterial.js new file mode 100644 index 000000000..31b6ce069 --- /dev/null +++ b/src/models/inventoryMaterial.js @@ -0,0 +1,46 @@ +const mongoose = require('mongoose') + +const {Schema} = mongoose + +const InventoryMaterial = new Schema({ + inventoryItemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'inventoryItemType', required: true }, + projectId: {type: Number, 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: String, required: true }, + taskId: { type: String, required: true }, + quantityUsed: { type: Number, required: true }, + responsibleUserId: { type: String, required: true } + }], + updateRecord: [{ // incident report affecting quantity/status of inventory item + date: { type: Date, required: true, default: Date.now() }, + createdBy: {type: String, required: true }, + action: { type: String, required: true }, // ex: Add, Reduce, Hold + cause: { type: String, required: true }, // ex: Used, Lost, Wasted, Transfer + quantity: { type: Number, required: true }, + description: { type: String, required: true, maxLength: 150 }, + responsibleUserId: { type: String, required: true }, + imageUrl: { type: String } + }], + purchaseRecord: [{ + date: { type: Date, required: true, default: Date.now() }, + createdBy: {type: String, required: true }, + invoiceId: { type: String, required: true }, + vendor: { type: String, required: true }, + brand: { type: String, required: true }, + amount: { type: Number, required: true }, // amount of item in each unit + quantity: { type: Number, required: true }, // number of units purchased + unitCost: { type: Number, required: true }, + tax: { type: Number, required: true }, + shipping: { type: Number, required: true }, + totalCost: { type: Number, required: true }, + imageUrl: { type: String }, + }] +}) + +module.exports = mongoose.model('inventoryMaterial', InventoryMaterial, 'inventoryMaterial'); From e2540140d889b31aede0656722eed32c5306d75e Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Mon, 2 Oct 2023 11:46:14 -0700 Subject: [PATCH 2/7] add materials route, router, controller --- .../bmdashboard/bmMaterialsController.js | 12 ++++++++++++ src/routes/bmdashboard/bmMaterialsRouter.js | 13 +++++++++++++ src/startup/routes.js | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 src/controllers/bmdashboard/bmMaterialsController.js create mode 100644 src/routes/bmdashboard/bmMaterialsRouter.js diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js new file mode 100644 index 000000000..11aa4a744 --- /dev/null +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -0,0 +1,12 @@ +const bmMaterialsController = function () { + const bmMaterialsList = async function _matsList(req, res) { + try { + return res.json({ message: "Hello World" }) + } catch (err) { + res.json(err); + } + }; + return { bmMaterialsList }; +}; + +module.exports = bmMaterialsController; \ No newline at end of file diff --git a/src/routes/bmdashboard/bmMaterialsRouter.js b/src/routes/bmdashboard/bmMaterialsRouter.js new file mode 100644 index 000000000..d8cca1aca --- /dev/null +++ b/src/routes/bmdashboard/bmMaterialsRouter.js @@ -0,0 +1,13 @@ +const express = require('express'); + +const routes = function () { +const materialsRouter = express.Router(); +const controller = require('../../controllers/bmdashboard/bmMaterialsController')(); + +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 43cd226bd..e0852dac3 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -55,6 +55,7 @@ const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverTe // bm dashboard const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); +const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(); module.exports = function (app) { @@ -88,4 +89,5 @@ module.exports = function (app) { app.use('/api', mouseoverTextRouter); // bm dashboard app.use('/api/bm', bmLoginRouter); + app.use('/api/bm', bmMaterialsRouter); }; From 9fdcdb8d4930421928d5deb4d825c7f5f9c6d213 Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Mon, 2 Oct 2023 18:09:21 -0700 Subject: [PATCH 3/7] update route, router, controller with updated material schema and new collection fetch --- .../bmdashboard/bmMaterialsController.js | 8 +++- ...ryMaterial.js => inventoryItemMaterial.js} | 38 ++++++++----------- src/routes/bmdashboard/bmMaterialsRouter.js | 4 +- src/startup/routes.js | 4 +- 4 files changed, 25 insertions(+), 29 deletions(-) rename src/models/{inventoryMaterial.js => inventoryItemMaterial.js} (51%) diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index 11aa4a744..797d5730d 100644 --- a/src/controllers/bmdashboard/bmMaterialsController.js +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -1,7 +1,11 @@ -const bmMaterialsController = function () { +const mongoose = require('mongoose') + +const bmMaterialsController = function (ItemMaterial, ItemType) { const bmMaterialsList = async function _matsList(req, res) { try { - return res.json({ message: "Hello World" }) + ItemMaterial.find() + .then(results => res.status(200).send(results)) + .catch(error => res.status(500).send(error)) } catch (err) { res.json(err); } diff --git a/src/models/inventoryMaterial.js b/src/models/inventoryItemMaterial.js similarity index 51% rename from src/models/inventoryMaterial.js rename to src/models/inventoryItemMaterial.js index 31b6ce069..b795d2f63 100644 --- a/src/models/inventoryMaterial.js +++ b/src/models/inventoryItemMaterial.js @@ -1,10 +1,10 @@ -const mongoose = require('mongoose') +const mongoose = require('mongoose'); -const {Schema} = mongoose +const { Schema } = mongoose; -const InventoryMaterial = new Schema({ +const InventoryItemMaterial = new Schema({ inventoryItemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'inventoryItemType', required: true }, - projectId: {type: Number, 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 }, @@ -12,35 +12,27 @@ const InventoryMaterial = new Schema({ 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: String, required: true }, - taskId: { type: String, required: true }, + createdBy: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile', required: true }, quantityUsed: { type: Number, required: true }, - responsibleUserId: { type: String, required: true } }], updateRecord: [{ // incident report affecting quantity/status of inventory item date: { type: Date, required: true, default: Date.now() }, - createdBy: {type: String, required: true }, - action: { type: String, required: true }, // ex: Add, Reduce, Hold - cause: { type: String, required: true }, // ex: Used, Lost, Wasted, Transfer - quantity: { type: Number, required: true }, + 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 }, - responsibleUserId: { type: String, required: true }, - imageUrl: { type: String } }], purchaseRecord: [{ date: { type: Date, required: true, default: Date.now() }, - createdBy: {type: String, required: true }, - invoiceId: { type: String, required: true }, - vendor: { type: String, required: true }, - brand: { type: String, required: true }, - amount: { type: Number, required: true }, // amount of item in each unit - quantity: { type: Number, required: true }, // number of units purchased - unitCost: { type: Number, required: true }, + 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 + subtotal: { type: Number, required: true }, tax: { type: Number, required: true }, shipping: { type: Number, required: true }, - totalCost: { type: Number, required: true }, - imageUrl: { type: String }, }] }) -module.exports = mongoose.model('inventoryMaterial', InventoryMaterial, 'inventoryMaterial'); +module.exports = mongoose.model('inventoryItemMaterial', InventoryItemMaterial, 'inventoryMaterial'); diff --git a/src/routes/bmdashboard/bmMaterialsRouter.js b/src/routes/bmdashboard/bmMaterialsRouter.js index d8cca1aca..d3e4518da 100644 --- a/src/routes/bmdashboard/bmMaterialsRouter.js +++ b/src/routes/bmdashboard/bmMaterialsRouter.js @@ -1,8 +1,8 @@ const express = require('express'); -const routes = function () { +const routes = function (itemMaterial, itemType) { const materialsRouter = express.Router(); -const controller = require('../../controllers/bmdashboard/bmMaterialsController')(); +const controller = require('../../controllers/bmdashboard/bmMaterialsController')(itemMaterial, itemType); materialsRouter.route('/materials') .get(controller.bmMaterialsList); diff --git a/src/startup/routes.js b/src/startup/routes.js index e0852dac3..cde07c94d 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); @@ -55,8 +56,7 @@ const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverTe // bm dashboard const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); -const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(); - +const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial, inventoryItemType); module.exports = function (app) { app.use('/api', forgotPwdRouter); From 9da89884167f825caf399c09ae44d0bb6fe35079 Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Mon, 2 Oct 2023 20:12:37 -0700 Subject: [PATCH 4/7] update inventoryitemtype schema --- src/models/inventoryItemType.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/models/inventoryItemType.js b/src/models/inventoryItemType.js index 92139f27d..207ac77b0 100644 --- a/src/models/inventoryItemType.js +++ b/src/models/inventoryItemType.js @@ -2,21 +2,14 @@ const mongoose = require('mongoose'); const { Schema } = mongoose; -// const InventoryItemType = new Schema({ -// name: { type: String, required: true }, -// description: { type: String }, -// imageUrl: { type: String }, -// quantifier: { type: String, default: 'each' }, -// }); - 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, 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 }, // amount of item available to be checked out - projectsUsing: [String], // ids of projects using the item + totalAvailable: { type: Number, required: true }, + projectsUsing: [ {type: mongoose.SchemaTypes.ObjectId, ref: 'project'} ], imageUrl: { type: String } }); From d4e04bd6c970ec6c27a4b21d48ba64b1b3c8fdfc Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Wed, 4 Oct 2023 16:31:43 -0700 Subject: [PATCH 5/7] populate db fetch with project, inventoryItemType objects --- src/controllers/bmdashboard/bmMaterialsController.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index 797d5730d..b55b70b7e 100644 --- a/src/controllers/bmdashboard/bmMaterialsController.js +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -4,6 +4,14 @@ const bmMaterialsController = function (ItemMaterial, ItemType) { const bmMaterialsList = async function _matsList(req, res) { try { ItemMaterial.find() + .populate({ + path: 'project', + select: '_id projectName' + }) + .populate({ + path: 'inventoryItemType', + select: '_id name uom totalStock' + }) .then(results => res.status(200).send(results)) .catch(error => res.status(500).send(error)) } catch (err) { From 975a858079086a9e3e88249ca0e3f49277d620ae Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Thu, 5 Oct 2023 11:55:09 -0700 Subject: [PATCH 6/7] update populate method --- .../bmdashboard/bmMaterialsController.js | 42 +++++++++++++++---- src/routes/bmdashboard/bmMaterialsRouter.js | 4 +- src/startup/routes.js | 2 +- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index b55b70b7e..a31ed460e 100644 --- a/src/controllers/bmdashboard/bmMaterialsController.js +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -1,17 +1,41 @@ const mongoose = require('mongoose') -const bmMaterialsController = function (ItemMaterial, ItemType) { +const bmMaterialsController = function (ItemMaterial) { const bmMaterialsList = async function _matsList(req, res) { try { ItemMaterial.find() - .populate({ - path: 'project', - select: '_id projectName' - }) - .populate({ - path: 'inventoryItemType', - select: '_id name uom totalStock' - }) + .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) { diff --git a/src/routes/bmdashboard/bmMaterialsRouter.js b/src/routes/bmdashboard/bmMaterialsRouter.js index d3e4518da..ab8a67388 100644 --- a/src/routes/bmdashboard/bmMaterialsRouter.js +++ b/src/routes/bmdashboard/bmMaterialsRouter.js @@ -1,8 +1,8 @@ const express = require('express'); -const routes = function (itemMaterial, itemType) { +const routes = function (itemMaterial) { const materialsRouter = express.Router(); -const controller = require('../../controllers/bmdashboard/bmMaterialsController')(itemMaterial, itemType); +const controller = require('../../controllers/bmdashboard/bmMaterialsController')(itemMaterial); materialsRouter.route('/materials') .get(controller.bmMaterialsList); diff --git a/src/startup/routes.js b/src/startup/routes.js index cde07c94d..bbd22b530 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -56,7 +56,7 @@ const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverTe // bm dashboard const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); -const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial, inventoryItemType); +const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial); module.exports = function (app) { app.use('/api', forgotPwdRouter); From 61663e93b974ecb87ecea6daab9ff6fa200f011b Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Mon, 9 Oct 2023 14:09:42 -0700 Subject: [PATCH 7/7] update material, itemType schema --- src/models/inventoryItemMaterial.js | 2 ++ src/models/inventoryItemType.js | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/models/inventoryItemMaterial.js b/src/models/inventoryItemMaterial.js index b795d2f63..e6153af45 100644 --- a/src/models/inventoryItemMaterial.js +++ b/src/models/inventoryItemMaterial.js @@ -29,6 +29,8 @@ const InventoryItemMaterial = new Schema({ 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 }, diff --git a/src/models/inventoryItemType.js b/src/models/inventoryItemType.js index 207ac77b0..321038a84 100644 --- a/src/models/inventoryItemType.js +++ b/src/models/inventoryItemType.js @@ -10,7 +10,8 @@ const InventoryItemType = new Schema({ // creates an item, tracks total amount i 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 } + imageUrl: { type: String }, + link: { type: String} }); module.exports = mongoose.model('inventoryItemType', InventoryItemType, 'inventoryItemType');