-
Notifications
You must be signed in to change notification settings - Fork 6
/
node_helper.js
56 lines (48 loc) · 1.37 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const ynab = require('ynab');
var NodeHelper = require("node_helper");
var ynabBudgetId;
var config;
var self;
var interval;
module.exports = NodeHelper.create({
socketNotificationReceived: function (noti, payload) {
if (noti == "YNAB_SET_CONFIG") {
this.initialize(payload);
}
},
initialize: function (payload) {
config = payload;
self = this;
var ynabAPI = new ynab.API(config.token);
if (config.budgetId) {
ynabBudgetId = config.budgetId;
this.updateBudget();
this.setInterval();
return;
}
ynabAPI.budgets.getBudgets().then(budgetsResponse => {
ynabBudgetId = budgetsResponse.data.budgets[0].id;
this.updateBudget();
this.setInterval();
}).catch(e => {
console.log("error: " + e);
});
},
setInterval: function () {
if (!interval) {
interval = setInterval(self.updateBudget, 90000);
}
},
updateBudget: function () {
var ynabAPI = new ynab.API(config.token);
ynabAPI.categories.getCategories(ynabBudgetId).then(categoriesResponse => {
const map = [].concat(...Array.from(categoriesResponse.data.category_groups.map(a => Array.from(a.categories)))).reduce((map, o) => { map[o.name] = o; return map; }, new Map());
var list = config.categories.map(a => map[a]).filter(a => a != undefined);
self.sendSocketNotification("YNAB_UPDATE", {
items: list,
});
}).catch(e => {
console.log("error: " + e);
});
}
});