2018-01-25 12:08:14 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('./sql');
|
2018-04-02 09:27:46 +08:00
|
|
|
const noteService = require('./notes');
|
|
|
|
const labelService = require('./labels');
|
2018-04-03 08:46:46 +08:00
|
|
|
const dateUtils = require('./date_utils');
|
2018-04-08 01:03:16 +08:00
|
|
|
const repository = require('./repository');
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-03 09:56:55 +08:00
|
|
|
const CALENDAR_ROOT_LABEL = 'calendarRoot';
|
2018-04-05 11:04:31 +08:00
|
|
|
const YEAR_LABEL = 'yearNote';
|
|
|
|
const MONTH_LABEL = 'monthNote';
|
|
|
|
const DATE_LABEL = 'dateNote';
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-02-11 02:53:35 +08: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-25 12:08:14 +08:00
|
|
|
async function createNote(parentNoteId, noteTitle, noteText) {
|
2018-04-08 01:03:16 +08:00
|
|
|
return await noteService.createNewNote(parentNoteId, {
|
2018-01-29 08:30:14 +08:00
|
|
|
title: noteTitle,
|
|
|
|
content: noteText,
|
2018-01-25 12:08:14 +08:00
|
|
|
target: 'into',
|
2018-01-29 08:30:14 +08:00
|
|
|
isProtected: false
|
2018-04-01 23:42:12 +08:00
|
|
|
});
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getNoteStartingWith(parentNoteId, startsWith) {
|
2018-04-08 01:03:16 +08:00
|
|
|
return await repository.getEntity(`SELECT notes.* FROM notes JOIN branches USING(noteId)
|
2018-01-29 08:30:14 +08:00
|
|
|
WHERE parentNoteId = ? AND title LIKE '${startsWith}%'
|
|
|
|
AND notes.isDeleted = 0 AND isProtected = 0
|
2018-03-25 09:39:15 +08:00
|
|
|
AND branches.isDeleted = 0`, [parentNoteId]);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
async function getRootCalendarNote() {
|
|
|
|
let rootNote = await labelService.getNoteWithLabel(CALENDAR_ROOT_LABEL);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
if (!rootNote) {
|
|
|
|
rootNote = (await noteService.createNewNote('root', {
|
2018-01-29 08:30:14 +08:00
|
|
|
title: 'Calendar',
|
2018-01-25 12:08:14 +08:00
|
|
|
target: 'into',
|
2018-01-29 08:30:14 +08:00
|
|
|
isProtected: false
|
2018-04-08 01:03:16 +08:00
|
|
|
})).note;
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await labelService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
return rootNote;
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
async function getYearNote(dateTimeStr, rootNote) {
|
2018-01-25 12:08:14 +08:00
|
|
|
const yearStr = dateTimeStr.substr(0, 4);
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
let yearNote = await labelService.getNoteWithLabel(YEAR_LABEL, yearStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
if (!yearNote) {
|
|
|
|
yearNote = await getNoteStartingWith(rootNote.noteId, yearStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
if (!yearNote) {
|
|
|
|
yearNote = await createNote(rootNote.noteId, yearStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await labelService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
return yearNote;
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
async function getMonthNote(dateTimeStr, rootNote) {
|
2018-01-25 12:08:14 +08:00
|
|
|
const monthStr = dateTimeStr.substr(0, 7);
|
|
|
|
const monthNumber = dateTimeStr.substr(5, 2);
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
let monthNote = await labelService.getNoteWithLabel(MONTH_LABEL, monthStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
if (!monthNote) {
|
|
|
|
const yearNote = await getYearNote(dateTimeStr, rootNote);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
monthNote = await getNoteStartingWith(yearNote.noteId, monthNumber);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
if (!monthNote) {
|
2018-04-03 08:46:46 +08:00
|
|
|
const dateObj = dateUtils.parseDate(dateTimeStr);
|
2018-02-11 02:53:35 +08:00
|
|
|
|
|
|
|
const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()];
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
monthNote = await createNote(yearNote.noteId, noteTitle);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await labelService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
return monthNote;
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
async function getDateNote(dateTimeStr, rootNote = null) {
|
|
|
|
if (!rootNote) {
|
|
|
|
rootNote = await getRootCalendarNote();
|
2018-01-27 12:40:48 +08:00
|
|
|
}
|
|
|
|
|
2018-01-25 12:08:14 +08:00
|
|
|
const dateStr = dateTimeStr.substr(0, 10);
|
|
|
|
const dayNumber = dateTimeStr.substr(8, 2);
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
let dateNote = await labelService.getNoteWithLabel(DATE_LABEL, dateStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
if (!dateNote) {
|
|
|
|
const monthNote = await getMonthNote(dateTimeStr, rootNote);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
dateNote = await getNoteStartingWith(monthNote.noteId, dayNumber);
|
2018-01-25 12:08:14 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
if (!dateNote) {
|
2018-04-03 08:46:46 +08:00
|
|
|
const dateObj = dateUtils.parseDate(dateTimeStr);
|
2018-02-11 02:53:35 +08:00
|
|
|
|
|
|
|
const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()];
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
dateNote = await createNote(monthNote.noteId, noteTitle);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await labelService.createLabel(dateNote.noteId, DATE_LABEL, dateStr);
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
return dateNote;
|
2018-01-25 12:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-04-08 01:03:16 +08:00
|
|
|
getRootCalendarNote,
|
|
|
|
getYearNote,
|
|
|
|
getMonthNote,
|
|
|
|
getDateNote
|
2018-01-25 12:08:14 +08:00
|
|
|
};
|