From 91ca07929d895580cb329948795002f477210d88 Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 17 Jan 2019 23:24:59 +0100 Subject: [PATCH] before we clone the note we must make sure it's not deleted --- src/services/cloning.js | 18 ++++++++++++++++++ src/services/tree.js | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/src/services/cloning.js b/src/services/cloning.js index bb3c51d52..af987aceb 100644 --- a/src/services/cloning.js +++ b/src/services/cloning.js @@ -8,6 +8,10 @@ const repository = require('./repository'); const Branch = require('../entities/branch'); async function cloneNoteToParent(noteId, parentNoteId, prefix) { + if (await isNoteDeleted(noteId) || await isNoteDeleted(parentNoteId)) { + return { success: false, message: 'Note is deleted.' }; + } + const validationResult = await treeService.validateParentChild(parentNoteId, noteId); if (!validationResult.success) { @@ -27,6 +31,10 @@ async function cloneNoteToParent(noteId, parentNoteId, prefix) { } async function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) { + if (await isNoteDeleted(noteId) || await isNoteDeleted(parentNoteId)) { + return { success: false, message: 'Note is deleted.' }; + } + const validationResult = await treeService.validateParentChild(parentNoteId, noteId); if (!validationResult.success) { @@ -61,6 +69,10 @@ async function toggleNoteInParent(present, noteId, parentNoteId, prefix) { async function cloneNoteAfter(noteId, afterBranchId) { const afterNote = await treeService.getBranch(afterBranchId); + if (await isNoteDeleted(noteId) || await isNoteDeleted(afterNote.parentNoteId)) { + return { success: false, message: 'Note is deleted.' }; + } + const validationResult = await treeService.validateParentChild(afterNote.parentNoteId, noteId); if (!validationResult.result) { @@ -84,6 +96,12 @@ async function cloneNoteAfter(noteId, afterBranchId) { return { success: true }; } +async function isNoteDeleted(noteId) { + const note = await repository.getNote(noteId); + + return note.isDeleted; +} + module.exports = { cloneNoteToParent, ensureNoteIsPresentInParent, diff --git a/src/services/tree.js b/src/services/tree.js index f51089d8b..6e97ed065 100644 --- a/src/services/tree.js +++ b/src/services/tree.js @@ -111,6 +111,12 @@ async function sortNotesAlphabetically(parentNoteId) { } async function setNoteToParent(noteId, prefix, parentNoteId) { + const parentNote = await repository.getNote(parentNoteId); + + if (parentNote.isDeleted) { + throw new Error("Cannot move note to deleted parent note"); + } + // case where there might be more such branches is ignored. It's expected there should be just one const branch = await repository.getEntity("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);