diff --git a/src/controller/inventory.controller.js b/src/controller/inventory.controller.js index 35fd891..3a01bdd 100644 --- a/src/controller/inventory.controller.js +++ b/src/controller/inventory.controller.js @@ -1,7 +1,15 @@ const Inventory = require("../models/Inventory"); +const WidgetFamily = require("../models/WidgetFamily"); const { InventoryTypes } = require("../config/constants"); module.exports = { + /** + * Gets the Inventory types + */ + getInventoryTypes: async (req, res, next) => { + res.send({ success: true, data: InventoryTypes }); + }, + /** * Gets the Inventory data by `id` */ @@ -162,6 +170,11 @@ module.exports = { res.status(404); return; } + + for (const inventory of inventoryData) { + inventory["widgetFamilies"] = await WidgetFamily.find({ inventory: inventory._id }); + } + res.send({ success: true, data: inventoryData }); } catch (error) { next(error); diff --git a/src/controller/inventory.router.js b/src/controller/inventory.router.js index f90000a..0beed4b 100644 --- a/src/controller/inventory.router.js +++ b/src/controller/inventory.router.js @@ -11,6 +11,11 @@ router.post("/", controller.createInventory); */ router.patch("/:id", controller.updateInventoryByID); +/** + * @route /inventory/types + */ +router.get("/types", controller.getInventoryTypes); + /** * @route /inventory/filter-by-type */ diff --git a/src/controller/item.controller.js b/src/controller/item.controller.js index 5dd3be9..49901f2 100644 --- a/src/controller/item.controller.js +++ b/src/controller/item.controller.js @@ -142,15 +142,24 @@ module.exports = { * Gets the Items data by filter */ getItemsByFilter: async (req, res, next) => { - let { family, type, page, perPage } = req.query; + let { family, type, inventory, page, perPage } = req.query; page = page ? parseInt(page) : 0; perPage = perPage ? parseInt(perPage) : 10; + const inventoryFilters = {}; let inventories; let widgetFamilies; let itemFilters; try { if (type && InventoryTypes.includes(type)) { - inventories = await Inventory.find({ type }); + inventoryFilters["type"] = type; + } + + if (inventory) { + inventoryFilters["_id"] = inventory; + } + + if (Object.keys(inventoryFilters).length > 0) { + inventories = await Inventory.find(inventoryFilters); } const widgetFamilyFilters = []; diff --git a/src/controller/widgetFamily.controller.js b/src/controller/widgetFamily.controller.js index b0199b8..112cf27 100644 --- a/src/controller/widgetFamily.controller.js +++ b/src/controller/widgetFamily.controller.js @@ -26,6 +26,29 @@ module.exports = { } }, + /** + * Gets the WidgetFamily children data by `id` + */ + getWidgetFamilyChildrenByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const widgetFamilyData = await WidgetFamily.find({ parent: id }); + if (!widgetFamilyData) { + res.status(404).send({ success: false, error: "Widget not found" }); + return; + } + res.send({ success: true, data: widgetFamilyData }); + } catch (error) { + next(error); + } + }, + /** * Create a WidgetFamily */ diff --git a/src/controller/widgetFamily.router.js b/src/controller/widgetFamily.router.js index a26196e..50b9f34 100644 --- a/src/controller/widgetFamily.router.js +++ b/src/controller/widgetFamily.router.js @@ -21,4 +21,9 @@ router.get("/search-by-inventory", controller.getWidgetFamilyByInventory); */ router.get("/:id", controller.getWidgetFamilyByID); +/** + * @route /widgetFamily/:id/children + */ +router.get("/:id/children", controller.getWidgetFamilyChildrenByID); + module.exports = router;