Added: delete sub level tree

This commit is contained in:
Llewellyn D'souza
2021-12-28 17:00:19 +05:30
parent 7a724b8a39
commit 806cfefba3
2 changed files with 36 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
const SubLevel = require("../../models/Sublevel");
const Level = require("../../models/Level");
// exports.moveSublevel = async (sub_level_id, under_sub_or_level_id, isMainLevel) => {
// };
exports.deleteSubLevelTreeFromRoot = async (root_sub_level_id) => {
let sub_level_ids = [];
let temp_sub_level_ids = [root_sub_level_id];
while (temp_sub_level_ids.length > 0) {
const level_sub_level_data = await SubLevel.find({
_id: temp_sub_level_ids,
});
sub_level_ids = [...sub_level_ids, ...temp_sub_level_ids];
temp_sub_level_ids = [];
level_sub_level_data.forEach((sub_level_data) => {
sub_level_data.sub_levels.forEach((sub_level) => {
temp_sub_level_ids.push(sub_level.sub_level_id.toString());
});
});
}
await SubLevel.deleteMany({ _id: sub_level_ids });
console.log("Deleting sub-level tree", { sub_level_ids });
return sub_level_ids;
};

View File

@@ -67,6 +67,12 @@ const schema = new mongoose.Schema(
}
);
schema.pre("save", function (next) {
// const sublevel = this;
// // write validation here?
next();
});
const SubLevel = mongoose.model("SubLevel", schema);
module.exports = SubLevel;