Fixed: sublevel queries and updates

This commit is contained in:
Llewellyn D'souza
2022-02-17 18:14:25 +05:30
parent 52d020717c
commit 0c8608d0b6
3 changed files with 15 additions and 3 deletions

View File

@@ -356,7 +356,18 @@ module.exports = {
const { id, type } = req.body;
if (!id || !type) return res.send({ success: false, message: "Missing id or type" });
const query = {};
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;
}
query[`${type}_id`] = id;
const childrenData = await getChildModel(type).find(query);

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;
}
req.send(sublevelData);
res.send(sublevelData);
} catch (error) {
next(error);
}

View File

@@ -58,7 +58,7 @@ const deleteSubLevelTreeFromRoot = async (root_sub_level_id) => {
const addSublevelToParent = async (payload, parent_id, parentIsLevel) => {
if (parentIsLevel) {
// add sublevel to parent
const parentData = await Sublevel.findById(parent_id);
const parentData = await Level.findById(parent_id);
parentData.sub_levels.push(payload);
return await parentData.save();
} else {