From e9705640366e0d4da6d346075d421c69c8bd9cc9 Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 10 Feb 2018 13:53:35 -0500 Subject: [PATCH] create months and days with associated english names, closes #37 --- src/routes/api/login.js | 2 +- src/services/backup.js | 2 +- src/services/date_notes.js | 16 ++++++++++++++-- src/services/notes.js | 2 +- src/services/utils.js | 9 ++++++++- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/routes/api/login.js b/src/routes/api/login.js index c632c5665..befc3bba3 100644 --- a/src/routes/api/login.js +++ b/src/routes/api/login.js @@ -14,7 +14,7 @@ const wrap = require('express-promise-wrap').wrap; router.post('/sync', wrap(async (req, res, next) => { const timestampStr = req.body.timestamp; - const timestamp = utils.parseDate(timestampStr); + const timestamp = utils.parseDateTime(timestampStr); const now = new Date(); diff --git a/src/services/backup.js b/src/services/backup.js index f143d4764..085aa1075 100644 --- a/src/services/backup.js +++ b/src/services/backup.js @@ -10,7 +10,7 @@ const sync_mutex = require('./sync_mutex'); async function regularBackup() { const now = new Date(); - const lastBackupDate = utils.parseDate(await options.getOption('last_backup_date')); + const lastBackupDate = utils.parseDateTime(await options.getOption('last_backup_date')); console.log(lastBackupDate); diff --git a/src/services/date_notes.js b/src/services/date_notes.js index 90cdd5e6a..d93c868fd 100644 --- a/src/services/date_notes.js +++ b/src/services/date_notes.js @@ -3,12 +3,16 @@ const sql = require('./sql'); const notes = require('./notes'); const attributes = require('./attributes'); +const utils = require('./utils'); const CALENDAR_ROOT_ATTRIBUTE = 'calendar_root'; const YEAR_ATTRIBUTE = 'year_note'; const MONTH_ATTRIBUTE = 'month_note'; const DATE_ATTRIBUTE = 'date_note'; +const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; +const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December']; + async function createNote(parentNoteId, noteTitle, noteText) { return (await notes.createNewNote(parentNoteId, { title: noteTitle, @@ -72,7 +76,11 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) { monthNoteId = await getNoteStartingWith(yearNoteId, monthNumber); if (!monthNoteId) { - monthNoteId = await createNote(yearNoteId, monthNumber); + const dateObj = utils.parseDate(dateTimeStr); + + const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()]; + + monthNoteId = await createNote(yearNoteId, noteTitle); } await attributes.createAttribute(monthNoteId, MONTH_ATTRIBUTE, monthStr); @@ -97,7 +105,11 @@ async function getDateNoteId(dateTimeStr, rootNoteId = null) { dateNoteId = await getNoteStartingWith(monthNoteId, dayNumber); if (!dateNoteId) { - dateNoteId = await createNote(monthNoteId, dayNumber); + const dateObj = utils.parseDate(dateTimeStr); + + const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()]; + + dateNoteId = await createNote(monthNoteId, noteTitle); } await attributes.createAttribute(dateNoteId, DATE_ATTRIBUTE, dateStr); diff --git a/src/services/notes.js b/src/services/notes.js index c8e695dab..efccf2560 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -235,7 +235,7 @@ async function updateNote(noteId, newNote, dataKey, sourceId) { "SELECT noteRevisionId FROM note_revisions WHERE noteId = ? AND dateModifiedTo >= ?", [noteId, historyCutoff]); await sql.doInTransaction(async () => { - const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.dateCreated).getTime(); + const msSinceDateCreated = now.getTime() - utils.parseDateTime(newNote.detail.dateCreated).getTime(); if (attributesMap.disable_versioning !== 'true' && !existingnoteRevisionId diff --git a/src/services/utils.js b/src/services/utils.js index 190dcd2c0..de73533ac 100644 --- a/src/services/utils.js +++ b/src/services/utils.js @@ -47,7 +47,7 @@ function dateStr(date) { * @param str - needs to be in the ISO 8601 format "YYYY-MM-DDTHH:MM:SS.sssZ" format as outputted by dateStr(). * also is assumed to be GMT time (as indicated by the "Z" at the end), *not* local time */ -function parseDate(str) { +function parseDateTime(str) { try { return new Date(Date.parse(str)); } @@ -56,6 +56,12 @@ function parseDate(str) { } } +function parseDate(str) { + const datePart = str.substr(0, 10); + + return parseDateTime(datePart + "T12:00:00.000Z"); +} + function toBase64(plainText) { return Buffer.from(plainText).toString('base64'); } @@ -117,6 +123,7 @@ module.exports = { nowDate, dateStr, parseDate, + parseDateTime, newNoteId, newNoteTreeId, newnoteRevisionId,