2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								const  sql  =  require ( './sql' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  log  =  require ( './log' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  options  =  require ( './options' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  utils  =  require ( './utils' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  audit _category  =  require ( './audit_category' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  eventLog  =  require ( './event_log' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  notes  =  require ( './notes' ) ;  
						 
					
						
							
								
									
										
										
										
											2017-11-16 21:50:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  sync _table  =  require ( './sync_table' ) ;  
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  updateNote ( entity ,  links ,  sourceId )  {  
						 
					
						
							
								
									
										
										
										
											2017-11-20 23:51:28 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  origNote  =  await  sql . getSingleResult ( "SELECT * FROM notes WHERE note_id = ?" ,  [ entity . note _id ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( ! origNote  ||  origNote . date _modified  <=  entity . date _modified )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  sql . doInTransaction ( async  ( )  =>  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            await  sql . replace ( "notes" ,  entity ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            await  sql . remove ( "links" ,  entity . note _id ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            for  ( const  link  of  links )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                delete  link [ 'lnk_id' ] ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-20 19:43:48 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                //await sql.insert('link', link);
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-16 21:50:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            await  sync _table . addNoteSync ( entity . note _id ,  sourceId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            await  notes . addNoteAudits ( origNote ,  entity ,  sourceId ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            await  eventLog . addNoteEvent ( entity . note _id ,  "Synced note <note>" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        log . info ( "Update/sync note "  +  entity . note _id ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  eventLog . addNoteEvent ( entity . note _id ,  "Sync conflict in note <note>, "  +  utils . formatTwoTimestamps ( origNote . date _modified ,  entity . date _modified ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  updateNoteTree ( entity ,  sourceId )  {  
						 
					
						
							
								
									
										
										
										
											2017-11-20 23:51:28 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  orig  =  await  sql . getSingleResultOrNull ( "SELECT * FROM notes_tree WHERE note_tree_id = ?" ,  [ entity . note _tree _id ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( orig  ===  null  ||  orig . date _modified  <  entity . date _modified )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  sql . doInTransaction ( async  ( )  =>  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 21:11:33 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            delete  entity . is _expanded ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            await  sql . replace ( 'notes_tree' ,  entity ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:17:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            await  sync _table . addNoteTreeSync ( entity . note _tree _id ,  sourceId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:17:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            // not sure why this is here ...
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            await  sql . addAudit ( audit _category . UPDATE _TITLE ,  sourceId ,  entity . note _id ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:17:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        log . info ( "Update/sync note tree "  +  entity . note _tree _id ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    else  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:17:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        await  eventLog . addNoteEvent ( entity . note _tree _id ,  "Sync conflict in note tree <note>, "  +  utils . formatTwoTimestamps ( orig . date _modified ,  entity . date _modified ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  updateNoteHistory ( entity ,  sourceId )  {  
						 
					
						
							
								
									
										
										
										
											2017-11-20 23:51:28 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  orig  =  await  sql . getSingleResultOrNull ( "SELECT * FROM notes_history WHERE note_history_id = ?" ,  [ entity . note _history _id ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( orig  ===  null  ||  orig . date _modified _to  <  entity . date _modified _to )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  sql . doInTransaction ( async  ( )  =>  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            await  sql . replace ( 'notes_history' ,  entity ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-16 21:50:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            await  sync _table . addNoteHistorySync ( entity . note _history _id ,  sourceId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        log . info ( "Update/sync note history "  +  entity . note _history _id ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  eventLog . addNoteEvent ( entity . note _id ,  "Sync conflict in note history for <note>, "  +  utils . formatTwoTimestamps ( orig . date _modified _to ,  entity . date _modified _to ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  updateNoteReordering ( entity ,  sourceId )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    await  sql . doInTransaction ( async  ( )  =>  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        Object . keys ( entity . ordering ) . forEach ( async  key  =>  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-18 17:17:46 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            await  sql . execute ( "UPDATE notes_tree SET note_pos = ? WHERE note_tree_id = ?" ,  [ entity . ordering [ key ] ,  key ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-16 21:50:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        await  sync _table . addNoteReorderingSync ( entity . note _pid ,  sourceId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        await  sql . addAudit ( audit _category . CHANGE _POSITION ,  sourceId ,  entity . note _pid ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  updateOptions ( entity ,  sourceId )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( ! options . SYNCED _OPTIONS . includes ( entity . opt _name ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-20 23:51:28 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  orig  =  await  sql . getSingleResultOrNull ( "SELECT * FROM options WHERE opt_name = ?" ,  [ entity . opt _name ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( orig  ===  null  ||  orig . date _modified  <  entity . date _modified )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  sql . doInTransaction ( async  ( )  =>  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            await  sql . replace ( 'options' ,  entity ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-16 21:50:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            await  sync _table . addOptionsSync ( entity . opt _name ,  sourceId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  eventLog . addEvent ( "Synced option "  +  entity . opt _name ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  eventLog . addEvent ( "Sync conflict in options for "  +  entity . opt _name  +  ", "  +  utils . formatTwoTimestamps ( orig . date _modified ,  entity . date _modified ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async  function  updateRecentNotes ( entity ,  sourceId )  {  
						 
					
						
							
								
									
										
										
										
											2017-11-20 23:51:28 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  orig  =  await  sql . getSingleResultOrNull ( "SELECT * FROM recent_notes WHERE note_path = ?" ,  [ entity . note _path ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( orig  ===  null  ||  orig . date _accessed  <  entity . date _accessed )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        await  sql . doInTransaction ( async  ( )  =>  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            await  sql . replace ( 'recent_notes' ,  entity ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-16 21:50:00 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            await  sync _table . addRecentNoteSync ( entity . note _id ,  sourceId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-09 20:52:47 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								module . exports  =  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updateNote , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updateNoteTree , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updateNoteHistory , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updateNoteReordering , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updateOptions , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    updateRecentNotes 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;