2018-01-13 18:02:41 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
2018-04-01 21:27:46 -04:00
|
|
|
const syncTable = require('../../services/sync_table');
|
2018-01-13 18:02:41 -05:00
|
|
|
const tree = require('../../services/tree');
|
2018-04-01 11:05:09 -04:00
|
|
|
const Branch = require('../../entities/branch');
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
async function cloneNoteToParent(req) {
|
2018-04-01 20:33:10 -04:00
|
|
|
const noteId = req.params.noteId;
|
2018-01-13 18:02:41 -05:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const prefix = req.body.prefix;
|
|
|
|
|
2018-04-01 20:33:10 -04:00
|
|
|
const validationResult = await tree.validateParentChild(parentNoteId, noteId);
|
2018-03-30 13:20:36 -04:00
|
|
|
|
|
|
|
if (!validationResult.success) {
|
|
|
|
return 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]);
|
2018-01-13 18:02:41 -05:00
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
|
|
|
|
2018-04-02 22:53:01 -04:00
|
|
|
const branch = await new Branch({
|
2018-04-01 20:33:10 -04:00
|
|
|
noteId: noteId,
|
2018-03-30 13:20:36 -04:00
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
prefix: prefix,
|
|
|
|
notePosition: newNotePos,
|
2018-04-01 17:38:24 -04:00
|
|
|
isExpanded: 0
|
2018-04-02 22:53:01 -04:00
|
|
|
}).save();
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
async function cloneNoteAfter(req) {
|
2018-01-13 18:02:41 -05:00
|
|
|
const noteId = req.params.noteId;
|
2018-03-24 21:39:15 -04:00
|
|
|
const afterBranchId = req.params.afterBranchId;
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const afterNote = await tree.getBranch(afterBranchId);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteId);
|
|
|
|
|
|
|
|
if (!validationResult.result) {
|
|
|
|
return validationResult;
|
2018-01-13 18:02:41 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 13:20:36 -04: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-13 18:02:41 -05:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncTable.addNoteReorderingSync(afterNote.parentNoteId);
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-04-02 22:53:01 -04:00
|
|
|
const branch = await new Branch({
|
2018-03-30 13:20:36 -04:00
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: afterNote.parentNoteId,
|
|
|
|
notePosition: afterNote.notePosition + 1,
|
2018-04-01 17:38:24 -04:00
|
|
|
isExpanded: 0
|
2018-04-02 22:53:01 -04:00
|
|
|
}).save();
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
return { success: true };
|
|
|
|
}
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2018-03-30 13:20:36 -04:00
|
|
|
module.exports = {
|
|
|
|
cloneNoteToParent,
|
|
|
|
cloneNoteAfter
|
|
|
|
};
|