| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const noteService = require('./notes'); | 
					
						
							|  |  |  | const labelService = require('./labels'); | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | const CALENDAR_ROOT_LABEL = 'calendar_root'; | 
					
						
							|  |  |  | const YEAR_LABEL = 'year_note'; | 
					
						
							|  |  |  | const MONTH_LABEL = 'month_note'; | 
					
						
							|  |  |  | const DATE_LABEL = 'date_note'; | 
					
						
							| 
									
										
										
										
											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-01 21:27:46 -04:00
										 |  |  |     const {note} = 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-01 11:42:12 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return note.noteId; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getNoteStartingWith(parentNoteId, startsWith) { | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     return await sql.getValue(`SELECT noteId 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-02-24 14:42:52 -05:00
										 |  |  | async function getRootCalendarNoteId() { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId) 
 | 
					
						
							|  |  |  |               WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);
 | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!rootNoteId) { | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |         const {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-01 11:42:12 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const rootNoteId = rootNote.noteId; | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |         await labelService.createLabel(rootNoteId, CALENDAR_ROOT_LABEL); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return rootNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getYearNoteId(dateTimeStr, rootNoteId) { | 
					
						
							|  |  |  |     const yearStr = dateTimeStr.substr(0, 4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     let yearNoteId = await labelService.getNoteIdWithLabel(YEAR_LABEL, yearStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!yearNoteId) { | 
					
						
							|  |  |  |         yearNoteId = await getNoteStartingWith(rootNoteId, yearStr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!yearNoteId) { | 
					
						
							|  |  |  |             yearNoteId = await createNote(rootNoteId, yearStr); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |         await labelService.createLabel(yearNoteId, YEAR_LABEL, yearStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return yearNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getMonthNoteId(dateTimeStr, rootNoteId) { | 
					
						
							|  |  |  |     const monthStr = dateTimeStr.substr(0, 7); | 
					
						
							|  |  |  |     const monthNumber = dateTimeStr.substr(5, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     let monthNoteId = await labelService.getNoteIdWithLabel(MONTH_LABEL, monthStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!monthNoteId) { | 
					
						
							|  |  |  |         const yearNoteId = await getYearNoteId(dateTimeStr, rootNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         monthNoteId = await getNoteStartingWith(yearNoteId, monthNumber); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!monthNoteId) { | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  |             const dateObj = utils.parseDate(dateTimeStr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             monthNoteId = await createNote(yearNoteId, noteTitle); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |         await labelService.createLabel(monthNoteId, MONTH_LABEL, monthStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return monthNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | async function getDateNoteId(dateTimeStr, rootNoteId = null) { | 
					
						
							|  |  |  |     if (!rootNoteId) { | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |         rootNoteId = await getRootCalendarNoteId(); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     const dateStr = dateTimeStr.substr(0, 10); | 
					
						
							|  |  |  |     const dayNumber = dateTimeStr.substr(8, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     let dateNoteId = await labelService.getNoteIdWithLabel(DATE_LABEL, dateStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!dateNoteId) { | 
					
						
							|  |  |  |         const monthNoteId = await getMonthNoteId(dateTimeStr, rootNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dateNoteId = await getNoteStartingWith(monthNoteId, dayNumber); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!dateNoteId) { | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  |             const dateObj = utils.parseDate(dateTimeStr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             dateNoteId = await createNote(monthNoteId, noteTitle); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |         await labelService.createLabel(dateNoteId, DATE_LABEL, dateStr); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return dateNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     getRootCalendarNoteId, | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     getYearNoteId, | 
					
						
							|  |  |  |     getMonthNoteId, | 
					
						
							|  |  |  |     getDateNoteId | 
					
						
							|  |  |  | }; |