diff --git a/src/config/constants.js b/src/config/constants.js index 8e66871..68c3d9b 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -23,7 +23,7 @@ const LevelPositions = [ "RUF", ]; -const InventoryScopes = ["Inventory", "Material", "Item"]; +const InventoryScopes = ["Inventory", "WidgetFamily", "Item"]; const WarehouseScopes = [ "Warehouse", @@ -51,7 +51,7 @@ const CustomAttributeTypes = [ "Enumerable", ]; -const SublevelInventoryTypes = ["Inventory", "Material", "Item"]; +const SublevelInventoryTypes = ["Inventory", "WidgetFamily", "Item"]; const AUTHENTICATION_FAILURE_ERROR_MESSAGE = "Authentication Failed!"; const AUTHORIZATION_FAILURE_ERROR_MESSAGE = diff --git a/src/controller/dashboard.controller.js b/src/controller/dashboard.controller.js index bfa8a4b..9b67ce4 100644 --- a/src/controller/dashboard.controller.js +++ b/src/controller/dashboard.controller.js @@ -8,7 +8,7 @@ const Bay = require("../models/Bay"); const Level = require("../models/Level"); const Sublevel = require("../models/Sublevel"); const Inventory = require("../models/Inventory"); -const Material = require("../models/Material"); +const WidgetFamily = require("../models/WidgetFamily"); const Item = require("../models/Item"); const createWarehouse = async ({ name, address, specs, company_id }) => { @@ -174,7 +174,7 @@ const createInventory = async ({ name, type }) => { }); }; -const createItems = async (items, material) => { +const createItems = async (items, widgetFamily) => { const itemsArray = []; for (const itemData of items) { const item = { @@ -191,7 +191,7 @@ const createItems = async (items, material) => { countPerPallet: itemData.countPerPallet, countPerPalletPackage: itemData.countPerPalletPackage, customAttributes: itemData.customAttributes, - material, + widgetFamily, }; if (Object.values(item).every((_) => _)) { @@ -202,33 +202,33 @@ const createItems = async (items, material) => { return itemsArray; }; -const createMaterials = async (materials, inventory, parent = undefined) => { - const materialsData = []; - for (const { name, family, items } of materials) { - const material = await Material.create({ +const createWidgetFamilies = async (widgetFamilies, inventory, parent = undefined) => { + const widgetFamiliesData = []; + for (const { name, family, items } of widgetFamilies) { + const widgetFamily = await WidgetFamily.create({ name, parent, inventory, }); - let materialFamily; + let widgetFamilyFamily; if (family) { - materialFamily = await createMaterials(family, inventory, material); + widgetFamilyFamily = await createWidgetFamilies(family, inventory, widgetFamily); } let itemsList; if (items) { - itemsList = await createItems(items, material); + itemsList = await createItems(items, widgetFamily); } - materialsData.push({ - material, - family: materialFamily, + widgetFamiliesData.push({ + widgetFamily, + family: widgetFamilyFamily, items: itemsList, }); } - return materialsData; + return widgetFamiliesData; }; module.exports = { @@ -292,17 +292,17 @@ module.exports = { } inventorySchema.inventory = inventory.toObject(); - if (req.body.inventory.materials) { - const materials = await createMaterials(req.body.inventory.materials, inventory); - if (!materials) { + if (req.body.inventory.widgetFamilies) { + const widgetFamilies = await createWidgetFamilies(req.body.inventory.widgetFamilies, inventory); + if (!widgetFamilies) { res.status(400).send({ success: false, - message: "Creation of Materials Failed, invalid/missing params", + message: "Creation of WidgetFamilies Failed, invalid/missing params", }); return; } - inventorySchema.inventory.materials = materials; + inventorySchema.inventory.widgetFamilies = widgetFamilies; } res.send({ success: true, data: inventorySchema }); diff --git a/src/controller/index.js b/src/controller/index.js index 71c055c..111cb1e 100644 --- a/src/controller/index.js +++ b/src/controller/index.js @@ -14,7 +14,7 @@ const levelRouter = require("./level.router"); const sublevelRouter = require("./sublevel.router"); const dashboardRouter = require("./dashboard.router"); const inventoryRouter = require("./inventory.router"); -const materialRouter = require("./material.router"); +const widgetFamilyRouter = require("./widgetFamily.router"); const itemRouter = require("./item.router"); router.use("/user-role", AuthenticateMiddleware, userRoleRouter); @@ -30,7 +30,7 @@ router.use("/level", levelRouter); router.use("/sublevel", sublevelRouter); router.use("/dashboard", dashboardRouter); router.use("/inventory", inventoryRouter); -router.use("/material", materialRouter); +router.use("/widget-family", widgetFamilyRouter); router.use("/item", itemRouter); router.get("/", (req, res) => { diff --git a/src/controller/item.controller.js b/src/controller/item.controller.js index a1a3e70..e7e6523 100644 --- a/src/controller/item.controller.js +++ b/src/controller/item.controller.js @@ -1,6 +1,6 @@ const mongoose = require("mongoose"); const Item = require("../models/Item"); -const Material = require("../models/Material"); +const WidgetFamily = require("../models/WidgetFamily"); const Inventory = require("../models/Inventory"); const { InventoryTypes } = require("../config/constants"); @@ -32,9 +32,9 @@ module.exports = { * Create a Item */ createItem: async (req, res, next) => { - let material; - if (req.body.materialId && mongoose.isValidObjectId(req.body.materialId)) { - material = await Material.findById(req.body.materialId); + let widgetFamily; + if (req.body.widgetFamilyId && mongoose.isValidObjectId(req.body.widgetFamilyId)) { + widgetFamily = await WidgetFamily.findById(req.body.widgetFamilyId); } const item = { commonName: req.body.commonName, @@ -50,7 +50,7 @@ module.exports = { countPerPallet: req.body.countPerPallet, countPerPalletPackage: req.body.countPerPalletPackage, customAttributes: req.body.customAttributes, - material: material, + widgetFamily: widgetFamily, }; for (const key of Object.keys(item)) { @@ -79,9 +79,9 @@ module.exports = { */ updateItemByID: async (req, res, next) => { const { id } = req.params; - let material; - if (req.body.materialId && mongoose.isValidObjectId(req.body.materialId)) { - material = await Material.findById(req.body.materialId); + let widgetFamily; + if (req.body.widgetFamilyId && mongoose.isValidObjectId(req.body.widgetFamilyId)) { + widgetFamily = await WidgetFamily.findById(req.body.widgetFamilyId); } if (!id) { @@ -103,7 +103,7 @@ module.exports = { countPerPallet: req.body.countPerPallet, countPerPalletPackage: req.body.countPerPalletPackage, customAttributes: req.body.customAttributes, - material: material, + widgetFamily: widgetFamily, }; try { @@ -134,33 +134,33 @@ module.exports = { page = page ? parseInt(page) : 0; perPage = perPage ? parseInt(perPage) : 10; let inventories; - let materials; + let widgetFamilies; let itemFilters; try { if (type && InventoryTypes.includes(type)) { inventories = await Inventory.find({ type }); } - const materialFilters = []; + const widgetFamilyFilters = []; if (inventories) { - materialFilters.push({ + widgetFamilyFilters.push({ inventory: { $in: inventories.map((_) => _._id) }, }); } if (family) { - materialFilters.push({ + widgetFamilyFilters.push({ name: family, }); } - if (materialFilters.length > 0) { - materials = await Material.find({ - $or: materialFilters, + if (widgetFamilyFilters.length > 0) { + widgetFamilies = await WidgetFamily.find({ + $or: widgetFamilyFilters, }); } - if (materials) { - itemFilters = { material: { $in: materials.map((_) => _._id) } }; + if (widgetFamilies) { + itemFilters = { widgetFamily: { $in: widgetFamilies.map((_) => _._id) } }; } else { itemFilters = {}; } @@ -172,7 +172,7 @@ module.exports = { formalName: 1, description: 1, manufacturer: 1, - material: 1, + widgetFamily: 1, size: 1, color: 1, type: 1, @@ -184,7 +184,7 @@ module.exports = { customAttributes: 1, }, { skip: page * perPage, limit: perPage } - ).populate({ path: "material" }); + ).populate({ path: "widgetFamily" }); if (!itemData) { res.status(404); return; diff --git a/src/controller/material.router.js b/src/controller/material.router.js deleted file mode 100644 index 6760fff..0000000 --- a/src/controller/material.router.js +++ /dev/null @@ -1,24 +0,0 @@ -const router = require("express").Router(); -const controller = require("./material.controller"); - -/** - * @route /material/ - */ -router.post("/", controller.createMaterial); - -/** - * @route /material/ - */ -router.patch("/:id", controller.updateMaterialByID); - -/** - * @route /material/search-by-inventory - */ -router.get("/search-by-inventory", controller.getMaterialByInventory); - -/** - * @route /material/:id - */ -router.get("/:id", controller.getMaterialByID); - -module.exports = router; diff --git a/src/controller/material.controller.js b/src/controller/widgetFamily.controller.js similarity index 66% rename from src/controller/material.controller.js rename to src/controller/widgetFamily.controller.js index 393e4b4..2a97926 100644 --- a/src/controller/material.controller.js +++ b/src/controller/widgetFamily.controller.js @@ -1,12 +1,12 @@ const mongoose = require("mongoose"); -const Material = require("../models/Material"); +const WidgetFamily = require("../models/WidgetFamily"); const Inventory = require("../models/Inventory"); module.exports = { /** - * Gets the Material data by `id` + * Gets the WidgetFamily data by `id` */ - getMaterialByID: async (req, res, next) => { + getWidgetFamilyByID: async (req, res, next) => { const { id } = req.params; if (!id) { @@ -15,27 +15,27 @@ module.exports = { } try { - const materialData = await Material.findById(id); - if (!materialData) { + const widgetFamilyData = await WidgetFamily.findById(id); + if (!widgetFamilyData) { res.status(404); return; } - res.send({ success: true, data: materialData }); + res.send({ success: true, data: widgetFamilyData }); } catch (error) { next(error); } }, /** - * Create a Material + * Create a WidgetFamily */ - createMaterial: async (req, res, next) => { + createWidgetFamily: async (req, res, next) => { const { name, parentId, inventoryId } = req.body; try { let parent; if (parentId && mongoose.isValidObjectId(parentId)) { - parent = await Material.findById(parentId); + parent = await WidgetFamily.findById(parentId); } else if (parentId && !mongoose.isValidObjectId(parentId)) { res.status(400).send("Invalid params parentId"); return; @@ -49,27 +49,27 @@ module.exports = { return; } - const materialData = new Material({ + const widgetFamilyData = new WidgetFamily({ name, parent, inventory, }); - await materialData.save(); - if (!materialData) { + await widgetFamilyData.save(); + if (!widgetFamilyData) { res.status(404); return; } - res.send({ success: true, data: materialData }); + res.send({ success: true, data: widgetFamilyData }); } catch (error) { next(error); } }, /** - * Update a Material detail + * Update a WidgetFamily detail */ - updateMaterialByID: async (req, res, next) => { + updateWidgetFamilyByID: async (req, res, next) => { const { id } = req.params; if (!id) { @@ -85,20 +85,20 @@ module.exports = { } try { - const materialData = await Material.findById(id); - if (!materialData) { + const widgetFamilyData = await WidgetFamily.findById(id); + if (!widgetFamilyData) { res.status(404); return; } if (name) { - materialData.name = name; + widgetFamilyData.name = name; } let parent; if (parentId && mongoose.isValidObjectId(parentId)) { - parent = await Material.findById(parentId); - materialData.parent = parent; + parent = await WidgetFamily.findById(parentId); + widgetFamilyData.parent = parent; } else if (parentId && !mongoose.isValidObjectId(parentId)) { res.status(400).send("Invalid params parentId"); return; @@ -107,22 +107,22 @@ module.exports = { let inventory; if (inventoryId && mongoose.isValidObjectId(inventoryId)) { inventory = await Inventory.findById(inventoryId); - materialData.inventory = inventory; + widgetFamilyData.inventory = inventory; } else if (inventoryId && !mongoose.isValidObjectId(inventoryId)) { res.status(400).send("Invalid params inventoryId"); return; } - await materialData.save(); - res.send({ success: true, data: materialData }); + await widgetFamilyData.save(); + res.send({ success: true, data: widgetFamilyData }); } catch (error) { next(error); } }, /** - * Gets the Material data by `inventory` + * Gets the WidgetFamily data by `inventory` */ - getMaterialByInventory: async (req, res, next) => { + getWidgetFamilyByInventory: async (req, res, next) => { let { inventory, page, perPage } = req.query; page = page ? parseInt(page) : 0; perPage = perPage ? parseInt(perPage) : 0; @@ -133,18 +133,18 @@ module.exports = { } try { - const materialData = await Material.find( + const widgetFamilyData = await WidgetFamily.find( { inventory: inventory }, { id: 1, name: 1, parent: 1, inventory: 1 }, { skip: page * perPage, limit: perPage } ) .populate({ path: "parent" }) .populate({ path: "inventory" }); - if (!materialData) { + if (!widgetFamilyData) { res.status(404); return; } - res.send({ success: true, data: materialData }); + res.send({ success: true, data: widgetFamilyData }); } catch (error) { next(error); } diff --git a/src/controller/widgetFamily.router.js b/src/controller/widgetFamily.router.js new file mode 100644 index 0000000..a26196e --- /dev/null +++ b/src/controller/widgetFamily.router.js @@ -0,0 +1,24 @@ +const router = require("express").Router(); +const controller = require("./widgetFamily.controller"); + +/** + * @route /widgetFamily/ + */ +router.post("/", controller.createWidgetFamily); + +/** + * @route /widgetFamily/ + */ +router.patch("/:id", controller.updateWidgetFamilyByID); + +/** + * @route /widgetFamily/search-by-inventory + */ +router.get("/search-by-inventory", controller.getWidgetFamilyByInventory); + +/** + * @route /widgetFamily/:id + */ +router.get("/:id", controller.getWidgetFamilyByID); + +module.exports = router; diff --git a/src/models/Item.js b/src/models/Item.js index 6835345..4c46b4a 100644 --- a/src/models/Item.js +++ b/src/models/Item.js @@ -18,9 +18,9 @@ const schema = new mongoose.Schema( required: true, trim: true, }, - material: { + widgetFamily: { type: mongoose.Schema.Types.ObjectId, - ref: "Material", + ref: "WidgetFamily", required: true, }, manufacturer: { @@ -83,6 +83,17 @@ const schema = new mongoose.Schema( }, }, ], + policiesMetadata: { + underStockLevelCount: { + type: Number, + }, + overStockLevelCount: { + type: Number, + }, + alertStockLevelCount: { + type: Number, + }, + }, }, { timestamps: true, diff --git a/src/models/Material.js b/src/models/WidgetFamily.js similarity index 75% rename from src/models/Material.js rename to src/models/WidgetFamily.js index 3f39983..3f52b98 100644 --- a/src/models/Material.js +++ b/src/models/WidgetFamily.js @@ -9,7 +9,7 @@ const schema = new mongoose.Schema( }, parent: { type: mongoose.Schema.Types.ObjectId, - ref: "Material", + ref: "WidgetFamily", }, inventory: { type: mongoose.Schema.Types.ObjectId, @@ -22,6 +22,6 @@ const schema = new mongoose.Schema( } ); -const Material = mongoose.model("Material", schema); +const WidgetFamily = mongoose.model("WidgetFamily", schema); -module.exports = Material; +module.exports = WidgetFamily;