| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const options = require('./options'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							|  |  |  | const notes = require('./notes'); | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  | const data_encryption = require('./data_encryption'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sync_table = require('./sync_table'); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function createNewNote(parentNoteId, note, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     const noteId = utils.newNoteId(); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     const noteTreeId = utils.newNoteTreeId(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let newNotePos = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |         if (note.target === 'into') { | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             const maxNotePos = await sql.getSingleValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |             newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (note.target === 'after') { | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             const afterNote = await sql.getSingleResult('SELECT note_position FROM notes_tree WHERE note_tree_id = ?', [note.target_note_tree_id]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             newNotePos = afterNote.note_position + 1; | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |             // not updating date_modified to avoig having to sync whole rows
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             await sql.execute('UPDATE notes_tree SET note_position = note_position + 1 WHERE parent_note_id = ? AND note_position > ? AND is_deleted = 0', | 
					
						
							|  |  |  |                 [parentNoteId, afterNote.note_position]); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             await sync_table.addNoteReorderingSync(parentNoteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-12-06 20:58:59 -05:00
										 |  |  |             throw new Error('Unknown target: ' + note.target); | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |         const now = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.insert("notes", { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             note_id: noteId, | 
					
						
							|  |  |  |             note_title: note.note_title, | 
					
						
							|  |  |  |             note_text: '', | 
					
						
							|  |  |  |             date_created: now, | 
					
						
							|  |  |  |             date_modified: now, | 
					
						
							|  |  |  |             is_protected: note.is_protected | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.insert("notes_tree", { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             note_tree_id: noteTreeId, | 
					
						
							|  |  |  |             note_id: noteId, | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             parent_note_id: parentNoteId, | 
					
						
							|  |  |  |             note_position: newNotePos, | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             is_expanded: 0, | 
					
						
							|  |  |  |             date_modified: now, | 
					
						
							|  |  |  |             is_deleted: 0 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         noteId, | 
					
						
							|  |  |  |         noteTreeId | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  | async function encryptNote(note, dataKey) { | 
					
						
							|  |  |  |     note.detail.note_title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(note.detail.note_id), note.detail.note_title); | 
					
						
							|  |  |  |     note.detail.note_text = data_encryption.encrypt(dataKey, data_encryption.noteTextIv(note.detail.note_id), note.detail.note_text); | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     const note = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     await protectNote(note, dataKey, protect, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |     const children = await sql.getFlattenedResults("SELECT note_id FROM notes_tree WHERE parent_note_id = ?", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const childNoteId of children) { | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await protectNoteRecursively(childNoteId, dataKey, protect, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 23:39:43 -05:00
										 |  |  | function decryptNote(note, dataKey) { | 
					
						
							|  |  |  |     note.note_title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title); | 
					
						
							|  |  |  |     note.note_text = data_encryption.decryptString(dataKey, data_encryption.noteTextIv(note.note_id), note.note_text); | 
					
						
							|  |  |  |     note.is_protected = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function protectNote(note, dataKey, protect, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     let changed = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (protect && !note.is_protected) { | 
					
						
							| 
									
										
										
										
											2017-11-18 12:53:17 -05:00
										 |  |  |         note.note_title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title); | 
					
						
							|  |  |  |         note.note_text = data_encryption.encrypt(dataKey, data_encryption.noteTextIv(note.note_id), note.note_text); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |         note.is_protected = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         changed = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (!protect && note.is_protected) { | 
					
						
							| 
									
										
										
										
											2017-12-06 23:39:43 -05:00
										 |  |  |         decryptNote(note, dataKey); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         changed = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (changed) { | 
					
						
							|  |  |  |         console.log("Updating..."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ? WHERE note_id = ?", | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             [note.note_title, note.note_text, note.is_protected, note.note_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteSync(note.note_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     await protectNoteHistory(note.note_id, dataKey, protect, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function protectNoteHistory(noteId, dataKey, protect, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     const historyToChange = await sql.getResults("SELECT * FROM notes_history WHERE note_id = ? AND is_protected != ?", [noteId, protect]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const history of historyToChange) { | 
					
						
							|  |  |  |         if (protect) { | 
					
						
							| 
									
										
										
										
											2017-11-18 12:53:17 -05:00
										 |  |  |             history.note_title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(history.note_history_id), history.note_title); | 
					
						
							|  |  |  |             history.note_text = data_encryption.encrypt(dataKey, data_encryption.noteTextIv(history.note_history_id), history.note_text); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             history.is_protected = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-11-18 12:53:17 -05:00
										 |  |  |             history.note_title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(history.note_history_id), history.note_title); | 
					
						
							|  |  |  |             history.note_text = data_encryption.decryptString(dataKey, data_encryption.noteTextIv(history.note_history_id), history.note_text); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             history.is_protected = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes_history SET note_title = ?, note_text = ?, is_protected = ? WHERE note_history_id = ?", | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             [history.note_title, history.note_text, history.is_protected, history.note_history_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteHistorySync(history.note_history_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  | async function updateNote(noteId, newNote, dataKey, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |     if (newNote.detail.is_protected) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |         await encryptNote(newNote, dataKey); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const now = new Date(); | 
					
						
							| 
									
										
										
										
											2017-12-13 23:27:02 -05:00
										 |  |  |     const nowStr = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000)); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const existingNoteHistoryId = await sql.getSingleValue( | 
					
						
							|  |  |  |         "SELECT note_history_id FROM notes_history WHERE note_id = ? AND date_modified_to >= ?", [noteId, historyCutoff]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |         const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.date_created).getTime(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!existingNoteHistoryId && msSinceDateCreated >= historySnapshotTimeInterval * 1000) { | 
					
						
							| 
									
										
										
										
											2017-12-06 22:31:28 -05:00
										 |  |  |             const oldNote = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 23:39:43 -05:00
										 |  |  |             if (oldNote.is_protected) { | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |                 decryptNote(oldNote, dataKey); | 
					
						
							| 
									
										
										
										
											2017-12-06 23:39:43 -05:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |             const newNoteHistoryId = utils.newNoteHistoryId(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |             await sql.insert('notes_history', { | 
					
						
							| 
									
										
										
										
											2017-11-20 23:51:28 -05:00
										 |  |  |                 note_history_id: newNoteHistoryId, | 
					
						
							|  |  |  |                 note_id: noteId, | 
					
						
							| 
									
										
										
										
											2017-12-06 23:39:43 -05:00
										 |  |  |                 // title and text should be decrypted now
 | 
					
						
							| 
									
										
										
										
											2017-12-06 22:31:28 -05:00
										 |  |  |                 note_title: oldNote.note_title, | 
					
						
							|  |  |  |                 note_text: oldNote.note_text, | 
					
						
							| 
									
										
										
										
											2017-12-06 23:39:43 -05:00
										 |  |  |                 is_protected: 0, // will be fixed in the protectNoteHistory() call
 | 
					
						
							| 
									
										
										
										
											2017-12-06 22:31:28 -05:00
										 |  |  |                 date_modified_from: oldNote.date_modified, | 
					
						
							| 
									
										
										
										
											2017-12-13 23:27:02 -05:00
										 |  |  |                 date_modified_to: nowStr | 
					
						
							| 
									
										
										
										
											2017-11-20 23:51:28 -05:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2017-11-16 00:22:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |             await sync_table.addNoteHistorySync(newNoteHistoryId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |         await protectNoteHistory(noteId, dataKey, newNote.detail.is_protected); | 
					
						
							| 
									
										
										
										
											2017-11-14 22:21:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [ | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |             newNote.detail.note_title, | 
					
						
							|  |  |  |             newNote.detail.note_text, | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |             newNote.detail.is_protected, | 
					
						
							| 
									
										
										
										
											2017-12-13 23:27:02 -05:00
										 |  |  |             nowStr, | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |             noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |         await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function deleteNote(noteTreeId, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const now = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.execute("UPDATE notes_tree SET is_deleted = 1, date_modified = ? WHERE note_tree_id = ?", [now, noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  |     const noteId = await sql.getSingleValue("SELECT note_id FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 23:54:54 -05:00
										 |  |  |     const notDeletedNoteTreesCount = await sql.getSingleValue("SELECT COUNT(*) FROM notes_tree WHERE note_id = ? AND is_deleted = 0", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!notDeletedNoteTreesCount) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET is_deleted = 1, date_modified = ? WHERE note_id = ?", [now, noteId]); | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |         const children = await sql.getResults("SELECT note_tree_id FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  |         for (const child of children) { | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             await deleteNote(child.note_tree_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     createNewNote, | 
					
						
							|  |  |  |     updateNote, | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     deleteNote, | 
					
						
							|  |  |  |     protectNoteRecursively | 
					
						
							| 
									
										
										
										
											2017-11-05 17:58:55 -05:00
										 |  |  | }; |