From 4c0315d2bfc5c88e07837ce043066915a19c7500 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 19 Nov 2017 23:12:39 -0500 Subject: [PATCH] fix deleting notes --- public/javascripts/tree_changes.js | 110 ++++++++++++++--------------- routes/api/notes.js | 4 +- services/notes.js | 29 ++++---- 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/public/javascripts/tree_changes.js b/public/javascripts/tree_changes.js index fd56682b9..84f310841 100644 --- a/public/javascripts/tree_changes.js +++ b/public/javascripts/tree_changes.js @@ -1,80 +1,78 @@ "use strict"; const treeChanges = (function() { - function moveBeforeNode(node, beforeNode) { - $.ajax({ + async function moveBeforeNode(node, beforeNode) { + await $.ajax({ url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveBefore/' + beforeNode.data.note_tree_id, type: 'PUT', - contentType: "application/json", - success: () => { - node.moveTo(beforeNode, 'before'); - - noteTree.setCurrentNotePathToHash(node); - } + contentType: "application/json" }); + + node.moveTo(beforeNode, 'before'); + + noteTree.setCurrentNotePathToHash(node); } - function moveAfterNode(node, afterNode) { - $.ajax({ + async function moveAfterNode(node, afterNode) { + await $.ajax({ url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + afterNode.data.note_tree_id, type: 'PUT', - contentType: "application/json", - success: () => { - node.moveTo(afterNode, 'after'); - - noteTree.setCurrentNotePathToHash(node); - } + contentType: "application/json" }); + + node.moveTo(afterNode, 'after'); + + noteTree.setCurrentNotePathToHash(node); } - function moveToNode(node, toNode) { - $.ajax({ + async function moveToNode(node, toNode) { + await $.ajax({ url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveTo/' + toNode.data.note_id, type: 'PUT', - contentType: "application/json", - success: () => { - node.moveTo(toNode); - - toNode.setExpanded(true); - - toNode.folder = true; - toNode.renderTitle(); - - noteTree.setCurrentNotePathToHash(node); - } + contentType: "application/json" }); + + node.moveTo(toNode); + + toNode.setExpanded(true); + + toNode.folder = true; + toNode.renderTitle(); + + noteTree.setCurrentNotePathToHash(node); } - function deleteNode(node) { - if (confirm('Are you sure you want to delete note "' + node.title + '"?')) { - $.ajax({ - url: baseApiUrl + 'notes/' + node.key, - type: 'DELETE', - success: () => { - if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { - node.getParent().folder = false; - node.getParent().renderTitle(); - } - - recentNotes.removeRecentNote(node.note_tree_id); - - let next = node.getNextSibling(); - if (!next) { - next = node.getParent(); - } - - node.remove(); - - // activate next element after this one is deleted so we don't lose focus - next.setActive(); - - noteTree.setCurrentNotePathToHash(next); - } - }); + async function deleteNode(node) { + if (!confirm('Are you sure you want to delete note "' + node.title + '"?')) { + return; } + + await $.ajax({ + url: baseApiUrl + 'notes/' + node.data.note_tree_id, + type: 'DELETE' + }); + + if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { + node.getParent().folder = false; + node.getParent().renderTitle(); + } + + recentNotes.removeRecentNote(node.note_tree_id); + + let next = node.getNextSibling(); + if (!next) { + next = node.getParent(); + } + + node.remove(); + + // activate next element after this one is deleted so we don't lose focus + next.setActive(); + + noteTree.setCurrentNotePathToHash(next); } - function moveNodeUp(node) { + async function moveNodeUp(node) { if (node.getParent() !== null) { $.ajax({ url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + node.getParent().data.note_tree_id, diff --git a/routes/api/notes.js b/routes/api/notes.js index 7e775c0f2..5f28c5a3f 100644 --- a/routes/api/notes.js +++ b/routes/api/notes.js @@ -52,11 +52,11 @@ router.put('/:noteId', async (req, res, next) => { res.send({}); }); -router.delete('/:noteId', async (req, res, next) => { +router.delete('/:noteTreeId', async (req, res, next) => { const browserId = utils.browserId(req); await sql.doInTransaction(async () => { - await notes.deleteNote(req.params.noteId, browserId); + await notes.deleteNote(req.params.noteTreeId, browserId); }); res.send({}); diff --git a/services/notes.js b/services/notes.js index 26cc454b1..803784683 100644 --- a/services/notes.js +++ b/services/notes.js @@ -218,22 +218,27 @@ async function addNoteAudits(origNote, newNote, browserId) { } -async function deleteNote(noteId, browserId) { +async function deleteNote(noteTreeId, browserId) { const now = utils.nowTimestamp(); + await sql.execute("update notes_tree set is_deleted = 1, date_modified = ? where note_tree_id = ?", [now, noteTreeId]); + await sync_table.addNoteTreeSync(noteTreeId); - const children = await sql.getResults("select note_id from notes_tree where note_pid = ? and is_deleted = 0", [noteId]); + const noteId = await sql.getSingleValue("SELECT note_id FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); - for (const child of children) { - await deleteNote(child.note_id, browserId); + const notDeletedNoteTreesCount = await sql.getSingleValue("SELECT COUNT(*) FROM notes_tree WHERE note_id = ?", [noteId]); + + if (!notDeletedNoteTreesCount) { + await sql.execute("update notes set is_deleted = 1, date_modified = ? where note_id = ?", [now, noteTreeId]); + await sync_table.addNoteSync(noteTreeId); + + const children = await sql.getResults("select note_tree_id from notes_tree where note_pid = ? and is_deleted = 0", [noteTreeId]); + + for (const child of children) { + await deleteNote(child.note_tree_id, browserId); + } + + await sql.addAudit(audit_category.DELETE_NOTE, browserId, noteTreeId); } - - await sql.execute("update notes_tree set is_deleted = 1, date_modified = ? where note_id = ?", [now, noteId]); - await sql.execute("update notes set is_deleted = 1, date_modified = ? where note_id = ?", [now, noteId]); - - await sync_table.addNoteTreeSync(noteId); - await sync_table.addNoteSync(noteId); - - await sql.addAudit(audit_category.DELETE_NOTE, browserId, noteId); } module.exports = {