| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							| 
									
										
										
										
											2017-10-15 19:47:05 -04:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							|  |  |  | const utils = require('../../services/utils'); | 
					
						
							|  |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sync_table = require('../../services/sync_table'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 22:47:23 -05:00
										 |  |  | router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |     const noteTreeId = req.params.noteTreeId; | 
					
						
							|  |  |  |     const parentNoteId = req.params.parentNoteId; | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const now = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |         await sql.execute("UPDATE notes_tree SET parent_note_id = ?, note_position = ?, date_modified = ? WHERE note_tree_id = ?", | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |             [parentNoteId, newNotePos, now, noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:57:20 -05:00
										 |  |  | router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |     const noteTreeId = req.params.noteTreeId; | 
					
						
							|  |  |  |     const beforeNoteTreeId = req.params.beforeNoteTreeId; | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const beforeNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [beforeNoteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     if (beforeNote) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |             // we don't change date_modified so other changes are prioritized in case of conflict
 | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |             // also we would have to sync all those modified note trees otherwise hash checks would fail
 | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  |                 [beforeNote.parent_note_id, beforeNote.note_position]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             await sync_table.addNoteReorderingSync(beforeNote.parent_note_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             const now = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-11-09 21:11:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             await sql.execute("UPDATE notes_tree SET parent_note_id = ?, note_position = ?, date_modified = ? WHERE note_tree_id = ?", | 
					
						
							|  |  |  |                 [beforeNote.parent_note_id, beforeNote.note_position, now, noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |         res.send({}); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         res.status(500).send("Before note " + beforeNoteTreeId + " doesn't exist."); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:57:20 -05:00
										 |  |  | router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |     const noteTreeId = req.params.noteTreeId; | 
					
						
							|  |  |  |     const afterNoteTreeId = req.params.afterNoteTreeId; | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const afterNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     if (afterNote) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |             // we don't change date_modified so other changes are prioritized in case of conflict
 | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |             // also we would have to sync all those modified note trees otherwise hash checks would fail
 | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  |                 [afterNote.parent_note_id, afterNote.note_position]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             await sync_table.addNoteReorderingSync(afterNote.parent_note_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 21:11:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             await sql.execute("UPDATE notes_tree SET parent_note_id = ?, note_position = ?, date_modified = ? WHERE note_tree_id = ?", | 
					
						
							|  |  |  |                 [afterNote.parent_note_id, afterNote.note_position + 1, utils.nowDate(), noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |         res.send({}); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         res.status(500).send("After note " + afterNoteTreeId + " doesn't exist."); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 22:47:23 -05:00
										 |  |  | router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |     const parentNoteId = req.params.parentNoteId; | 
					
						
							|  |  |  |     const childNoteId = req.params.childNoteId; | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |     const prefix = req.body.prefix; | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const existing = await sql.getFirstValue('SELECT * FROM notes_tree WHERE note_id = ? AND parent_note_id = ?', [childNoteId, parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     if (existing && !existing.is_deleted) { | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  |         return res.send({ | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |             success: false, | 
					
						
							|  |  |  |             message: 'This note already exists in target parent note.' | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  |     if (!await checkCycle(parentNoteId, childNoteId)) { | 
					
						
							|  |  |  |         return res.send({ | 
					
						
							|  |  |  |             success: false, | 
					
						
							|  |  |  |             message: 'Cloning note here would create cycle.' | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |         const noteTree = { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             note_tree_id: utils.newNoteTreeId(), | 
					
						
							|  |  |  |             note_id: childNoteId, | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             parent_note_id: parentNoteId, | 
					
						
							| 
									
										
										
										
											2017-12-21 21:54:25 -05:00
										 |  |  |             prefix: prefix, | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             note_position: newNotePos, | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             is_expanded: 0, | 
					
						
							|  |  |  |             date_modified: utils.nowDate(), | 
					
						
							|  |  |  |             is_deleted: 0 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.replace("notes_tree", noteTree); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteTreeSync(noteTree.note_tree_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-12-23 08:24:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         await sql.execute("UPDATE notes_tree SET is_expanded = 1 WHERE note_id = ?", [parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |     res.send({ | 
					
						
							|  |  |  |         success: true | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:57:20 -05:00
										 |  |  | router.put('/:noteId/clone-after/:afterNoteTreeId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  |     const afterNoteTreeId = req.params.afterNoteTreeId; | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const afterNote = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     if (!afterNote) { | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  |         return res.status(500).send("After note " + afterNoteTreeId + " doesn't exist."); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |     if (!await checkCycle(afterNote.parent_note_id, noteId)) { | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  |         return res.send({ | 
					
						
							|  |  |  |             success: false, | 
					
						
							|  |  |  |             message: 'Cloning note here would create cycle.' | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const existing = await sql.getFirstValue('SELECT * FROM notes_tree WHERE note_id = ? AND parent_note_id = ?', [noteId, afterNote.parent_note_id]); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     if (existing && !existing.is_deleted) { | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  |         return res.send({ | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |             success: false, | 
					
						
							|  |  |  |             message: 'This note already exists in target parent note.' | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |         // we don't change date_modified so other changes are prioritized in case of conflict
 | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |         // also we would have to sync all those modified note trees otherwise hash checks would fail
 | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  |             [afterNote.parent_note_id, afterNote.note_position]); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |         await sync_table.addNoteReorderingSync(afterNote.parent_note_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |         const noteTree = { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             note_tree_id: utils.newNoteTreeId(), | 
					
						
							|  |  |  |             note_id: noteId, | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             parent_note_id: afterNote.parent_note_id, | 
					
						
							|  |  |  |             note_position: afterNote.note_position + 1, | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             is_expanded: 0, | 
					
						
							|  |  |  |             date_modified: utils.nowDate(), | 
					
						
							|  |  |  |             is_deleted: 0 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.replace("notes_tree", noteTree); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteTreeSync(noteTree.note_tree_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 20:58:13 -05:00
										 |  |  |     res.send({ | 
					
						
							|  |  |  |         success: true | 
					
						
							| 
									
										
										
										
											2017-11-23 20:49:24 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  | async function checkCycle(parentNoteId, childNoteId) { | 
					
						
							|  |  |  |     if (parentNoteId === 'root') { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (parentNoteId === childNoteId) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const parentNoteIds = await sql.getFirstColumn("SELECT DISTINCT parent_note_id FROM notes_tree WHERE note_id = ?", [parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-11-23 21:50:12 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const pid of parentNoteIds) { | 
					
						
							|  |  |  |         if (!await checkCycle(pid, childNoteId)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:57:20 -05:00
										 |  |  | router.put('/:noteTreeId/expanded/:expanded', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |     const noteTreeId = req.params.noteTreeId; | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     const expanded = req.params.expanded; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |         await sql.execute("UPDATE notes_tree SET is_expanded = ? WHERE note_tree_id = ?", [expanded, noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // we don't sync expanded attribute
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |