-
Notifications
You must be signed in to change notification settings - Fork 1
/
wunderlist-create-task.js
69 lines (63 loc) · 1.83 KB
/
wunderlist-create-task.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
var wunderlistSDK = require('./wunderlist-sdk');
module.exports = function(RED) {
function WunderlistCreateTask(n) {
RED.nodes.createNode(this, n);
var node = this;
this.config = RED.nodes.getNode(n.config);
this.on('input', function(msg, send, done) {
send = send || function() { node.send.apply(node,arguments) };
var wunderlistAPI = wunderlistSDK.getApi(node.config, msg);
var tasks = wunderlistAPI.http.tasks;
var reminders = wunderlistAPI.http.reminders;
var params = {
'list_id': Number(n.listId || msg.listId),
'title': n.taskTitle || msg.payload
};
// Add due date if in msg
if (msg.dueDate){
params.due_date = msg.dueDate;
}
tasks.create(params)
.done(function (taskData, statusCode) {
msg.taskId = taskData.id;
msg.task = taskData;
if (!msg.reminderDate) {
send(msg);
if (done) {
done();
}
} else {
var reminderData = {
'task_id': taskData.id,
'date': msg.reminderDate
};
reminders.create(reminderData)
.done((reminderData, status) => {
msg.reminder = reminderData;
send(msg);
if (done) {
done();
}
})
.fail((resp, code) => {
var err = resp || 'Wunderlist API error';
if (done) {
done(err)
} else {
node.error(err);
}
})
}
})
.fail(function (resp, code) {
var err = resp || 'Wunderlist API error';
if (done) {
done(err)
} else {
node.error(err);
}
});
});
}
RED.nodes.registerType('wunderlist-create-task', WunderlistCreateTask);
};