calculate affected counts and take into account includeDescendants when executing

This commit is contained in:
zadam
2022-06-12 10:35:30 +02:00
parent 63f0e441b9
commit 1bfc5fb77f
3 changed files with 59 additions and 7 deletions

View File

@@ -2,13 +2,47 @@ const becca = require("../../becca/becca");
const bulkActionService = require("../../services/bulk_actions");
function execute(req) {
const {noteIds} = req.body;
const {noteIds, includeDescendants} = req.body;
const affectedNoteIds = getAffectedNoteIds(noteIds, includeDescendants);
const bulkActionNote = becca.getNote('bulkaction');
bulkActionService.executeActions(bulkActionNote, noteIds);
bulkActionService.executeActions(bulkActionNote, affectedNoteIds);
}
function getAffectedNoteCount(req) {
const {noteIds, includeDescendants} = req.body;
const affectedNoteIds = getAffectedNoteIds(noteIds, includeDescendants);
return {
affectedNoteCount: affectedNoteIds.size
};
}
function getAffectedNoteIds(noteIds, includeDescendants) {
const affectedNoteIds = new Set();
for (const noteId of noteIds) {
const note = becca.getNote(noteId);
if (!note) {
continue;
}
affectedNoteIds.add(noteId);
if (includeDescendants) {
for (const descendantNoteId of note.getDescendantNoteIds()) {
affectedNoteIds.add(descendantNoteId);
}
}
}
return affectedNoteIds;
}
module.exports = {
execute
execute,
getAffectedNoteCount
};