From 37e492e166f2635ac21777c8c20df02a9e12077b Mon Sep 17 00:00:00 2001 From: Sathishkumar Krishnan Date: Thu, 30 Dec 2021 08:45:04 +0530 Subject: [PATCH] feat: filter queries for item, material & inventory --- src/controller/inventory.controller.js | 29 +++++++++++ src/controller/inventory.router.js | 5 ++ src/controller/item.controller.js | 70 ++++++++++++++++++++++++++ src/controller/item.router.js | 5 ++ src/controller/material.controller.js | 30 +++++++++++ src/controller/material.router.js | 5 ++ 6 files changed, 144 insertions(+) diff --git a/src/controller/inventory.controller.js b/src/controller/inventory.controller.js index 9175e00..53d857b 100644 --- a/src/controller/inventory.controller.js +++ b/src/controller/inventory.controller.js @@ -1,4 +1,5 @@ const Inventory = require("../models/Inventory"); +const { InventoryTypes } = require("../config/constants"); module.exports = { /** @@ -86,4 +87,32 @@ module.exports = { next(error); } }, + /** + * Gets the Inventory data by `type` + */ + getInventoryByType: async (req, res, next) => { + let { type, page, perPage } = req.query; + page = page || 0; + perPage = perPage || 10; + + if (!type || !InventoryTypes.includes(type)) { + res.status(400).send("Missing/Invalid type param"); + return; + } + + try { + const inventoryData = await Inventory.find( + { type: type }, + { id: 1, name: 1, type: 1 }, + { skip: page * perPage, limit: perPage } + ); + if (!inventoryData) { + res.status(404); + return; + } + req.send({ success: true, data: inventoryData }); + } catch (error) { + next(error); + } + }, }; diff --git a/src/controller/inventory.router.js b/src/controller/inventory.router.js index a00ad8e..a46a984 100644 --- a/src/controller/inventory.router.js +++ b/src/controller/inventory.router.js @@ -16,4 +16,9 @@ router.post("/", controller.createInventory); */ router.patch("/:id", controller.updateInventoryByID); +/** + * @route /inventory/filter-by-type + */ +router.get("/filter-by-type", controller.getInventoryByType); + module.exports = router; diff --git a/src/controller/item.controller.js b/src/controller/item.controller.js index 1653df2..9820cad 100644 --- a/src/controller/item.controller.js +++ b/src/controller/item.controller.js @@ -1,6 +1,8 @@ const mongoose = require("mongoose"); const Item = require("../models/Item"); const Material = require("../models/Material"); +const Inventory = require("../models/Inventory"); +const { InventoryTypes } = require("../config/constants"); module.exports = { /** @@ -116,4 +118,72 @@ module.exports = { next(error); } }, + + /** + * Gets the Items data by filter + */ + getItemsByFilter: async (req, res, next) => { + let { family, type, page, perPage } = req.query; + page = page || 0; + perPage = perPage || 10; + let inventories; + let materials; + let itemFilters; + try { + if (type && InventoryTypes.includes(type)) { + inventories = await Inventory.find({ type }); + } + + const materialFilters = []; + if (inventories) { + materialFilters.push({ + inventory: { $in: inventories.map((_) => _._id) }, + }); + } + + if (family) { + materialFilters.push({ + name: family, + }); + } + if (materialFilters.length > 0) { + materials = await Material.find({ + $or: materialFilters, + }); + } + + if (materials) { + itemFilters = { material: { $in: materials.map((_) => _._id) } }; + } else { + itemFilters = {}; + } + const itemData = await Item.find( + itemFilters, + { + id: 1, + commonName: 1, + formalName: 1, + description: 1, + manufacturer: 1, + size: 1, + color: 1, + type: 1, + unitOfMaterial: 1, + unitCost: 1, + packageCount: 1, + countPerPallet: 1, + countPerPalletPackage: 1, + customAttributes: 1, + }, + { skip: page * perPage, limit: perPage } + ); + if (!itemData) { + res.status(404); + return; + } + req.send({ success: true, data: itemData }); + } catch (error) { + next(error); + } + }, }; diff --git a/src/controller/item.router.js b/src/controller/item.router.js index effbc24..fa135f0 100644 --- a/src/controller/item.router.js +++ b/src/controller/item.router.js @@ -16,4 +16,9 @@ router.post("/", controller.createItem); */ router.patch("/:id", controller.updateItemByID); +/** + * @route /item/filter + */ +router.get("/filter", controller.getItemsByFilter); + module.exports = router; diff --git a/src/controller/material.controller.js b/src/controller/material.controller.js index da5fb4e..b6ed670 100644 --- a/src/controller/material.controller.js +++ b/src/controller/material.controller.js @@ -119,4 +119,34 @@ module.exports = { next(error); } }, + /** + * Gets the Material data by `inventory` + */ + getMaterialByInventory: async (req, res, next) => { + let { inventory, page, perPage } = req.query; + page = page || 0; + perPage = perPage || 10; + + if (!inventory || !mongoose.isValidObjectId(inventory)) { + res.status(400).send("Missing inventory param"); + return; + } + + try { + const materialData = await Material.find( + { inventory: inventory }, + { id: 1, name: 1, parent: 1, inventory: 1 }, + { skip: page * perPage, limit: perPage } + ) + .populate({ path: "parent" }) + .populate({ path: "inventory" }); + if (!materialData) { + res.status(404); + return; + } + req.send({ success: true, data: materialData }); + } catch (error) { + next(error); + } + }, }; diff --git a/src/controller/material.router.js b/src/controller/material.router.js index 7fa0873..09fc561 100644 --- a/src/controller/material.router.js +++ b/src/controller/material.router.js @@ -16,4 +16,9 @@ router.post("/", controller.createMaterial); */ router.patch("/:id", controller.updateMaterialByID); +/** + * @route /material/search-by-inventory + */ +router.get("/search-by-inventory", controller.getMaterialByInventory); + module.exports = router;