| 
									
										
										
										
											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 auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  | const sync = require('../../services/sync'); | 
					
						
							|  |  |  | const audit_category = require('../../services/audit_category'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  | router.post('', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const treeLoadTime = req.body.treeLoadTime; | 
					
						
							|  |  |  |     const currentNoteId = req.body.currentNoteId; | 
					
						
							|  |  |  |     const currentNoteDateModified = req.body.currentNoteDateModified; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const browserId = req.get('x-browser-id'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |     const noteTreeChangesCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " + | 
					
						
							|  |  |  |         "AND date_modified >= ? AND category IN (?, ?, ?)", [browserId, treeLoadTime, | 
					
						
							|  |  |  |         audit_category.UPDATE_TITLE, audit_category.CHANGE_PARENT, audit_category.CHANGE_POSITION]); | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |     const currentNoteChangesCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " + | 
					
						
							|  |  |  |         "AND date_modified >= ? AND note_id = ? AND category IN (?)", [browserId, currentNoteDateModified, currentNoteId, | 
					
						
							|  |  |  |         audit_category.UPDATE_CONTENT]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 23:39:17 -04:00
										 |  |  |     if (currentNoteChangesCount > 0) { | 
					
						
							|  |  |  |         console.log("Current note changed!"); | 
					
						
							|  |  |  |         console.log("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != '" + browserId + "') " + | 
					
						
							|  |  |  |             "AND date_modified >= " + currentNoteDateModified + " AND note_id = '" + currentNoteId + "' AND category IN ('" + audit_category.UPDATE_CONTENT + "')"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |     let changesToPushCount = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sync.isSyncSetup) { | 
					
						
							|  |  |  |         const lastSyncedPush = await sql.getOption('last_synced_push'); | 
					
						
							|  |  |  |         changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-01 20:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     res.send({ | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |         'changedTree': noteTreeChangesCount > 0, | 
					
						
							|  |  |  |         'changedCurrentNote': currentNoteChangesCount > 0, | 
					
						
							| 
									
										
										
										
											2017-11-01 20:31:44 -04:00
										 |  |  |         'changesToPushCount': changesToPushCount | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |