diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index da45aad1d..207e2428a 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() @@ -42,15 +42,9 @@ const bmMaterialsController = function (ItemMaterial, 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 (ItemMaterial, 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 = { 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: { diff --git a/src/controllers/userProfileController.js b/src/controllers/userProfileController.js index a8c0cf230..1a3dd504f 100644 --- a/src/controllers/userProfileController.js +++ b/src/controllers/userProfileController.js @@ -241,6 +241,7 @@ const userProfileController = function (UserProfile) { ); const canEditTeamCode = req.body.requestor.role === 'Owner' + || req.body.requestor.role === 'Administrator' || req.body.requestor.permissions?.frontPermissions.includes('editTeamCode'); if (!isRequestorAuthorized) { @@ -575,6 +576,7 @@ const userProfileController = function (UserProfile) { if (key === 'teamCode') { const canEditTeamCode = req.body.requestor.role === 'Owner' + || req.body.requestor.role === 'Administrator' || req.body.requestor.permissions?.frontPermissions.includes('editTeamCode'); if (!canEditTeamCode) { diff --git a/src/models/bmdashboard/buildingInventoryItem.js b/src/models/bmdashboard/buildingInventoryItem.js index dfaa8c73a..fdcfde3dd 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? @@ -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) 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);