|
|
|
@ -1,28 +1,25 @@
|
|
|
|
"use strict";
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const auth = require('../../services/auth');
|
|
|
|
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sync_table = require('../../services/sync_table');
|
|
|
|
const sync_table = require('../../services/sync_table');
|
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
|
|
|
|
|
|
|
const tree = require('../../services/tree');
|
|
|
|
const tree = require('../../services/tree');
|
|
|
|
|
|
|
|
|
|
|
|
router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
async function cloneNoteToParent(req) {
|
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const childNoteId = req.params.childNoteId;
|
|
|
|
const childNoteId = req.params.childNoteId;
|
|
|
|
const prefix = req.body.prefix;
|
|
|
|
const prefix = req.body.prefix;
|
|
|
|
const sourceId = req.headers.source_id;
|
|
|
|
const sourceId = req.headers.source_id;
|
|
|
|
|
|
|
|
|
|
|
|
if (!await tree.validateParentChild(res, parentNoteId, childNoteId)) {
|
|
|
|
const validationResult = await tree.validateParentChild(parentNoteId, childNoteId);
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (!validationResult.success) {
|
|
|
|
|
|
|
|
return validationResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
|
|
|
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
|
|
|
|
|
|
|
|
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
|
|
|
|
const branch = {
|
|
|
|
const branch = {
|
|
|
|
branchId: utils.newBranchId(),
|
|
|
|
branchId: utils.newBranchId(),
|
|
|
|
noteId: childNoteId,
|
|
|
|
noteId: childNoteId,
|
|
|
|
@ -39,23 +36,23 @@ router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async
|
|
|
|
await sync_table.addBranchSync(branch.branchId, sourceId);
|
|
|
|
await sync_table.addBranchSync(branch.branchId, sourceId);
|
|
|
|
|
|
|
|
|
|
|
|
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
|
|
|
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.send({ success: true });
|
|
|
|
return { success: true };
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
router.put('/:noteId/clone-after/:afterBranchId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
async function cloneNoteAfter(req) {
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
const afterBranchId = req.params.afterBranchId;
|
|
|
|
const afterBranchId = req.params.afterBranchId;
|
|
|
|
const sourceId = req.headers.source_id;
|
|
|
|
const sourceId = req.headers.source_id;
|
|
|
|
|
|
|
|
|
|
|
|
const afterNote = await tree.getBranch(afterBranchId);
|
|
|
|
const afterNote = await tree.getBranch(afterBranchId);
|
|
|
|
|
|
|
|
|
|
|
|
if (!await tree.validateParentChild(res, afterNote.parentNoteId, noteId)) {
|
|
|
|
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteId);
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (!validationResult.result) {
|
|
|
|
|
|
|
|
return validationResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
|
|
|
|
// we don't change dateModified so other changes are prioritized in case of conflict
|
|
|
|
// 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
|
|
|
|
// 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",
|
|
|
|
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
|
|
|
@ -76,9 +73,11 @@ router.put('/:noteId/clone-after/:afterBranchId', auth.checkApiAuth, wrap(async
|
|
|
|
await sql.replace("branches", branch);
|
|
|
|
await sql.replace("branches", branch);
|
|
|
|
|
|
|
|
|
|
|
|
await sync_table.addBranchSync(branch.branchId, sourceId);
|
|
|
|
await sync_table.addBranchSync(branch.branchId, sourceId);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.send({ success: true });
|
|
|
|
return { success: true };
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
cloneNoteToParent,
|
|
|
|
|
|
|
|
cloneNoteAfter
|
|
|
|
|
|
|
|
};
|