Skip to content

Commit

Permalink
next birthdays
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Mar 23, 2021
1 parent fed49f3 commit 9fa48c4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 32 deletions.
4 changes: 4 additions & 0 deletions admin/index_m.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
<input type="text" class="value" id="icalUrl" />
<label for="icalUrl" class="translate">icalUrl</label>
</div>
<div class="col s6 input-field">
<input type="text" class="value" id="nextTextTemplate" />
<label for="nextTextTemplate" class="translate">text template</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
Expand Down
12 changes: 12 additions & 0 deletions admin/words.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ systemDictionary = {
"pl": "iCal Url",
"zh-cn": "iCal网址"
},
"text template (next)": {
"en": "text template (next)",
"de": "Textvorlage (nächste)",
"ru": "текстовый шаблон (следующий)",
"pt": "modelo de texto (próximo)",
"nl": "tekstsjabloon (volgende)",
"fr": "modèle de texte (suivant)",
"it": "modello di testo (successivo)",
"es": "plantilla de texto (siguiente)",
"pl": "szablon tekstowy (następny)",
"zh-cn": "文字范本(下一个)"
},
"name": {
"en": "name",
"de": "Name",
Expand Down
29 changes: 27 additions & 2 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
]
},
"native": {
"icalUrl": ""
"icalUrl": "",
"nextTextTemplate": "%n turns %a"
},
"objects": [],
"instanceObjects": [
Expand Down Expand Up @@ -98,6 +99,30 @@
"name": "Next birthday(s)"
},
"native": {}
},
{
"_id": "next.text",
"type": "state",
"common": {
"name": "Next birthdays",
"type": "string",
"role": "value",
"read": true,
"write": false
},
"native": {}
},
{
"_id": "next.daysLeft",
"type": "state",
"common": {
"name": "Next days left",
"type": "number",
"role": "value",
"read": true,
"write": false
},
"native": {}
}
]
}
}
58 changes: 28 additions & 30 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Birthdays extends utils.Adapter {

const configBirthday = moment({ year: birthday.year, month: birthday.month - 1, day: birthday.day });

if (configBirthday.isValid()) {
if (configBirthday.isValid() && configBirthday.year() <= this.today.year()) {
this.log.debug('found birthday in settings: ' + birthday.name + ' (' + birthday.year + ')');

this.addBirthday(birthday.name, configBirthday.date(), configBirthday.month(), configBirthday.year());
Expand Down Expand Up @@ -104,9 +104,13 @@ class Birthdays extends utils.Adapter {
const month = event.start.getMonth(); // month as a number (0-11)
const birthYear = parseInt(event.description);

this.log.debug('found birthday in calendar: ' + name + ' (' + birthYear + ')');
if (!isNaN(birthYear) && birthYear <= this.today.year()) {
this.log.debug('found birthday in calendar: ' + name + ' (' + birthYear + ')');

this.addBirthday(name, day, month, birthYear);
this.addBirthday(name, day, month, birthYear);
} else {
this.log.warn('invalid birthday date in calendar: ' + name);
}
}
}

Expand Down Expand Up @@ -147,16 +151,10 @@ class Birthdays extends utils.Adapter {
this.setState('summary.json', {val: JSON.stringify(this.birthdays), ack: true});

const keepBirthdays = [];
const monthBirhtdays = (await this.getChannelsOfAsync('month'))
const allBirhtdays = (await this.getChannelsOfAsync('month'))
.map(obj => { return this.removeNamespace(obj._id) })
.filter(id => new RegExp('month\.[0-9]{2}\..+', 'g').test(id));

const nextBirhtdays = (await this.getChannelsOfAsync('next'))
.map(obj => { return this.removeNamespace(obj._id) })
.filter(id => new RegExp('next\..+', 'g').test(id));

const allBirhtdays = [].concat(monthBirhtdays, nextBirhtdays);

for (const b in this.birthdays) {
const birthday = this.birthdays[b];

Expand All @@ -165,26 +163,11 @@ class Birthdays extends utils.Adapter {

keepBirthdays.push(monthPath);

await this.fillPathWithBirhtday(monthPath, birthday);
}

// next birthdays
if (this.birthdays.length > 0) {
const nextBirthdayDaysLeft = this.birthdays[0].daysLeft;
const nextBirthdays = this.birthdays.filter(birthday => birthday.daysLeft == nextBirthdayDaysLeft); // get all birthdays with same days left

this.log.debug('next birthday(s): ' + JSON.stringify(nextBirthdays));

for (const b in nextBirthdays) {
const birthday = this.birthdays[b];

const cleanName = this.cleanNamespace(birthday.name);
const nextPath = 'next.' + cleanName;

keepBirthdays.push(nextPath);

await this.fillPathWithBirhtday(nextPath, birthday);
if (allBirhtdays.indexOf(monthPath) === -1) {
this.log.debug('birthday added: ' + monthPath);
}

await this.fillPathWithBirhtday(monthPath, birthday);
}

// Delete non existent birthdays
Expand All @@ -193,11 +176,26 @@ class Birthdays extends utils.Adapter {

if (keepBirthdays.indexOf(id) === -1) {
this.delObject(id, {recursive: true}, () => {
this.log.debug('Birthday deleted: ' + id);
this.log.debug('birthday deleted: ' + id);
});
}
}

// next birthdays
if (this.birthdays.length > 0) {
const nextBirthdayDaysLeft = this.birthdays[0].daysLeft;
const nextBirthdays = this.birthdays
.filter(birthday => birthday.daysLeft == nextBirthdayDaysLeft) // get all birthdays with same days left
.map(birthday => {
return this.config.nextTextTemplate
.replace('%n', birthday.name)
.replace('%a', birthday.age);
});

await this.setStateAsync('next.daysLeft', {val: nextBirthdayDaysLeft, ack: true});
await this.setStateAsync('next.text', {val: nextBirthdays.join(', '), ack: true});
}

}

async fillPathWithBirhtday(path, birthday) {
Expand Down

0 comments on commit 9fa48c4

Please sign in to comment.