| 
									
										
										
										
											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 audit_category = require('../../services/audit_category'); | 
					
						
							|  |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  | router.put('/:noteId/moveTo/:parentId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     let noteId = req.params.noteId; | 
					
						
							|  |  |  |     let parentId = req.params.parentId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 |  |  |     const row = await sql.getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ? and is_deleted = 0', [parentId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     const maxNotePos = row['max_note_pos']; | 
					
						
							|  |  |  |     let newNotePos = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (maxNotePos === null)  // no children yet
 | 
					
						
							|  |  |  |         newNotePos = 0; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         newNotePos = maxNotePos + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     const now = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |         await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", | 
					
						
							|  |  |  |             [parentId, newNotePos, now, noteId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         await sql.addNoteTreeSync(noteId); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         await sql.addAudit(audit_category.CHANGE_PARENT, req, noteId, null, parentId); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.put('/:noteId/moveBefore/:beforeNoteId', async (req, res, next) => { | 
					
						
							|  |  |  |     let noteId = req.params.noteId; | 
					
						
							|  |  |  |     let beforeNoteId = req.params.beforeNoteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const beforeNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [beforeNoteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     if (beforeNote) { | 
					
						
							|  |  |  |         const now = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.execute("update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_id = ?", [now, beforeNoteId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |             await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", | 
					
						
							|  |  |  |                 [beforeNote['note_pid'], beforeNote['note_pos'], now, noteId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |             await sql.addNoteTreeSync(noteId); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |             await sql.addAudit(audit_category.CHANGE_POSITION, req, noteId); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => { | 
					
						
							|  |  |  |     let noteId = req.params.noteId; | 
					
						
							|  |  |  |     let afterNoteId = req.params.afterNoteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const afterNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [afterNoteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     if (afterNote) { | 
					
						
							|  |  |  |         const now = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.execute("update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_pid = ? and note_pos > ? and is_deleted = 0", | 
					
						
							|  |  |  |                 [now, afterNote['note_pid'], afterNote['note_pos']]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |             await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", | 
					
						
							|  |  |  |                 [afterNote['note_pid'], afterNote['note_pos'] + 1, now, noteId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |             await sql.addNoteTreeSync(noteId); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |             await sql.addAudit(audit_category.CHANGE_POSITION, req, noteId); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.put('/:noteId/expanded/:expanded', async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  |     const expanded = req.params.expanded; | 
					
						
							|  |  |  |     const now = utils.nowTimestamp(); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |         await sql.execute("update notes_tree set is_expanded = ?, date_modified = ? where note_id = ?", [expanded, now, noteId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         await sql.addNoteTreeSync(noteId); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         await sql.addAudit(audit_category.CHANGE_EXPANDED, req, noteId, null, expanded); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |