Merge branch 'fix/apis-1' of github.com:kfnawaz/plaidware-wms-core into fix/apis-1

This commit is contained in:
Sathishkumar Krishnan
2022-02-27 13:00:07 +05:30
2 changed files with 27 additions and 4 deletions

View File

@@ -356,10 +356,32 @@ module.exports = {
const { id, type } = req.body;
if (!id || !type) return res.send({ success: false, message: "Missing id or type" });
const query = {};
query[`${type}_id`] = id;
let query = {};
switch (type) {
case "level":
case "sublevel":
query = { $or: [{ main_level_id: id, parent_sublevel_id: id }] };
break;
default:
query[`${type}_id`] = id;
break;
}
let childrenData = await getChildModel(type).find(query);
// populate locations to sublevel
if (childrenData && ["level", "sublevel"].includes(type)) {
const parentData = type === "level" ? await Level.findById(id) : await Sublevel.findById(id);
childrenData =
parentData &&
childrenData.map((child) => ({
...child?._doc,
positions: parentData.sub_levels?.find((t2) => t2.sub_level_id.toString() === child._id.toString())?.positions,
}));
}
const childrenData = await getChildModel(type).find(query);
res.send({ success: true, data: { parent: { id, type }, childrenData } });
} catch (error) {
next(error);

View File

@@ -31,6 +31,7 @@ module.exports = {
* Create a sublevel
*/
createSubLevel: async (req, res, next) => {
console.log(req.body);
const { name, type, specs, parent_id, parentIsLevel, positions } = req.body;
if (!(name && type && parent_id && positions)) {
@@ -70,7 +71,7 @@ module.exports = {
res.status(404);
return;
}
res.send({ success: true, data: sublevelData });
res.send({ ...sublevelData?._doc, positions });
} catch (error) {
next(error);
}