From e04eac955e14ca9d8f3be28c95bb9b98805d4357 Mon Sep 17 00:00:00 2001 From: Sathishkumar Krishnan Date: Tue, 25 Jan 2022 15:14:50 +0530 Subject: [PATCH] feat: added widgetfamily details to inventory query & widgetfamily children get api --- src/controller/inventory.controller.js | 6 ++++++ src/controller/widgetFamily.controller.js | 23 +++++++++++++++++++++++ src/controller/widgetFamily.router.js | 5 +++++ 3 files changed, 34 insertions(+) diff --git a/src/controller/inventory.controller.js b/src/controller/inventory.controller.js index 35fd891..f1a1e81 100644 --- a/src/controller/inventory.controller.js +++ b/src/controller/inventory.controller.js @@ -1,4 +1,5 @@ const Inventory = require("../models/Inventory"); +const WidgetFamily = require("../models/WidgetFamily"); const { InventoryTypes } = require("../config/constants"); module.exports = { @@ -162,6 +163,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/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;