2018-01-14 07:02:41 +08: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-14 10:32:29 +08:00
|
|
|
const notes = require('../../services/notes');
|
2018-04-01 11:08:22 +08:00
|
|
|
const repository = require('../../services/repository');
|
2018-01-14 07:02:41 +08:00
|
|
|
|
|
|
|
/**
|
2018-04-04 10:15:28 +08: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-14 07:02:41 +08:00
|
|
|
*/
|
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function moveBranchToParent(req) {
|
2018-03-25 09:39:15 +08:00
|
|
|
const branchId = req.params.branchId;
|
2018-01-14 07:02:41 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
const noteToMove = await tree.getBranch(branchId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
const validationResult = await tree.validateParentChild(parentNoteId, noteToMove.noteId, branchId);
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
2018-10-22 04:42:20 +08:00
|
|
|
return [200, validationResult];
|
2018-01-14 07:02:41 +08:00
|
|
|
}
|
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
2018-01-14 07:02:41 +08:00
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
|
|
|
|
2018-04-02 00:03:21 +08:00
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.parentNoteId = parentNoteId;
|
|
|
|
branch.notePosition = newNotePos;
|
|
|
|
await branch.save();
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function moveBranchBeforeNote(req) {
|
2018-03-25 09:39:15 +08:00
|
|
|
const branchId = req.params.branchId;
|
|
|
|
const beforeBranchId = req.params.beforeBranchId;
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
const noteToMove = await tree.getBranch(branchId);
|
|
|
|
const beforeNote = await tree.getBranch(beforeBranchId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
const validationResult = await tree.validateParentChild(beforeNote.parentNoteId, noteToMove.noteId, branchId);
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
2018-10-22 04:42:20 +08:00
|
|
|
return [200, validationResult];
|
2018-01-14 07:02:41 +08:00
|
|
|
}
|
|
|
|
|
2019-03-13 03:58:31 +08:00
|
|
|
// we don't change utcDateModified so other changes are prioritized in case of conflict
|
2018-04-04 10:15:28 +08:00
|
|
|
// also we would have to sync all those modified branches otherwise hash checks would fail
|
2018-03-31 00:57:22 +08:00
|
|
|
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
|
|
|
|
[beforeNote.parentNoteId, beforeNote.notePosition]);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-04-02 00:03:21 +08:00
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.parentNoteId = beforeNote.parentNoteId;
|
|
|
|
branch.notePosition = beforeNote.notePosition;
|
|
|
|
await branch.save();
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function moveBranchAfterNote(req) {
|
2018-03-25 09:39:15 +08:00
|
|
|
const branchId = req.params.branchId;
|
|
|
|
const afterBranchId = req.params.afterBranchId;
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
const noteToMove = await tree.getBranch(branchId);
|
|
|
|
const afterNote = await tree.getBranch(afterBranchId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteToMove.noteId, branchId);
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
2018-10-22 04:42:20 +08:00
|
|
|
return [200, validationResult];
|
2018-01-14 07:02:41 +08:00
|
|
|
}
|
|
|
|
|
2019-03-13 03:58:31 +08:00
|
|
|
// we don't change utcDateModified so other changes are prioritized in case of conflict
|
2018-04-04 10:15:28 +08:00
|
|
|
// also we would have to sync all those modified branches otherwise hash checks would fail
|
2018-03-31 00:57:22 +08:00
|
|
|
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
|
|
|
[afterNote.parentNoteId, afterNote.notePosition]);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-04-02 00:03:21 +08:00
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.parentNoteId = afterNote.parentNoteId;
|
|
|
|
branch.notePosition = afterNote.notePosition + 1;
|
|
|
|
await branch.save();
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function setExpanded(req) {
|
2018-03-25 09:39:15 +08:00
|
|
|
const branchId = req.params.branchId;
|
2018-01-14 07:02:41 +08:00
|
|
|
const expanded = req.params.expanded;
|
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
await sql.execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
|
|
|
|
// we don't sync expanded label
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteBranch(req) {
|
2018-04-01 11:08:22 +08:00
|
|
|
const branch = await repository.getBranch(req.params.branchId);
|
|
|
|
|
2019-05-21 03:50:01 +08:00
|
|
|
return {
|
|
|
|
noteDeleted: await notes.deleteNote(branch)
|
|
|
|
};
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:33:10 +08: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-31 00:57:22 +08:00
|
|
|
module.exports = {
|
|
|
|
moveBranchToParent,
|
|
|
|
moveBranchBeforeNote,
|
|
|
|
moveBranchAfterNote,
|
|
|
|
setExpanded,
|
2018-04-02 08:33:10 +08:00
|
|
|
deleteBranch,
|
|
|
|
setPrefix
|
2018-03-31 00:57:22 +08:00
|
|
|
};
|