2018-01-11 00:01:16 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								"use strict" ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  express  =  require ( 'express' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  router  =  express . Router ( ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  sql  =  require ( '../../services/sql' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  auth  =  require ( '../../services/auth' ) ;  
						 
					
						
							
								
									
										
										
										
											2018-01-11 21:40:09 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  sync _table  =  require ( '../../services/sync_table' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const  utils  =  require ( '../../services/utils' ) ;  
						 
					
						
							
								
									
										
										
										
											2018-01-11 00:01:16 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								const  wrap  =  require ( 'express-promise-wrap' ) . wrap ;  
						 
					
						
							
								
									
										
										
										
											2018-02-04 19:27:27 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  attributes  =  require ( '../../services/attributes' ) ;  
						 
					
						
							
								
									
										
										
										
											2018-01-11 00:01:16 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-04 19:27:27 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								router . get ( '/notes/:noteId/attributes' ,  auth . checkApiAuth ,  wrap ( async  ( req ,  res ,  next )  =>  {  
						 
					
						
							
								
									
										
										
										
											2018-01-11 00:01:16 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    const  noteId  =  req . params . noteId ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-10 08:37:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    res . send ( await  sql . getRows ( "SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated" ,  [ noteId ] ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-11 00:01:16 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								} ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-04 19:27:27 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								router . put ( '/notes/:noteId/attributes' ,  auth . checkApiAuth ,  wrap ( async  ( req ,  res ,  next )  =>  {  
						 
					
						
							
								
									
										
										
										
											2018-01-11 21:40:09 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  noteId  =  req . params . noteId ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  attributes  =  req . body ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  now  =  utils . nowDate ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    await  sql . doInTransaction ( async  ( )  =>  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        for  ( const  attr  of  attributes )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-28 19:30:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            if  ( attr . attributeId )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-10 08:37:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                await  sql . execute ( "UPDATE attributes SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE attributeId = ?" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    [ attr . name ,  attr . value ,  now ,  attr . isDeleted ,  attr . position ,  attr . attributeId ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-11 21:40:09 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            else  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-06 23:09:19 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                // if it was "created" and then immediatelly deleted, we just don't create it at all
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                if  ( attr . isDeleted )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    continue ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-28 19:30:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                attr . attributeId  =  utils . newAttributeId ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-11 21:40:09 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                await  sql . insert ( "attributes" ,  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-10 08:37:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                    attributeId :  attr . attributeId , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    noteId :  noteId , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    name :  attr . name , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    value :  attr . value , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    position :  attr . position , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    dateCreated :  now , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    dateModified :  now , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    isDeleted :  false 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-11 21:40:09 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-28 19:30:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            await  sync _table . addAttributeSync ( attr . attributeId ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-11 21:40:09 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-10 08:37:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    res . send ( await  sql . getRows ( "SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated" ,  [ noteId ] ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-11 21:40:09 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								} ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-04 19:27:27 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								router . get ( '/attributes/names' ,  auth . checkApiAuth ,  wrap ( async  ( req ,  res ,  next )  =>  {  
						 
					
						
							
								
									
										
										
										
											2018-02-06 23:09:19 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  names  =  await  sql . getColumn ( "SELECT DISTINCT name FROM attributes WHERE isDeleted = 0" ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-04 19:27:27 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for  ( const  attr  of  attributes . BUILTIN _ATTRIBUTES )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( ! names . includes ( attr ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            names . push ( attr ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    names . sort ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    res . send ( names ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-04 19:43:11 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								router . get ( '/attributes/values/:attributeName' ,  auth . checkApiAuth ,  wrap ( async  ( req ,  res ,  next )  =>  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const  attributeName  =  req . params . attributeName ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-06 23:09:19 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const  values  =  await  sql . getColumn ( "SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value" ,  [ attributeName ] ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-04 19:43:11 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    res . send ( values ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-11 00:01:16 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								module . exports  =  router ;