-
Notifications
You must be signed in to change notification settings - Fork 25
/
node_helper.js
90 lines (75 loc) · 2.64 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Magic Mirror
* Node Helper: Trello
*
* By Joseph Bethge
* MIT Licensed.
*/
const Trello = require("node-trello");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
var self = this;
console.log("Starting node helper for: " + self.name);
self.api_key = ""
self.token = ""
self.list = ""
self.trelloConnections = {};
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === "TRELLO_CONFIG") {
self.createTrelloConnection(payload.id, payload.api_key, payload.token);
}
if (notification === "REQUEST_LIST_CONTENT") {
const list = payload.list;
const id = payload.id;
self.retrieveListContent(list, id);
}
},
// create trello connection
createTrelloConnection: function(id, key, token) {
var self = this;
if (key === "")
{
var error = {statusCode: 400, statusMessage: "api_key is empty", responseBody: "Please add it."};
self.sendSocketNotification("TRELLO_ERROR", {id: id, error: error});
return;
}
self.trelloConnections[id] = new Trello(key, token);
},
// retrieve list content
retrieveListContent: function(list, id) {
var self = this;
if (!self.trelloConnections[id]) {
return;
}
const path = "/1/lists/" + list + "/cards";
self.trelloConnections[id].get(path, {}, function(error, data) {
if (error)
{
console.log(error);
self.sendSocketNotification("TRELLO_ERROR", {id: id, error: error});
return;
}
for (var card in data)
{
for (var checklist in data[card].idChecklists)
{
const checklistId = data[card].idChecklists[checklist];
const checklistPath = "/1/checklists/" + checklistId;
self.trelloConnections[id].get(checklistPath, {}, function(error, checklistData) {
if (error)
{
console.log(error);
return;
}
self.sendSocketNotification("CHECK_LIST_CONTENT", {id: id, data: checklistData});
});
}
}
self.sendSocketNotification("LIST_CONTENT", {id: id, data: data});
});
},
});