-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
310 lines (251 loc) · 9.49 KB
/
server.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const express = require('express');
const app = express();
const { config } = require('process');
//Read credentials and API Urls from config.json file, see config.json.sample for details
var CONFIG = require('./config.json');
//Prepare OAuth2 request
var ClientOAuth2 = require('client-oauth2')
var calmAuth = new ClientOAuth2({
clientId: CONFIG.client_id,
clientSecret: CONFIG.client_secret,
accessTokenUri: CONFIG.auth_url
})
app.use(express.static('static'));
app.use(express.json());
app.get('/test', function (req, res) {
//See if the server is running and OAuth2 is working...
res.send('Hello World!');
//Perform OAuth2 call and log access token...
calmAuth.credentials.getToken()
.then(function (user) {
console.log(user.accessToken) //=> { accessToken: '...', tokenType: 'bearer', ... }
})
});
app.get('/getprojects', function (req, res) {
var projects = [];
console.log('Get Projects called');
//Perform OAuth Request to get Bearer Token
calmAuth.credentials.getToken()
.then(function (user) {
//The access token is provided in the results of the OAuth request
const axiosConfig = {
headers: { Authorization: `Bearer ${user.accessToken}` }
};
//Using Axios for the actual API Calls
var apiCall = require('axios');
//Make the actual API Call using the OAuth token retrieved before
apiCall.get(
CONFIG.api_baseurl + '/api/calm-projects/v1/projects',
axiosConfig
).then((apiResponse) => {
//The response contains the JSON with the project details...
//console.log(apiResponse.data);
//Filter out projects that are not open (O = Open, C = Closed/Hidden)
apiResponse.data.forEach(element => {
if (element.status == 'O') {
var data = {
id: element.id,
name: element.name
};
projects.push(data);
}
});
res.send(projects);
}, (apiError) => {
//Hand over the error response from the API to the client
console.log('Error Code: ' + apiError.response.status);
res.send(apiError.response.status, error.response.data);
console.log(apiError);
}).catch(function (error) { //Add catch on API failure
res.status(500).json(error);
})
});
});
app.post('/createTask', function (req, res) {
console.log('Create task called');
//console.log(req.body);
//Perform OAuth Request to get Bearer Token
calmAuth.credentials.getToken()
.then(function (user) {
//The access token is provided in the results of the OAuth request
const axiosConfig = {
headers: { Authorization: `Bearer ${user.accessToken}` }
};
//Using Axios for the actual API Calls
var apiCall = require('axios');
//Pass through the body parameters from the client to the API Call
const bodyParameters = req.body;
apiCall.post(
CONFIG.api_baseurl + '/api/calm-tasks/v1/tasks/',
bodyParameters,
axiosConfig
).then((apiResponse) => {
//console.log(apiResponse.data);
res.send(apiResponse.data);
}, (apiError) => {
console.log('Error Code: ' + apiError.response.status);
res.send(apiError.response.status, apiError.response.data);
console.log(apiError);
});
});
});
app.patch('/editTask', function (req, res) {
const url = require('url');
const queryObject = url.parse(req.url, true).query;
//console.log(queryObject);
console.log('Edit task called');
console.log(' Task ID: ' + queryObject.taskid);
console.log(' New Title: ' + req.body.title);
console.log(' New Status: ' + req.body.status);
//Perform OAuth Request to get Bearer Token
calmAuth.credentials.getToken()
.then(function (user) {
//The access token is provided in the results of the OAuth request
const axiosConfig = {
headers: { Authorization: `Bearer ${user.accessToken}` }
};
const bodyParameters = {
title: req.body.title,
status: req.body.status
};
var apiCall = require('axios');
apiCall.patch(
CONFIG.api_baseurl + '/api/calm-tasks/v1/tasks/' + queryObject.taskid,
bodyParameters,
axiosConfig
).then((apiResponse) => {
//console.log(apiResponse.data);
res.send(apiResponse.data);
}, (apiError) => {
console.log('Error Code: ' + apiError.response.status);
res.send(apiError.response.status, apiError.response.data);
console.log(apiError);
});
});
});
app.get('/gettasks', function (req, res) {
const url = require('url');
const queryObject = url.parse(req.url, true).query;
console.log('Get Tasks called');
var tasks = [];
//Perform OAuth Request to get Bearer Token
calmAuth.credentials.getToken()
.then(function (user) {
//The access token is provided in the results of the OAuth request
const axiosConfig = {
headers: { Authorization: `Bearer ${user.accessToken}` }
};
var apiCall = require('axios');
//Checking if the user wants to display Template Tasks
if (queryObject.istmpl == "true") {
apiCall.get(
CONFIG.api_baseurl + '/api/calm-tasks/v1/tasks?projectId=' + queryObject.projectid,
axiosConfig
).then((apiResponse) => {
//The response contains the JSON with the project details...
//console.log(apiResponse.data);
//Filter out tasks with the wrong status
apiResponse.data.forEach(element => {
//console.log(element);
if (element.status == queryObject.status && element.type != 'CALMTMPL') {
//console.log("SENT");
tasks.push(element);
}
// Temporary exceptions for User Stories
if (element.status == 'CIPUSOPEN' && queryObject.status == 'CIPTKOPEN') {
//console.log("SENT");
tasks.push(element);
}
else if (element.status == 'CIPUSINP' && queryObject.status == 'CIPTKINP') {
//console.log("SENT");
tasks.push(element);
}
else if (element.status == 'CIPUSCLOSE' && queryObject.status == 'CIPTKCLOSE') {
//console.log("SENT");
tasks.push(element);
}
});
//send results
//console.log(tasks);
res.send(tasks);
}, (apiError) => {
//Hand over the error response from the API to the client
console.log('Error Code: ' + apiError.response.status);
res.send(apiError.response.status, error.response.data);
console.log(apiError);
});
} else {
apiCall.get(
CONFIG.api_baseurl + '/api/calm-tasks/v1/tasks?projectId=' + queryObject.projectid,
axiosConfig
).then((apiResponse) => {
//The response contains the JSON with the project details...
//console.log(apiResponse.data);
//Filter out tasks with the wrong status
apiResponse.data.forEach(element => {
//console.log(element);
if (element.status == queryObject.status) {
//console.log("SENT");
tasks.push(element);
}
// Temporary exceptions for User Stories
if (element.status == 'CIPUSOPEN' && queryObject.status == 'CIPTKOPEN') {
//console.log("SENT");
tasks.push(element);
}
else if (element.status == 'CIPUSINP' && queryObject.status == 'CIPTKINP') {
//console.log("SENT");
tasks.push(element);
}
else if (element.status == 'CIPUSCLOSE' && queryObject.status == 'CIPTKCLOSE') {
//console.log("SENT");
tasks.push(element);
}
});
//send results
//console.log(tasks);
res.send(tasks);
}, (apiError) => {
//Hand over the error response from the API to the client
console.log('Error Code: ' + apiError.response.status);
res.send(apiError.response.status, error.response.data);
console.log(apiError);
});
}
});
});
app.get('/tasks', function (req, res) {
const url = require('url');
const queryObject = url.parse(req.url, true).query;
//Perform OAuth Request to get Bearer Token
calmAuth.credentials.getToken()
.then(function (user) {
//The access token is provided in the results of the OAuth request
const axiosConfig = {
headers: { Authorization: `Bearer ${user.accessToken}` }
};
var apiCall = require('axios');
apiCall.get(
CONFIG.api_baseurl + '/api/calm-tasks/v1/tasks/' + queryObject.taskid,
axiosConfig
).then((apiResponse) => {
//The response contains the JSON with the project details...
//console.log(apiResponse.data);
//send results
//console.log(tasks);
res.send(apiResponse.data);
}, (apiError) => {
//Hand over the error response from the API to the client
console.log('Error Code: ' + apiError.response.status);
res.send(apiError.response.status, error.response.data);
console.log(apiError);
});
});
});
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost'
app.listen(port, function () {
console.log('kanban4calmserver listening on port ' + port);
console.log('to access the application use http://' + host + ':'+ port)
//console.log(process.env);
});