| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const notes = require('./notes'); | 
					
						
							|  |  |  | const attributes = require('./attributes'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const CALENDAR_ROOT_ATTRIBUTE = 'calendar_root'; | 
					
						
							|  |  |  | const YEAR_ATTRIBUTE = 'year_note'; | 
					
						
							|  |  |  | const MONTH_ATTRIBUTE = 'month_note'; | 
					
						
							|  |  |  | const DATE_ATTRIBUTE = 'date_note'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function createNote(parentNoteId, noteTitle, noteText) { | 
					
						
							|  |  |  |     return (await notes.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-01-24 23:08:14 -05:00
										 |  |  |     })).noteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getNoteStartingWith(parentNoteId, startsWith) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     return await sql.getFirstValue(`SELECT noteId FROM notes JOIN note_tree USING(noteId) 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                                     WHERE parentNoteId = ? AND title LIKE '${startsWith}%' | 
					
						
							|  |  |  |                                     AND notes.isDeleted = 0 AND isProtected = 0  | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |                                     AND note_tree.isDeleted = 0`, [parentNoteId]);
 | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getRootNoteId() { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     let rootNoteId = await sql.getFirstValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId) 
 | 
					
						
							|  |  |  |               WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`);
 | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!rootNoteId) { | 
					
						
							|  |  |  |         rootNoteId = (await notes.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-01-24 23:08:14 -05:00
										 |  |  |         })).noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await attributes.createAttribute(rootNoteId, CALENDAR_ROOT_ATTRIBUTE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return rootNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getYearNoteId(dateTimeStr, rootNoteId) { | 
					
						
							|  |  |  |     const yearStr = dateTimeStr.substr(0, 4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let yearNoteId = await attributes.getNoteIdWithAttribute(YEAR_ATTRIBUTE, yearStr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!yearNoteId) { | 
					
						
							|  |  |  |         yearNoteId = await getNoteStartingWith(rootNoteId, yearStr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!yearNoteId) { | 
					
						
							|  |  |  |             yearNoteId = await createNote(rootNoteId, yearStr); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await attributes.createAttribute(yearNoteId, YEAR_ATTRIBUTE, yearStr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return yearNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getMonthNoteId(dateTimeStr, rootNoteId) { | 
					
						
							|  |  |  |     const monthStr = dateTimeStr.substr(0, 7); | 
					
						
							|  |  |  |     const monthNumber = dateTimeStr.substr(5, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let monthNoteId = await attributes.getNoteIdWithAttribute(MONTH_ATTRIBUTE, monthStr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!monthNoteId) { | 
					
						
							|  |  |  |         const yearNoteId = await getYearNoteId(dateTimeStr, rootNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         monthNoteId = await getNoteStartingWith(yearNoteId, monthNumber); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!monthNoteId) { | 
					
						
							|  |  |  |             monthNoteId = await createNote(yearNoteId, monthNumber); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await attributes.createAttribute(monthNoteId, MONTH_ATTRIBUTE, monthStr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return monthNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | async function getDateNoteId(dateTimeStr, rootNoteId = null) { | 
					
						
							|  |  |  |     if (!rootNoteId) { | 
					
						
							|  |  |  |         rootNoteId = await getRootNoteId(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 23:08:14 -05:00
										 |  |  |     const dateStr = dateTimeStr.substr(0, 10); | 
					
						
							|  |  |  |     const dayNumber = dateTimeStr.substr(8, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let dateNoteId = await attributes.getNoteIdWithAttribute(DATE_ATTRIBUTE, dateStr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!dateNoteId) { | 
					
						
							|  |  |  |         const monthNoteId = await getMonthNoteId(dateTimeStr, rootNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dateNoteId = await getNoteStartingWith(monthNoteId, dayNumber); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!dateNoteId) { | 
					
						
							|  |  |  |             dateNoteId = await createNote(monthNoteId, dayNumber); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await attributes.createAttribute(dateNoteId, DATE_ATTRIBUTE, dateStr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return dateNoteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     getRootNoteId, | 
					
						
							|  |  |  |     getYearNoteId, | 
					
						
							|  |  |  |     getMonthNoteId, | 
					
						
							|  |  |  |     getDateNoteId | 
					
						
							|  |  |  | }; |