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-03-31 01:20:36 +08:00
|
|
|
async function cloneNoteToParent(req) {
|
2018-01-14 07:02:41 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const childNoteId = req.params.childNoteId;
|
|
|
|
const prefix = req.body.prefix;
|
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
const validationResult = await tree.validateParentChild(parentNoteId, childNoteId);
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
|
|
|
return 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-03-31 01:20:36 +08:00
|
|
|
const branch = {
|
|
|
|
branchId: utils.newBranchId(),
|
|
|
|
noteId: childNoteId,
|
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
prefix: prefix,
|
|
|
|
notePosition: newNotePos,
|
|
|
|
isExpanded: 0,
|
|
|
|
dateModified: utils.nowDate(),
|
|
|
|
isDeleted: 0
|
|
|
|
};
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
await sql.replace("branches", branch);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
await sync_table.addBranchSync(branch.branchId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
async function cloneNoteAfter(req) {
|
2018-01-14 07:02:41 +08:00
|
|
|
const noteId = req.params.noteId;
|
2018-03-25 09:39:15 +08:00
|
|
|
const afterBranchId = req.params.afterBranchId;
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
const afterNote = await tree.getBranch(afterBranchId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteId);
|
|
|
|
|
|
|
|
if (!validationResult.result) {
|
|
|
|
return validationResult;
|
2018-01-14 07:02:41 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
// we don't change dateModified so other changes are prioritized in case of conflict
|
|
|
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
|
|
|
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-03-31 01:20:36 +08:00
|
|
|
const branch = {
|
|
|
|
branchId: utils.newBranchId(),
|
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: afterNote.parentNoteId,
|
|
|
|
notePosition: afterNote.notePosition + 1,
|
|
|
|
isExpanded: 0,
|
|
|
|
dateModified: utils.nowDate(),
|
|
|
|
isDeleted: 0
|
|
|
|
};
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
await sql.replace("branches", branch);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
await sync_table.addBranchSync(branch.branchId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-03-31 01:20:36 +08:00
|
|
|
module.exports = {
|
|
|
|
cloneNoteToParent,
|
|
|
|
cloneNoteAfter
|
|
|
|
};
|