Merge pull request #13 from kfnawaz/feat/model-changes

Feat: Model changes
This commit is contained in:
bluestreamlds
2022-01-06 08:05:26 +05:30
committed by GitHub
9 changed files with 111 additions and 100 deletions

View File

@@ -23,7 +23,7 @@ const LevelPositions = [
"RUF", "RUF",
]; ];
const InventoryScopes = ["Inventory", "Material", "Item"]; const InventoryScopes = ["Inventory", "WidgetFamily", "Item"];
const WarehouseScopes = [ const WarehouseScopes = [
"Warehouse", "Warehouse",
@@ -51,7 +51,7 @@ const CustomAttributeTypes = [
"Enumerable", "Enumerable",
]; ];
const SublevelInventoryTypes = ["Inventory", "Material", "Item"]; const SublevelInventoryTypes = ["Inventory", "WidgetFamily", "Item"];
const AUTHENTICATION_FAILURE_ERROR_MESSAGE = "Authentication Failed!"; const AUTHENTICATION_FAILURE_ERROR_MESSAGE = "Authentication Failed!";
const AUTHORIZATION_FAILURE_ERROR_MESSAGE = const AUTHORIZATION_FAILURE_ERROR_MESSAGE =

View File

@@ -8,7 +8,7 @@ const Bay = require("../models/Bay");
const Level = require("../models/Level"); const Level = require("../models/Level");
const Sublevel = require("../models/Sublevel"); const Sublevel = require("../models/Sublevel");
const Inventory = require("../models/Inventory"); const Inventory = require("../models/Inventory");
const Material = require("../models/Material"); const WidgetFamily = require("../models/WidgetFamily");
const Item = require("../models/Item"); const Item = require("../models/Item");
const createWarehouse = async ({ name, address, specs, company_id }) => { 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 = []; const itemsArray = [];
for (const itemData of items) { for (const itemData of items) {
const item = { const item = {
@@ -191,7 +191,7 @@ const createItems = async (items, material) => {
countPerPallet: itemData.countPerPallet, countPerPallet: itemData.countPerPallet,
countPerPalletPackage: itemData.countPerPalletPackage, countPerPalletPackage: itemData.countPerPalletPackage,
customAttributes: itemData.customAttributes, customAttributes: itemData.customAttributes,
material, widgetFamily,
}; };
if (Object.values(item).every((_) => _)) { if (Object.values(item).every((_) => _)) {
@@ -202,33 +202,33 @@ const createItems = async (items, material) => {
return itemsArray; return itemsArray;
}; };
const createMaterials = async (materials, inventory, parent = undefined) => { const createWidgetFamilies = async (widgetFamilies, inventory, parent = undefined) => {
const materialsData = []; const widgetFamiliesData = [];
for (const { name, family, items } of materials) { for (const { name, family, items } of widgetFamilies) {
const material = await Material.create({ const widgetFamily = await WidgetFamily.create({
name, name,
parent, parent,
inventory, inventory,
}); });
let materialFamily; let widgetFamilyFamily;
if (family) { if (family) {
materialFamily = await createMaterials(family, inventory, material); widgetFamilyFamily = await createWidgetFamilies(family, inventory, widgetFamily);
} }
let itemsList; let itemsList;
if (items) { if (items) {
itemsList = await createItems(items, material); itemsList = await createItems(items, widgetFamily);
} }
materialsData.push({ widgetFamiliesData.push({
material, widgetFamily,
family: materialFamily, family: widgetFamilyFamily,
items: itemsList, items: itemsList,
}); });
} }
return materialsData; return widgetFamiliesData;
}; };
module.exports = { module.exports = {
@@ -292,17 +292,17 @@ module.exports = {
} }
inventorySchema.inventory = inventory.toObject(); inventorySchema.inventory = inventory.toObject();
if (req.body.inventory.materials) { if (req.body.inventory.widgetFamilies) {
const materials = await createMaterials(req.body.inventory.materials, inventory); const widgetFamilies = await createWidgetFamilies(req.body.inventory.widgetFamilies, inventory);
if (!materials) { if (!widgetFamilies) {
res.status(400).send({ res.status(400).send({
success: false, success: false,
message: "Creation of Materials Failed, invalid/missing params", message: "Creation of WidgetFamilies Failed, invalid/missing params",
}); });
return; return;
} }
inventorySchema.inventory.materials = materials; inventorySchema.inventory.widgetFamilies = widgetFamilies;
} }
res.send({ success: true, data: inventorySchema }); res.send({ success: true, data: inventorySchema });

View File

@@ -14,7 +14,7 @@ const levelRouter = require("./level.router");
const sublevelRouter = require("./sublevel.router"); const sublevelRouter = require("./sublevel.router");
const dashboardRouter = require("./dashboard.router"); const dashboardRouter = require("./dashboard.router");
const inventoryRouter = require("./inventory.router"); const inventoryRouter = require("./inventory.router");
const materialRouter = require("./material.router"); const widgetFamilyRouter = require("./widgetFamily.router");
const itemRouter = require("./item.router"); const itemRouter = require("./item.router");
router.use("/user-role", AuthenticateMiddleware, userRoleRouter); router.use("/user-role", AuthenticateMiddleware, userRoleRouter);
@@ -30,7 +30,7 @@ router.use("/level", levelRouter);
router.use("/sublevel", sublevelRouter); router.use("/sublevel", sublevelRouter);
router.use("/dashboard", dashboardRouter); router.use("/dashboard", dashboardRouter);
router.use("/inventory", inventoryRouter); router.use("/inventory", inventoryRouter);
router.use("/material", materialRouter); router.use("/widget-family", widgetFamilyRouter);
router.use("/item", itemRouter); router.use("/item", itemRouter);
router.get("/", (req, res) => { router.get("/", (req, res) => {

View File

@@ -1,6 +1,6 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const Item = require("../models/Item"); const Item = require("../models/Item");
const Material = require("../models/Material"); const WidgetFamily = require("../models/WidgetFamily");
const Inventory = require("../models/Inventory"); const Inventory = require("../models/Inventory");
const { InventoryTypes } = require("../config/constants"); const { InventoryTypes } = require("../config/constants");
@@ -32,9 +32,9 @@ module.exports = {
* Create a Item * Create a Item
*/ */
createItem: async (req, res, next) => { createItem: async (req, res, next) => {
let material; let widgetFamily;
if (req.body.materialId && mongoose.isValidObjectId(req.body.materialId)) { if (req.body.widgetFamilyId && mongoose.isValidObjectId(req.body.widgetFamilyId)) {
material = await Material.findById(req.body.materialId); widgetFamily = await WidgetFamily.findById(req.body.widgetFamilyId);
} }
const item = { const item = {
commonName: req.body.commonName, commonName: req.body.commonName,
@@ -50,7 +50,7 @@ module.exports = {
countPerPallet: req.body.countPerPallet, countPerPallet: req.body.countPerPallet,
countPerPalletPackage: req.body.countPerPalletPackage, countPerPalletPackage: req.body.countPerPalletPackage,
customAttributes: req.body.customAttributes, customAttributes: req.body.customAttributes,
material: material, widgetFamily: widgetFamily,
}; };
for (const key of Object.keys(item)) { for (const key of Object.keys(item)) {
@@ -79,9 +79,9 @@ module.exports = {
*/ */
updateItemByID: async (req, res, next) => { updateItemByID: async (req, res, next) => {
const { id } = req.params; const { id } = req.params;
let material; let widgetFamily;
if (req.body.materialId && mongoose.isValidObjectId(req.body.materialId)) { if (req.body.widgetFamilyId && mongoose.isValidObjectId(req.body.widgetFamilyId)) {
material = await Material.findById(req.body.materialId); widgetFamily = await WidgetFamily.findById(req.body.widgetFamilyId);
} }
if (!id) { if (!id) {
@@ -103,7 +103,7 @@ module.exports = {
countPerPallet: req.body.countPerPallet, countPerPallet: req.body.countPerPallet,
countPerPalletPackage: req.body.countPerPalletPackage, countPerPalletPackage: req.body.countPerPalletPackage,
customAttributes: req.body.customAttributes, customAttributes: req.body.customAttributes,
material: material, widgetFamily: widgetFamily,
}; };
try { try {
@@ -134,33 +134,33 @@ module.exports = {
page = page ? parseInt(page) : 0; page = page ? parseInt(page) : 0;
perPage = perPage ? parseInt(perPage) : 10; perPage = perPage ? parseInt(perPage) : 10;
let inventories; let inventories;
let materials; let widgetFamilies;
let itemFilters; let itemFilters;
try { try {
if (type && InventoryTypes.includes(type)) { if (type && InventoryTypes.includes(type)) {
inventories = await Inventory.find({ type }); inventories = await Inventory.find({ type });
} }
const materialFilters = []; const widgetFamilyFilters = [];
if (inventories) { if (inventories) {
materialFilters.push({ widgetFamilyFilters.push({
inventory: { $in: inventories.map((_) => _._id) }, inventory: { $in: inventories.map((_) => _._id) },
}); });
} }
if (family) { if (family) {
materialFilters.push({ widgetFamilyFilters.push({
name: family, name: family,
}); });
} }
if (materialFilters.length > 0) { if (widgetFamilyFilters.length > 0) {
materials = await Material.find({ widgetFamilies = await WidgetFamily.find({
$or: materialFilters, $or: widgetFamilyFilters,
}); });
} }
if (materials) { if (widgetFamilies) {
itemFilters = { material: { $in: materials.map((_) => _._id) } }; itemFilters = { widgetFamily: { $in: widgetFamilies.map((_) => _._id) } };
} else { } else {
itemFilters = {}; itemFilters = {};
} }
@@ -172,7 +172,7 @@ module.exports = {
formalName: 1, formalName: 1,
description: 1, description: 1,
manufacturer: 1, manufacturer: 1,
material: 1, widgetFamily: 1,
size: 1, size: 1,
color: 1, color: 1,
type: 1, type: 1,
@@ -184,7 +184,7 @@ module.exports = {
customAttributes: 1, customAttributes: 1,
}, },
{ skip: page * perPage, limit: perPage } { skip: page * perPage, limit: perPage }
).populate({ path: "material" }); ).populate({ path: "widgetFamily" });
if (!itemData) { if (!itemData) {
res.status(404); res.status(404);
return; return;

View File

@@ -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;

View File

@@ -1,12 +1,12 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const Material = require("../models/Material"); const WidgetFamily = require("../models/WidgetFamily");
const Inventory = require("../models/Inventory"); const Inventory = require("../models/Inventory");
module.exports = { 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; const { id } = req.params;
if (!id) { if (!id) {
@@ -15,27 +15,27 @@ module.exports = {
} }
try { try {
const materialData = await Material.findById(id); const widgetFamilyData = await WidgetFamily.findById(id);
if (!materialData) { if (!widgetFamilyData) {
res.status(404); res.status(404);
return; return;
} }
res.send({ success: true, data: materialData }); res.send({ success: true, data: widgetFamilyData });
} catch (error) { } catch (error) {
next(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; const { name, parentId, inventoryId } = req.body;
try { try {
let parent; let parent;
if (parentId && mongoose.isValidObjectId(parentId)) { if (parentId && mongoose.isValidObjectId(parentId)) {
parent = await Material.findById(parentId); parent = await WidgetFamily.findById(parentId);
} else if (parentId && !mongoose.isValidObjectId(parentId)) { } else if (parentId && !mongoose.isValidObjectId(parentId)) {
res.status(400).send("Invalid params parentId"); res.status(400).send("Invalid params parentId");
return; return;
@@ -49,27 +49,27 @@ module.exports = {
return; return;
} }
const materialData = new Material({ const widgetFamilyData = new WidgetFamily({
name, name,
parent, parent,
inventory, inventory,
}); });
await materialData.save(); await widgetFamilyData.save();
if (!materialData) { if (!widgetFamilyData) {
res.status(404); res.status(404);
return; return;
} }
res.send({ success: true, data: materialData }); res.send({ success: true, data: widgetFamilyData });
} catch (error) { } catch (error) {
next(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; const { id } = req.params;
if (!id) { if (!id) {
@@ -85,20 +85,20 @@ module.exports = {
} }
try { try {
const materialData = await Material.findById(id); const widgetFamilyData = await WidgetFamily.findById(id);
if (!materialData) { if (!widgetFamilyData) {
res.status(404); res.status(404);
return; return;
} }
if (name) { if (name) {
materialData.name = name; widgetFamilyData.name = name;
} }
let parent; let parent;
if (parentId && mongoose.isValidObjectId(parentId)) { if (parentId && mongoose.isValidObjectId(parentId)) {
parent = await Material.findById(parentId); parent = await WidgetFamily.findById(parentId);
materialData.parent = parent; widgetFamilyData.parent = parent;
} else if (parentId && !mongoose.isValidObjectId(parentId)) { } else if (parentId && !mongoose.isValidObjectId(parentId)) {
res.status(400).send("Invalid params parentId"); res.status(400).send("Invalid params parentId");
return; return;
@@ -107,22 +107,22 @@ module.exports = {
let inventory; let inventory;
if (inventoryId && mongoose.isValidObjectId(inventoryId)) { if (inventoryId && mongoose.isValidObjectId(inventoryId)) {
inventory = await Inventory.findById(inventoryId); inventory = await Inventory.findById(inventoryId);
materialData.inventory = inventory; widgetFamilyData.inventory = inventory;
} else if (inventoryId && !mongoose.isValidObjectId(inventoryId)) { } else if (inventoryId && !mongoose.isValidObjectId(inventoryId)) {
res.status(400).send("Invalid params inventoryId"); res.status(400).send("Invalid params inventoryId");
return; return;
} }
await materialData.save(); await widgetFamilyData.save();
res.send({ success: true, data: materialData }); res.send({ success: true, data: widgetFamilyData });
} catch (error) { } catch (error) {
next(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; let { inventory, page, perPage } = req.query;
page = page ? parseInt(page) : 0; page = page ? parseInt(page) : 0;
perPage = perPage ? parseInt(perPage) : 0; perPage = perPage ? parseInt(perPage) : 0;
@@ -133,18 +133,18 @@ module.exports = {
} }
try { try {
const materialData = await Material.find( const widgetFamilyData = await WidgetFamily.find(
{ inventory: inventory }, { inventory: inventory },
{ id: 1, name: 1, parent: 1, inventory: 1 }, { id: 1, name: 1, parent: 1, inventory: 1 },
{ skip: page * perPage, limit: perPage } { skip: page * perPage, limit: perPage }
) )
.populate({ path: "parent" }) .populate({ path: "parent" })
.populate({ path: "inventory" }); .populate({ path: "inventory" });
if (!materialData) { if (!widgetFamilyData) {
res.status(404); res.status(404);
return; return;
} }
res.send({ success: true, data: materialData }); res.send({ success: true, data: widgetFamilyData });
} catch (error) { } catch (error) {
next(error); next(error);
} }

View File

@@ -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;

View File

@@ -18,9 +18,9 @@ const schema = new mongoose.Schema(
required: true, required: true,
trim: true, trim: true,
}, },
material: { widgetFamily: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Material", ref: "WidgetFamily",
required: true, required: true,
}, },
manufacturer: { manufacturer: {
@@ -83,6 +83,17 @@ const schema = new mongoose.Schema(
}, },
}, },
], ],
policiesMetadata: {
underStockLevelCount: {
type: Number,
},
overStockLevelCount: {
type: Number,
},
alertStockLevelCount: {
type: Number,
},
},
}, },
{ {
timestamps: true, timestamps: true,

View File

@@ -9,7 +9,7 @@ const schema = new mongoose.Schema(
}, },
parent: { parent: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Material", ref: "WidgetFamily",
}, },
inventory: { inventory: {
type: mongoose.Schema.Types.ObjectId, 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;