| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 19:12:52 -04:00
										 |  |  | const noteCacheService = require('../../services/note_cache'); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function getAutocomplete(req) { | 
					
						
							|  |  |  |     const query = req.query.query; | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |     const currentNoteId = req.query.currentNoteId || 'none'; | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |     let results; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (query.trim().length === 0) { | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |         results = await getRecentNotes(currentNoteId); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         results = noteCacheService.findNotes(query); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return results.map(res => { | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |             value: res.path, | 
					
						
							| 
									
										
										
										
											2018-06-05 22:47:47 -04:00
										 |  |  |             label: res.title | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  | async function getRecentNotes(currentNoteId) { | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |     const recentNotes = await repository.getEntities(`
 | 
					
						
							|  |  |  |       SELECT  | 
					
						
							|  |  |  |         recent_notes.*  | 
					
						
							|  |  |  |       FROM  | 
					
						
							|  |  |  |         recent_notes | 
					
						
							|  |  |  |         JOIN branches USING(branchId) | 
					
						
							|  |  |  |       WHERE | 
					
						
							|  |  |  |         recent_notes.isDeleted = 0 | 
					
						
							|  |  |  |         AND branches.isDeleted = 0 | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |         AND branches.noteId != ? | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |       ORDER BY  | 
					
						
							|  |  |  |         dateCreated DESC | 
					
						
							| 
									
										
										
										
											2018-08-16 21:02:42 +02:00
										 |  |  |       LIMIT 200`, [currentNoteId]);
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return recentNotes.map(rn => { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             path: rn.notePath, | 
					
						
							|  |  |  |             title: noteCacheService.getNoteTitleForPath(rn.notePath.split('/')) | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     getAutocomplete | 
					
						
							|  |  |  | }; |