2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"use strict" ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  express  =  require ( 'express' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  router  =  express . Router ( ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  sql  =  require ( '../../services/sql' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  auth  =  require ( '../../services/auth' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  utils  =  require ( '../../services/utils' ) ;  
						 
					
						
							
								
									
										
										
										
											2017-11-16 21:50:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  sync _table  =  require ( '../../services/sync_table' ) ;  
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:05:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  options  =  require ( '../../services/options' ) ;  
						 
					
						
							
								
									
										
										
										
											2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								router . get ( '' ,  auth . checkApiAuth ,  async  ( req ,  res ,  next )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    res . send ( await  getRecentNotes ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:05:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								router . put ( '/:noteTreeId' ,  auth . checkApiAuth ,  async  ( req ,  res ,  next )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  noteTreeId  =  req . params . noteTreeId ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    await  sql . replace ( 'recent_notes' ,  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:05:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        note _tree _id :  noteTreeId , 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        date _accessed :  utils . nowTimestamp ( ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        is _deleted :  0 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:05:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    await  sync _table . addRecentNoteSync ( noteTreeId ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    await  options . setOption ( 'start_note_tree_id' ,  noteTreeId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    res . send ( await  getRecentNotes ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:05:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								router . delete ( '/:noteTreeId' ,  auth . checkApiAuth ,  async  ( req ,  res ,  next )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    await  sql . execute ( 'UPDATE recent_notes SET is_deleted = 1 WHERE note_tree_id = ?' ,  [ req . params . noteTreeId ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:05:50 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    await  sync _table . addRecentNoteSync ( req . params . noteTreeId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    res . send ( await  getRecentNotes ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  getRecentNotes ( )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    await  deleteOld ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return  await  sql . getResults ( "SELECT * FROM recent_notes WHERE is_deleted = 0 ORDER BY date_accessed DESC" ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  deleteOld ( )  {  
						 
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  cutoffDateAccessed  =  await  sql . getSingleValue ( "SELECT date_accessed FROM recent_notes WHERE is_deleted = 0 ORDER BY date_accessed DESC LIMIT 100, 1" ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-04 23:46:50 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( cutoffDateAccessed )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  sql . execute ( "DELETE FROM recent_notes WHERE date_accessed < ?" ,  [ cutoffDateAccessed ] ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								module . exports  =  router ;