Notes/src/services/branches.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-02-18 11:47:32 +02:00
const treeService = require('./tree');
2024-02-16 22:44:12 +02:00
const sql = require('./sql');
2022-02-05 12:06:23 +01:00
function moveBranchToNote(branchToMove, targetParentNoteId) {
if (branchToMove.parentNoteId === targetParentNoteId) {
2022-02-05 12:06:23 +01:00
return {success: true}; // no-op
}
const validationResult = treeService.validateParentChild(targetParentNoteId, branchToMove.noteId, branchToMove.branchId);
2022-02-05 12:06:23 +01:00
if (!validationResult.success) {
return [200, validationResult];
}
const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [targetParentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;
const newBranch = branchToMove.createClone(targetParentNoteId, newNotePos);
2022-02-05 12:06:23 +01:00
newBranch.save();
branchToMove.markAsDeleted();
2022-02-05 12:06:23 +01:00
return {
success: true,
branch: newBranch
};
}
function moveBranchToBranch(branchToMove, targetParentBranch) {
const res = moveBranchToNote(branchToMove, targetParentBranch.noteId);
2022-02-05 12:06:23 +01:00
if (!res.success) {
return res;
}
// expanding so that the new placement of the branch is immediately visible
if (!targetParentBranch.isExpanded) {
targetParentBranch.isExpanded = true;
targetParentBranch.save();
}
2022-02-05 12:06:23 +01:00
return res;
}
module.exports = {
moveBranchToBranch,
moveBranchToNote
};