-
Notifications
You must be signed in to change notification settings - Fork 11
/
Lab4_5_complete_task.js
88 lines (78 loc) · 2.88 KB
/
Lab4_5_complete_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Copyright © 2016 ServiceNow, Inc.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* This is a re-usable module to do tasks related operations with ServiceNow instance.
*/
module.exports = Task;
// ******************************************
// Lab 5 - Replace this function definition *
// ******************************************
function Task(snInstanceURL, accessToken, options) {
this.snInstanceURL = snInstanceURL;
this.accessToken = accessToken;
this.options = options;
}
// Returns the tasks assigned to user.
Task.prototype.getTasks = function (callBack) {
var request = require('request');
request.debug = this.options.verbose;
request({
baseUrl: this.snInstanceURL,
method: 'GET',
// This uri is a part of myTasks service.
uri: '/api/x_snc_my_work/v1/tracker/task',
json: true,
// Set the cookie to authenticate the request.
// ****************************************************
// Lab 5 - Replace the three lines after this comment *
// ****************************************************
auth: {
bearer: this.accessToken
}
}, function (err, response, body) {
callBack(err, response, body);
});
}
// Adds a comment to the task.
Task.prototype.addComment = function (taskID, comment, callBack) {
var request = require('request');
request.debug = this.options.verbose;
request({
baseUrl: this.snInstanceURL,
method: 'POST',
uri: '/api/x_snc_my_work/v1/tracker/task/' + taskID + '/comment',
json: true,
body: {
"comment": comment
},
headers: {
'Cookie': this.snCoookie
}
}, function (err, response, body) {
callBack(err, response, body);
});
}
// Get comments from task. MyTask api does not have a method to get tasks. Here we use sys_journal api
// to get the comments from ServiceNow instance.
Task.prototype.getComments = function (taskID, callBack) {
var request = require('request');
request.debug = this.options.verbose;
request({
baseUrl: this.snInstanceURL,
method: 'GET',
// Use ServiceNow table API (http://wiki.servicenow.com/index.php?title=Table_API#gsc.tab=0) to get comments.
// The ServiceNow table API users a parameter called sysparm_query to query back end data. We can use a
// Similar approach to invoke any table API method.
uri: 'api/now/v2/table/sys_journal_field?sysparm_query=element_id%3D' + taskID + '%5EORDERBYDESCsys_created_on',
json: true,
headers: {
'Cookie': this.snCoookie
}
}, function (err, response, body) {
callBack(err, response, body);
});
}