Merge pull request #11 from kfnawaz/feat/query-endpoints

Feat: Filter endpoints
This commit is contained in:
bluestreamlds
2021-12-30 09:47:57 +05:30
committed by GitHub
6 changed files with 144 additions and 0 deletions

View File

@@ -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);
}
},
};

View File

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

View File

@@ -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);
}
},
};

View File

@@ -16,4 +16,9 @@ router.post("/", controller.createItem);
*/
router.patch("/:id", controller.updateItemByID);
/**
* @route /item/filter
*/
router.get("/filter", controller.getItemsByFilter);
module.exports = router;

View File

@@ -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);
}
},
};

View File

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