Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gary add materials page #577

Merged
merged 14 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 52 additions & 52 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/controllers/bmdashboard/bmInventoryTypeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const bmInventoryTypeController = function (InvType) {
const fetchMaterialTypes = async (req, res) => {
try {
InvType
.find()
.exec()
.then(result => res.status(200).send(result))
.catch(error => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};

return {
fetchMaterialTypes,
};
};

module.exports = bmInventoryTypeController;
64 changes: 62 additions & 2 deletions src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const mongoose = require('mongoose');

const bmMaterialsController = function (ItemMaterial) {
// use in bmPurchaseMaterials auth check (see below)
// const buildingProject = require('../../models/bmdashboard/buildingProject');

const bmMaterialsController = function (ItemMaterial, BuildingMaterial) {
const bmMaterialsList = async function _matsList(req, res) {
try {
ItemMaterial.find()
Expand Down Expand Up @@ -42,7 +45,64 @@ const bmMaterialsController = function (ItemMaterial) {
res.json(err);
}
};
return { bmMaterialsList };

const bmPurchaseMaterials = async function (req, res) {
const {
projectId,
matTypeId,
quantity,
priority,
brand,
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
// TODO: uncomment this code to execute auth check
// const { buildingManager: bmId } = await buildingProject.findById(projectId, 'buildingManager').exec();
// if (bmId !== requestorId) {
// res.status(403).send({ message: 'You are not authorized to edit this record.' });
// return;
// }

// 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 doc = await BuildingMaterial.findOne({ project: projectId, itemType: matTypeId });
if (!doc) {
const newDoc = {
itemType: matTypeId,
project: projectId,
purchaseRecord: [newPurchaseRecord],
};
BuildingMaterial
.create(newDoc)
.then(() => res.status(201).send())
.catch(error => res.status(500).send(error));
return;
}
BuildingMaterial
.findOneAndUpdate(
{ _id: mongoose.Types.ObjectId(doc._id) },
{ $push: { purchaseRecord: newPurchaseRecord } },
)
.exec()
.then(() => res.status(201).send())
.catch(error => res.status(500).send(error));
} catch (error) {
res.status(500).send(error);
}
};
return {
bmMaterialsList,
bmPurchaseMaterials,
};
};

module.exports = bmMaterialsController;
7 changes: 6 additions & 1 deletion src/controllers/inventoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,15 @@ const inventoryController = function (Item, ItemType) {
}
const itemType = new ItemType();

itemType.type = req.body.type;
itemType.name = req.body.name;
itemType.description = req.body.description;
itemType.uom = req.body.uom;
itemType.totalStock = req.body.totalStock;
itemType.totalAvailable = req.body.totalAvailable;
itemType.projectsUsing = [];
itemType.imageUrl = req.body.imageUrl || req.body.imageURL;
itemType.quantifier = req.body.quantifier;
itemType.link = req.body.link;

itemType.save()
.then(results => res.status(201).send(results))
Expand Down
13 changes: 13 additions & 0 deletions src/models/bmdashboard/buildingInventoryType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const buildingInventoryType = new Schema({
category: { type: String, enum: ['Consumable', 'Material', 'Tool', 'Equipment'], required: true },
name: { type: String, required: true },
description: { type: String, required: true },
unit: { type: String, required: true }, // unit of measurement
imageUrl: String,
});

module.exports = mongoose.model('buildingInventoryType', buildingInventoryType, 'buildingInventoryTypes');
30 changes: 30 additions & 0 deletions src/models/bmdashboard/buildingMaterial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const buildingMaterial = new Schema({
itemType: { type: mongoose.SchemaTypes.ObjectId, ref: 'buildingInventoryType' },
project: { type: mongoose.SchemaTypes.ObjectId, ref: 'buildingProject' },
stockBought: { type: Number, default: 0 }, // total amount of item bought for use in the project
stockUsed: { type: Number, default: 0 }, // total amount of item used successfully in the project
stockWasted: { type: Number, default: 0 }, // total amount of item wasted/ruined/lost in the project
stockAvailable: { type: Number, default: 0 }, // bought - (used + wasted)
purchaseRecord: [{
_id: false, // do not add _id field to subdocument
date: { type: Date, default: Date.now() },
requestedBy: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile' },
quantity: { type: Number, required: true },
priority: { type: String, enum: ['Low', 'Medium', 'High'], required: true },
brand: String,
status: { type: String, default: 'Pending', enum: ['Approved', 'Pending', 'Rejected'] },
}],
updateRecord: [{
_id: false,
date: { type: Date, required: true },
createdBy: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile' },
quantityUsed: { type: Number, required: true },
quantityWasted: { type: Number, required: true },
}],
});

module.exports = mongoose.model('buildingMaterial', buildingMaterial, 'buildingMaterials');
6 changes: 3 additions & 3 deletions src/models/inventoryItemMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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 },
stockUsed: { type: Number, default: 0 },
stockAvailable: { type: Number, required: true },
stockHeld: { type: Number, required: true },
stockWasted: { type: Number, required: true },
stockHeld: { type: Number, default: 0 },
stockWasted: { type: Number, default: 0 },
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 },
Expand Down
6 changes: 2 additions & 4 deletions src/models/inventoryItemType.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ const InventoryItemType = new Schema({ // creates an item, tracks total amount i
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'} ],
projectsUsing: [{ type: mongoose.SchemaTypes.ObjectId, ref: 'project' }],
imageUrl: { type: String },
link: { type: String}
link: { type: String },
});

module.exports = mongoose.model('inventoryItemType', InventoryItemType, 'inventoryItemType');


13 changes: 13 additions & 0 deletions src/routes/bmdashboard/bmInventoryTypeRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require('express');

const routes = function (invType) {
const inventoryTypeRouter = express.Router();
const controller = require('../../controllers/bmdashboard/bmInventoryTypeController')(invType);

inventoryTypeRouter.route('/invtypes/materials')
.get(controller.fetchMaterialTypes);

return inventoryTypeRouter;
};

module.exports = routes;
13 changes: 6 additions & 7 deletions src/routes/bmdashboard/bmMaterialsRouter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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);

const routes = function (itemMaterial, buildingMaterial) {
const materialsRouter = express.Router();
const controller = require('../../controllers/bmdashboard/bmMaterialsController')(itemMaterial, buildingMaterial);
materialsRouter.route('/materials')
.get(controller.bmMaterialsList)
.post(controller.bmPurchaseMaterials);
return materialsRouter;
};

Expand Down
7 changes: 5 additions & 2 deletions src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const reason = require('../models/reason');
const mouseoverText = require('../models/mouseoverText');
const inventoryItemMaterial = require('../models/inventoryItemMaterial');
const buildingProject = require('../models/bmdashboard/buildingProject');

const buildingInventoryType = require('../models/bmdashboard/buildingInventoryType');
const buildingMaterial = require('../models/bmdashboard/buildingMaterial');

const userProfileRouter = require('../routes/userProfileRouter')(userProfile);
const badgeRouter = require('../routes/badgeRouter')(badge);
Expand Down Expand Up @@ -60,8 +61,9 @@ const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverTe

// bm dashboard
const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')();
const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial);
const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial, buildingMaterial);
const bmProjectRouter = require('../routes/bmdashboard/bmProjectRouter')(buildingProject);
const bmInventoryTypeRouter = require('../routes/bmdashboard/bmInventoryTypeRouter')(buildingInventoryType);

module.exports = function (app) {
app.use('/api', forgotPwdRouter);
Expand Down Expand Up @@ -97,4 +99,5 @@ module.exports = function (app) {
app.use('/api/bm', bmLoginRouter);
app.use('/api/bm', bmMaterialsRouter);
app.use('/api/bm', bmProjectRouter);
app.use('/api/bm', bmInventoryTypeRouter);
};
Loading