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
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
function moveBranchToNote(branchToMove, targetParentNoteId) {
|
|
|
|
if (branchToMove.parentNoteId === targetParentNoteId) {
|
2022-02-05 12:06:23 +01:00
|
|
|
return {success: true}; // no-op
|
|
|
|
}
|
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
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;
|
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
const newBranch = branchToMove.createClone(targetParentNoteId, newNotePos);
|
2022-02-05 12:06:23 +01:00
|
|
|
newBranch.save();
|
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
branchToMove.markAsDeleted();
|
2022-02-05 12:06:23 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
branch: newBranch
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-16 23:31:33 +01:00
|
|
|
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
|
2023-11-16 23:31:33 +01:00
|
|
|
if (!targetParentBranch.isExpanded) {
|
|
|
|
targetParentBranch.isExpanded = true;
|
|
|
|
targetParentBranch.save();
|
|
|
|
}
|
2022-02-05 12:06:23 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
moveBranchToBranch,
|
|
|
|
moveBranchToNote
|
|
|
|
};
|