2018-04-03 22:15:28 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('./sql');
|
2020-08-02 23:43:39 +02:00
|
|
|
const eventChangesService = require('./entity_changes.js');
|
2018-08-11 19:45:55 +02:00
|
|
|
const treeService = require('./tree');
|
|
|
|
const noteService = require('./notes');
|
|
|
|
const repository = require('./repository');
|
2018-04-03 22:15:28 -04:00
|
|
|
const Branch = require('../entities/branch');
|
2019-11-07 19:51:38 +01:00
|
|
|
const TaskContext = require("./task_context.js");
|
2020-02-01 16:05:23 +08:00
|
|
|
const utils = require('./utils');
|
2018-04-03 22:15:28 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function cloneNoteToParent(noteId, parentBranchId, prefix) {
|
|
|
|
const parentBranch = repository.getBranch(parentBranchId);
|
2020-05-31 22:33:02 +02:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (isNoteDeleted(noteId) || isNoteDeleted(parentBranch.noteId)) {
|
2019-01-17 23:24:59 +01:00
|
|
|
return { success: false, message: 'Note is deleted.' };
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const validationResult = treeService.validateParentChild(parentBranch.noteId, noteId);
|
2018-04-03 22:15:28 -04:00
|
|
|
|
|
|
|
if (!validationResult.success) {
|
|
|
|
return validationResult;
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const branch = new Branch({
|
2018-04-03 22:15:28 -04:00
|
|
|
noteId: noteId,
|
2020-05-31 22:33:02 +02:00
|
|
|
parentNoteId: parentBranch.noteId,
|
2018-04-03 22:15:28 -04:00
|
|
|
prefix: prefix,
|
|
|
|
isExpanded: 0
|
|
|
|
}).save();
|
|
|
|
|
2020-05-31 22:33:02 +02:00
|
|
|
parentBranch.isExpanded = true; // the new target should be expanded so it immediately shows up to the user
|
2020-06-20 12:31:38 +02:00
|
|
|
parentBranch.save();
|
2018-04-03 22:15:28 -04:00
|
|
|
|
2019-03-18 22:33:19 +01:00
|
|
|
return { success: true, branchId: branch.branchId };
|
2018-04-03 22:15:28 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {
|
|
|
|
if (isNoteDeleted(noteId) || isNoteDeleted(parentNoteId)) {
|
2019-01-17 23:24:59 +01:00
|
|
|
return { success: false, message: 'Note is deleted.' };
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const validationResult = treeService.validateParentChild(parentNoteId, noteId);
|
2018-08-13 10:59:31 +02:00
|
|
|
|
|
|
|
if (!validationResult.success) {
|
|
|
|
return validationResult;
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
new Branch({
|
2018-08-13 10:59:31 +02:00
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
prefix: prefix,
|
|
|
|
isExpanded: 0
|
|
|
|
}).save();
|
2018-08-11 19:45:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {
|
|
|
|
const branch = repository.getEntity(`SELECT * FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]);
|
2018-08-11 19:45:55 +02:00
|
|
|
|
|
|
|
if (branch) {
|
2020-02-01 16:05:23 +08:00
|
|
|
const deleteId = utils.randomString(10);
|
2020-06-20 12:31:38 +02:00
|
|
|
noteService.deleteBranch(branch, deleteId, new TaskContext());
|
2018-08-11 19:45:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function toggleNoteInParent(present, noteId, parentNoteId, prefix) {
|
2018-08-13 13:53:08 +02:00
|
|
|
if (present) {
|
2020-06-20 12:31:38 +02:00
|
|
|
ensureNoteIsPresentInParent(noteId, parentNoteId, prefix);
|
2018-08-13 13:53:08 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-06-20 12:31:38 +02:00
|
|
|
ensureNoteIsAbsentFromParent(noteId, parentNoteId);
|
2018-08-13 13:53:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function cloneNoteAfter(noteId, afterBranchId) {
|
|
|
|
const afterNote = repository.getBranch(afterBranchId);
|
2018-04-03 22:15:28 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (isNoteDeleted(noteId) || isNoteDeleted(afterNote.parentNoteId)) {
|
2019-01-17 23:24:59 +01:00
|
|
|
return { success: false, message: 'Note is deleted.' };
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const validationResult = treeService.validateParentChild(afterNote.parentNoteId, noteId);
|
2018-04-03 22:15:28 -04:00
|
|
|
|
2019-10-26 21:14:06 +02:00
|
|
|
if (!validationResult.success) {
|
2018-04-03 22:15:28 -04:00
|
|
|
return validationResult;
|
|
|
|
}
|
|
|
|
|
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
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
2018-04-03 22:15:28 -04:00
|
|
|
[afterNote.parentNoteId, afterNote.notePosition]);
|
|
|
|
|
2020-08-02 23:43:39 +02:00
|
|
|
eventChangesService.addNoteReorderingEntityChange(afterNote.parentNoteId);
|
2018-04-03 22:15:28 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const branch = new Branch({
|
2018-04-03 22:15:28 -04:00
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: afterNote.parentNoteId,
|
2019-10-19 12:36:16 +02:00
|
|
|
notePosition: afterNote.notePosition + 10,
|
2018-04-03 22:15:28 -04:00
|
|
|
isExpanded: 0
|
|
|
|
}).save();
|
|
|
|
|
2019-03-18 22:33:19 +01:00
|
|
|
return { success: true, branchId: branch.branchId };
|
2018-04-03 22:15:28 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function isNoteDeleted(noteId) {
|
|
|
|
const note = repository.getNote(noteId);
|
2019-01-17 23:24:59 +01:00
|
|
|
|
|
|
|
return note.isDeleted;
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:15:28 -04:00
|
|
|
module.exports = {
|
|
|
|
cloneNoteToParent,
|
2018-08-11 19:45:55 +02:00
|
|
|
ensureNoteIsPresentInParent,
|
|
|
|
ensureNoteIsAbsentFromParent,
|
2018-08-13 13:53:08 +02:00
|
|
|
toggleNoteInParent,
|
2018-04-03 22:15:28 -04:00
|
|
|
cloneNoteAfter
|
2020-05-31 22:33:02 +02:00
|
|
|
};
|