| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const noteService = require('./notes'); | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  | const attributeService = require('./attributes'); | 
					
						
							| 
									
										
										
										
											2018-04-02 20:46:46 -04:00
										 |  |  | const dateUtils = require('./date_utils'); | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  | const repository = require('./repository'); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:56:55 -04:00
										 |  |  | const CALENDAR_ROOT_LABEL = 'calendarRoot'; | 
					
						
							| 
									
										
										
										
											2018-04-04 23:04:31 -04:00
										 |  |  | const YEAR_LABEL = 'yearNote'; | 
					
						
							|  |  |  | const MONTH_LABEL = 'monthNote'; | 
					
						
							|  |  |  | const DATE_LABEL = 'dateNote'; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  | const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; | 
					
						
							|  |  |  | const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | async function createNote(parentNoteId, noteTitle, noteText) { | 
					
						
							| 
									
										
										
										
											2018-04-07 13:14:01 -04:00
										 |  |  |     return (await noteService.createNewNote(parentNoteId, { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         title: noteTitle, | 
					
						
							|  |  |  |         content: noteText, | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |         target: 'into', | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         isProtected: false | 
					
						
							| 
									
										
										
										
											2018-04-07 13:14:01 -04:00
										 |  |  |     })).note; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getNoteStartingWith(parentNoteId, startsWith) { | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     return await repository.getEntity(`SELECT notes.* FROM notes JOIN branches USING(noteId) 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                                     WHERE parentNoteId = ? AND title LIKE '${startsWith}%' | 
					
						
							|  |  |  |                                     AND notes.isDeleted = 0 AND isProtected = 0  | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |                                     AND branches.isDeleted = 0`, [parentNoteId]);
 | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  | async function getRootCalendarNote() { | 
					
						
							| 
									
										
										
										
											2018-05-08 16:39:01 -04:00
										 |  |  |     // some caching here could be useful (e.g. in CLS)
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     let rootNote = await attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     if (!rootNote) { | 
					
						
							|  |  |  |         rootNote = (await noteService.createNewNote('root', { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             title: 'Calendar', | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |             target: 'into', | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             isProtected: false | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         })).note; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |         await attributeService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL); | 
					
						
							|  |  |  |         await attributeService.createLabel(rootNote.noteId, 'sorted'); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     return rootNote; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  | async function getYearNote(dateStr, rootNote) { | 
					
						
							|  |  |  |     const yearStr = dateStr.substr(0, 4); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     let yearNote = await attributeService.getNoteWithLabel(YEAR_LABEL, yearStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     if (!yearNote) { | 
					
						
							|  |  |  |         yearNote = await getNoteStartingWith(rootNote.noteId, yearStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         if (!yearNote) { | 
					
						
							|  |  |  |             yearNote = await createNote(rootNote.noteId, yearStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |         await attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr); | 
					
						
							|  |  |  |         await attributeService.createLabel(yearNote.noteId, 'sorted'); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     return yearNote; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  | async function getMonthNote(dateStr, rootNote) { | 
					
						
							|  |  |  |     const monthStr = dateStr.substr(0, 7); | 
					
						
							|  |  |  |     const monthNumber = dateStr.substr(5, 2); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     let monthNote = await attributeService.getNoteWithLabel(MONTH_LABEL, monthStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     if (!monthNote) { | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  |         const yearNote = await getYearNote(dateStr, rootNote); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         monthNote = await getNoteStartingWith(yearNote.noteId, monthNumber); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         if (!monthNote) { | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  |             const dateObj = dateUtils.parseLocalDate(dateStr); | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |             monthNote = await createNote(yearNote.noteId, noteTitle); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |         await attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr); | 
					
						
							|  |  |  |         await attributeService.createLabel(monthNote.noteId, 'sorted'); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     return monthNote; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  | async function getDateNote(dateStr) { | 
					
						
							| 
									
										
										
										
											2018-05-08 16:39:01 -04:00
										 |  |  |     const rootNote = await getRootCalendarNote(); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  |     const dayNumber = dateStr.substr(8, 2); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     let dateNote = await attributeService.getNoteWithLabel(DATE_LABEL, dateStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     if (!dateNote) { | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  |         const monthNote = await getMonthNote(dateStr, rootNote); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         dateNote = await getNoteStartingWith(monthNote.noteId, dayNumber); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |         if (!dateNote) { | 
					
						
							| 
									
										
										
										
											2019-01-15 23:46:01 +01:00
										 |  |  |             const dateObj = dateUtils.parseLocalDate(dateStr); | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |             dateNote = await createNote(monthNote.noteId, noteTitle); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |         await attributeService.createLabel(dateNote.noteId, DATE_LABEL, dateStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     return dateNote; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-04-07 13:03:16 -04:00
										 |  |  |     getRootCalendarNote, | 
					
						
							|  |  |  |     getYearNote, | 
					
						
							|  |  |  |     getMonthNote, | 
					
						
							|  |  |  |     getDateNote | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | }; |