diff --git a/event_alerts.js b/event_alerts.js new file mode 100644 index 0000000..d90e406 --- /dev/null +++ b/event_alerts.js @@ -0,0 +1,24 @@ +// Create as JS Backend note and add these attributes: +// ~targetTemplateEvent=@event template (this note is described below) +// #customRequestHandler=event_alerts +// Create another note (event template) with this promoted attributes: +// startTime +// Optionally add: +// endTime, location + +const {req, res} = api; + +const targetTemplate = await api.currentNote.getRelationValue("targetTemplateEvent"); +const events = await api.getNotesWithLabel("template", targetTemplate); + +let eventsData = []; + +for (const event of events) { + const attr = await event.getAttribute("label", "startTime"); + eventsData.push({ + name: event.title, + startTime: attr.value + }); +} + +res.send(eventsData); diff --git a/new_reminder.js b/new_reminder.js new file mode 100644 index 0000000..7dc14d4 --- /dev/null +++ b/new_reminder.js @@ -0,0 +1,63 @@ +// Create as JS Backend note with these attributes: +// ~targetTemplate=@reminder template +// #customRequestHandler=new_reminder +// Create a new note (reminder template) with these promoted attributes: +// todoDate: date +// todoTime: text +// doneDate: date +// reminder = true + +Date.prototype.addHours = function(h) { + this.setTime(this.getTime() + (h*60*60*1000)); + return this; +} + +const {req, res} = api; + +const time = new Date(req.body["time"]); +api.log(time); +time.addHours(1); // TODO: consider summer time +const task = req.body["task"]; + +var hour = time.getHours(); +var minute = time.getMinutes(); +var second = time.getSeconds(); + +if (hour < 10) { + hour = '0' + hour; +} +if (minute < 10) { + minute = '0' + minute; +} +if (second < 10) { + second = '0' + second; +} + +const year = time.getFullYear(); +var month = time.getMonth() + 1; +var day = time.getDate(); + +if (month < 10) { + month = '0' + month; +} +if (day < 10) { + day = '0' + day; +} + +const todayDateStr = year + "-" + month + "-" + day; +const todayNote = await api.getDateNote(todayDateStr); + +const targetTemplate = await api.currentNote.getAttributeValue('relation', 'targetTemplate'); +const resp = await api.createNote( + todayNote.noteId, + task, + '', + { + "attributes": [ + {"type": "relation", "name": "template", "value": targetTemplate}, + {"type": "label", "name": "todoDate", "value": todayDateStr}, + {"type": "label", "name": "todoTime", "value": hour + ":" + minute + ":" + second} + ]} +); + +res.sendStatus(200); diff --git a/task_alerts.js b/task_alerts.js new file mode 100644 index 0000000..04218ea --- /dev/null +++ b/task_alerts.js @@ -0,0 +1,29 @@ +// Create as JS Backend note with attributes: +// ~targetTemplate=@task template (included in Trilium task manager) +// #customRequestHandler=task_alerts +// ~targetTemplateReminder=@reminder template (see new_reminder.js) + +const {req, res} = api; + +const targetTemplate = await api.currentNote.getRelationValue('targetTemplate'); +const tasks = await api.getNotesWithLabel("template", targetTemplate); + +let tasksData = []; + +for (const task of tasks) { + tasksData.push({ + attributes: await task.getAttributes(), + ...task + }); +} + +const targetTemplateReminder = await api.currentNote.getRelationValue('targetTemplateReminder'); +const reminders = await api.getNotesWithLabel("template", targetTemplateReminder); +for (const task of reminders) { + tasksData.push({ + attributes: await task.getAttributes(), + ...task + }); +} + +res.send(tasksData);