2018-01-13 18:02:41 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sync_table = require('../../services/sync_table');
|
|
|
|
const tree = require('../../services/tree');
|
2018-01-13 21:32:29 -05:00
|
|
|
const notes = require('../../services/notes');
|
2018-03-31 23:08:22 -04:00
|
|
|
const repository = require('../../services/repository');
|
2019-10-18 23:19:16 +02:00
|
|
|
const TaskContext = require('../../services/task_context');
|
2018-01-13 18:02:41 -05:00
|
|
|
|
|
|
|
/**
|
2018-04-03 22:15:28 -04:00
|
|
|
* Code in this file deals with moving and cloning branches. Relationship between note and parent note is unique
|
|
|
|
* for not deleted branches. There may be multiple deleted note-parent note relationships.
|
2018-01-13 18:02:41 -05:00
|
|
|
*/
|
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function moveBranchToParent(req) {
|
2019-10-19 12:36:16 +02:00
|
|
|
const {branchId, parentNoteId} = req.params;
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const noteToMove = await tree.getBranch(branchId);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
const validationResult = await tree.validateParentChild(parentNoteId, noteToMove.noteId, branchId);
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
2018-10-21 22:42:20 +02:00
|
|
|
return [200, validationResult];
|
2018-01-13 18:02:41 -05:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
2019-10-19 12:36:16 +02:00
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-04-01 12:03:21 -04:00
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.parentNoteId = parentNoteId;
|
|
|
|
branch.notePosition = newNotePos;
|
|
|
|
await branch.save();
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function moveBranchBeforeNote(req) {
|
2019-10-19 12:36:16 +02:00
|
|
|
const {branchId, beforeBranchId} = req.params;
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const noteToMove = await tree.getBranch(branchId);
|
|
|
|
const beforeNote = await tree.getBranch(beforeBranchId);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
const validationResult = await tree.validateParentChild(beforeNote.parentNoteId, noteToMove.noteId, branchId);
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
2018-10-21 22:42:20 +02:00
|
|
|
return [200, validationResult];
|
2018-01-13 18:02:41 -05:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:58:31 +01:00
|
|
|
// we don't change utcDateModified so other changes are prioritized in case of conflict
|
2018-04-03 22:15:28 -04:00
|
|
|
// also we would have to sync all those modified branches otherwise hash checks would fail
|
2019-10-19 12:36:16 +02:00
|
|
|
await sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
|
2018-03-30 12:57:22 -04:00
|
|
|
[beforeNote.parentNoteId, beforeNote.notePosition]);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-04-01 12:03:21 -04:00
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.parentNoteId = beforeNote.parentNoteId;
|
|
|
|
branch.notePosition = beforeNote.notePosition;
|
|
|
|
await branch.save();
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function moveBranchAfterNote(req) {
|
2019-10-19 12:36:16 +02:00
|
|
|
const {branchId, afterBranchId} = req.params;
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const noteToMove = await tree.getBranch(branchId);
|
|
|
|
const afterNote = await tree.getBranch(afterBranchId);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteToMove.noteId, branchId);
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
2018-10-21 22:42:20 +02:00
|
|
|
return [200, validationResult];
|
2018-01-13 18:02:41 -05:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:58:31 +01:00
|
|
|
// we don't change utcDateModified so other changes are prioritized in case of conflict
|
2018-04-03 22:15:28 -04:00
|
|
|
// also we would have to sync all those modified branches otherwise hash checks would fail
|
2019-10-19 12:36:16 +02:00
|
|
|
await sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
2018-03-30 12:57:22 -04:00
|
|
|
[afterNote.parentNoteId, afterNote.notePosition]);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-04-01 12:03:21 -04:00
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.parentNoteId = afterNote.parentNoteId;
|
2019-10-19 12:36:16 +02:00
|
|
|
branch.notePosition = afterNote.notePosition + 10;
|
2018-04-01 12:03:21 -04:00
|
|
|
await branch.save();
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function setExpanded(req) {
|
2019-10-19 12:36:16 +02:00
|
|
|
const {branchId, expanded} = req.params;
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
await sql.execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
|
|
|
|
// we don't sync expanded label
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteBranch(req) {
|
2019-10-19 00:11:07 +02:00
|
|
|
const last = req.query.last === 'true';
|
2018-03-31 23:08:22 -04:00
|
|
|
const branch = await repository.getBranch(req.params.branchId);
|
2019-10-18 23:19:16 +02:00
|
|
|
const taskContext = TaskContext.getInstance(req.query.taskId, 'delete-notes');
|
2018-03-31 23:08:22 -04:00
|
|
|
|
2019-10-19 00:11:07 +02:00
|
|
|
const noteDeleted = await notes.deleteBranch(branch, taskContext);
|
|
|
|
|
|
|
|
if (last) {
|
|
|
|
taskContext.taskSucceeded();
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:50:01 +02:00
|
|
|
return {
|
2019-10-19 00:11:07 +02:00
|
|
|
noteDeleted: noteDeleted
|
2019-05-20 21:50:01 +02:00
|
|
|
};
|
2018-03-30 12:57:22 -04:00
|
|
|
}
|
|
|
|
|
2018-04-01 20:33:10 -04:00
|
|
|
async function setPrefix(req) {
|
|
|
|
const branchId = req.params.branchId;
|
|
|
|
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
|
|
|
|
|
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.prefix = prefix;
|
|
|
|
await branch.save();
|
|
|
|
}
|
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
module.exports = {
|
|
|
|
moveBranchToParent,
|
|
|
|
moveBranchBeforeNote,
|
|
|
|
moveBranchAfterNote,
|
|
|
|
setExpanded,
|
2018-04-01 20:33:10 -04:00
|
|
|
deleteBranch,
|
|
|
|
setPrefix
|
2018-03-30 12:57:22 -04:00
|
|
|
};
|