From f08d2d5066115bfd10ef6d3d5781d7e464438099 Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Sun, 7 Jan 2024 12:18:39 -0800 Subject: [PATCH 1/5] update mats route, controller to use updated model. isolate populate method causing fetch err. --- src/controllers/bmdashboard/bmMaterialsController.js | 10 +++++----- src/routes/bmdashboard/bmMaterialsRouter.js | 4 ++-- src/startup/routes.js | 7 ++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index da45aad1d..0191b56fc 100644 --- a/src/controllers/bmdashboard/bmMaterialsController.js +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -1,6 +1,6 @@ const mongoose = require('mongoose'); -const bmMaterialsController = function (ItemMaterial, BuildingMaterial) { +const bmMaterialsController = function (BuildingMaterial) { const bmMaterialsList = async function _matsList(req, res) { try { BuildingMaterial.find() @@ -9,10 +9,10 @@ const bmMaterialsController = function (ItemMaterial, BuildingMaterial) { path: 'project', select: '_id name', }, - { - path: 'itemType', - select: '_id name unit', - }, + // { + // path: 'itemType', + // select: '_id name unit', + // }, { path: 'updateRecord', populate: { diff --git a/src/routes/bmdashboard/bmMaterialsRouter.js b/src/routes/bmdashboard/bmMaterialsRouter.js index 9a520d7d6..733148d14 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, buildingMaterial) { +const routes = function (buildingMaterial) { const materialsRouter = express.Router(); - const controller = require('../../controllers/bmdashboard/bmMaterialsController')(itemMaterial, buildingMaterial); + const controller = require('../../controllers/bmdashboard/bmMaterialsController')(buildingMaterial); materialsRouter.route('/materials') .get(controller.bmMaterialsList) .post(controller.bmPurchaseMaterials); diff --git a/src/startup/routes.js b/src/startup/routes.js index 1ac1d669b..492e455cd 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -21,10 +21,10 @@ const weeklySummaryAIPrompt = require('../models/weeklySummaryAIPrompt'); const profileInitialSetuptoken = require('../models/profileInitialSetupToken'); const reason = require('../models/reason'); const mouseoverText = require('../models/mouseoverText'); -const inventoryItemMaterial = require('../models/inventoryItemMaterial'); +// const inventoryItemMaterial = require('../models/inventoryItemMaterial'); const mapLocations = require('../models/mapLocation'); const buildingProject = require('../models/bmdashboard/buildingProject'); -const buildingMaterial = require('../models/bmdashboard/buildingMaterial'); +// const buildingMaterial = require('../models/bmdashboard/buildingMaterial'); const { invTypeBase, materialType, @@ -35,6 +35,7 @@ const { } = require('../models/bmdashboard/buildingInventoryType'); const { buildingConsumable, + buildingMaterial, } = require('../models/bmdashboard/buildingInventoryItem'); const buildingTool = require('../models/bmdashboard/buildingTool'); @@ -75,7 +76,7 @@ const mapLocationRouter = require('../routes/mapLocationsRouter')(mapLocations); // bm dashboard const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); -const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial, buildingMaterial); +const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(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); From 273fa005f4cf6ebd3f1d60b734858cba39fa436b Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Sun, 7 Jan 2024 12:52:01 -0800 Subject: [PATCH 2/5] update populate ref string --- src/controllers/bmdashboard/bmMaterialsController.js | 8 ++++---- src/models/bmdashboard/buildingInventoryItem.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index 0191b56fc..51295d035 100644 --- a/src/controllers/bmdashboard/bmMaterialsController.js +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -9,10 +9,10 @@ const bmMaterialsController = function (BuildingMaterial) { path: 'project', select: '_id name', }, - // { - // path: 'itemType', - // select: '_id name unit', - // }, + { + path: 'itemType', + select: '_id name unit', + }, { path: 'updateRecord', populate: { diff --git a/src/models/bmdashboard/buildingInventoryItem.js b/src/models/bmdashboard/buildingInventoryItem.js index dfaa8c73a..c44f37211 100644 --- a/src/models/bmdashboard/buildingInventoryItem.js +++ b/src/models/bmdashboard/buildingInventoryItem.js @@ -11,7 +11,7 @@ const mongoose = require('mongoose'); // documents stored in 'buildingInventoryItems' collection const smallItemBaseSchema = mongoose.Schema({ - itemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'buildingInventoryType' }, + itemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'invTypeBase' }, project: { type: mongoose.SchemaTypes.ObjectId, ref: 'buildingProject' }, stockBought: { type: Number, default: 0 }, // total amount of item bought for use in the project // TODO: can stockAvailable default be a function? From 66d3814f11d40fc94c0b028556cde2d1b7e2aaf4 Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Sun, 7 Jan 2024 13:27:58 -0800 Subject: [PATCH 3/5] update brand to brandPref --- .../bmdashboard/bmMaterialsController.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index 51295d035..207e2428a 100644 --- a/src/controllers/bmdashboard/bmMaterialsController.js +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -42,15 +42,9 @@ const bmMaterialsController = function (BuildingMaterial) { matTypeId, quantity, priority, - brand, + brand: brandPref, requestor: { requestorId }, } = req.body; - const newPurchaseRecord = { - quantity, - priority, - brand, - requestedBy: requestorId, - }; try { // check if requestor has permission to make purchase request //! Note: this code is disabled until permissions are added @@ -64,6 +58,12 @@ const bmMaterialsController = function (BuildingMaterial) { // check if the material is already being used in the project // if no, add a new document to the collection // if yes, update the existing document + const newPurchaseRecord = { + quantity, + priority, + brandPref, + requestedBy: requestorId, + }; const doc = await BuildingMaterial.findOne({ project: projectId, itemType: matTypeId }); if (!doc) { const newDoc = { From 7b4f5431ff0fdf48b097fb9fea7181fef8d93f2d Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Sun, 7 Jan 2024 19:55:18 -0800 Subject: [PATCH 4/5] update lookup func in BuildingProject aggregator to fetch from correct collection and limit results to materials --- src/controllers/bmdashboard/bmProjectController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/bmdashboard/bmProjectController.js b/src/controllers/bmdashboard/bmProjectController.js index b6b3e4e18..a4f6712e0 100644 --- a/src/controllers/bmdashboard/bmProjectController.js +++ b/src/controllers/bmdashboard/bmProjectController.js @@ -31,10 +31,11 @@ const bmMProjectController = function (BuildingProject) { { $unwind: '$buildingManager' }, { $lookup: { - from: 'buildingMaterials', + from: 'buildingInventoryItems', let: { id: '$_id' }, pipeline: [ { $match: { $expr: { $eq: ['$project', '$$id'] } } }, + { $match: { __t: 'material_item' } }, { $project: { updateRecord: 0, project: 0 } }, { $lookup: { From 90692149b687e823f3de95c2ba21bf71b03ef376 Mon Sep 17 00:00:00 2001 From: Tim Kent Date: Sun, 7 Jan 2024 20:32:38 -0800 Subject: [PATCH 5/5] update itemType ref in large item schema --- src/models/bmdashboard/buildingInventoryItem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/bmdashboard/buildingInventoryItem.js b/src/models/bmdashboard/buildingInventoryItem.js index c44f37211..fdcfde3dd 100644 --- a/src/models/bmdashboard/buildingInventoryItem.js +++ b/src/models/bmdashboard/buildingInventoryItem.js @@ -41,7 +41,7 @@ const smallItemBase = mongoose.model('smallItemBase', smallItemBaseSchema, 'buil // documents stored in 'buildingInventoryItems' collection const largeItemBaseSchema = mongoose.Schema({ - itemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'buildingInventoryType' }, + itemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'invTypeBase' }, project: { type: mongoose.SchemaTypes.ObjectId, ref: 'buildingProject' }, purchaseStatus: { type: String, enum: ['Rental', 'Purchase'], required: true }, // rental fields are required if purchaseStatus = "Rental" (hopefully correct syntax)