This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
230 lines (201 loc) · 7.53 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
// server.js : where your node app starts
// https://newton.ai/
// init project
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var https = require('https');
var _ = require('underscore');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies for POST requests
app.use(bodyParser.json()); // support json encoded bodies for POST requests
app.get("/", function (request, response) {
//response.sendFile(__dirname + '/views/index.html');
response.sendStatus(200);
});
// GET API for Mobile App
// Examples: /keywords?filter=engineer | /keyword?filter=analyst
// It calls https://mckesson.taleo.net/careersection/rest/suggestions/text?suggestId=1&careerPortalNo=101430233&language=en&term=
app.get("/keywords", function (request, response) {
/*//Test data
var test=['engineering','engineer','engineers','engine','engines','engineered'];
response.send(test);
return;*/
// // // //
var options = {
host: 'mckesson.taleo.net',
path: '/careersection/rest/suggestions/text?suggestId=1&careerPortalNo=101430233&language=en&term='+ request.query.filter,
method: 'GET',
//headers: {'custom': 'Custom Header Demo works'}
};
//Request sent to McKesson Server to et Keywords for auto-complete
var mckReq = https.request(options, function(mckRes){
var str = '';
mckRes.on('data', function (chunk) {
str += chunk;
});
mckRes.on('end', function () {
response.send(str);
console.log(str);
});
mckRes.on('error', function (err) {
response.send(err);
console.log('mck Error:::',err);
});
});
mckReq.end();
});
// GET/POST API for Zott Bot (http://zo.tt)
// Examples: /keywords?filter=engineer | /keyword?filter=analyst
// It calls https://mckesson.taleo.net/careersection/rest/suggestions/text?suggestId=1&careerPortalNo=101430233&language=en&term=
app.get("/keywordsbot", function (request, response) {GetKeywordsBot(request,response)});
app.post("/keywordsbot", function (request, response) {GetKeywordsBot(request,response)});
function GetKeywordsBot(request,response){
var options = {
host: 'mckesson.taleo.net',
path: '/careersection/rest/suggestions/text?suggestId=1&careerPortalNo=101430233&language=en&term='+ request.query.filter,
method: 'GET',
//headers: {'custom': 'Custom Header Demo works'}
};
//Request sent to McKesson Server to et Keywords for auto-complete
var mckReq = https.request(options, function(mckRes){
var str = '';
mckRes.on('data', function (chunk) {
str += chunk;
});
mckRes.on('end', function () {
var zottjson = _.object(['k1','k2','k3','k4','k5'],JSON.parse(str));
response.send(zottjson);
console.log(zottjson);
});
mckRes.on('error', function (err) {
response.send(err);
console.log('mck Error:::',err);
});
});
mckReq.end();
}
// Examples: /jobs?filter=engineer | /jobs?filter=analyst
app.get("/jobs", function (request, response) {
// create the JSON object for the Post Body
var template = require('./config/template1.json'); //read template request body from JSON config file
template.fieldData.fields.KEYWORD = request.query.filter; //Update the template with the keyword from the incoming request
var mckPostData = JSON.stringify(template); //The actual Request Body to be sent to MCK
//Assemble the MCK request 'Options' object to pass to the HTTPS POST Request
var mckReqOptions = {
host: 'mckesson.taleo.net',
path: '/careersection/rest/jobboard/searchjobs?lang=en&portal=101430233',
method: 'POST',
json : true,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(mckPostData)
},
body : {}
};
//Create the POST request (this is an Asynchronous call)
//i.e. this function would be called back by Nodejs server whenever it receives a response packet/error for this request
var mckReq = https.request(mckReqOptions, function(mckRes){
var allchunks ='';
mckRes.on('data', function (chunk) {
allchunks += chunk;
});
mckRes.on('end', function () {
var shortlist = extractJobs(JSON.parse(allchunks));
response.send(shortlist);
});
mckRes.on('error', function (err) {
response.send(err);
console.log('mck Error:::',err);
});
});
mckReq.on('error', function(e) { console.error(e); });
// Finally POST the data and end the request!
mckReq.write(mckPostData);
mckReq.end();
});
app.get("/jobsbot", function (request, response) { GetJobs(request,response); });
app.post("/jobsbot", function (request, response) { GetJobs(request,response); });
function GetJobs(request,response){
// create the JSON object for the Post Body
var template = require('./config/template1.json'); //read template request body from JSON config file
template.fieldData.fields.KEYWORD = request.query.filter; //Update the template with the keyword from the incoming request
var mckPostData = JSON.stringify(template); //The actual Request Body to be sent to MCK
//Assemble the MCK request 'Options' object to pass to the HTTPS POST Request
var mckReqOptions = {
host: 'mckesson.taleo.net',
path: '/careersection/rest/jobboard/searchjobs?lang=en&portal=101430233',
method: 'POST',
json : true,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(mckPostData)
},
body : {}
};
//Create the POST request (this is an Asynchronous call)
//i.e. this function would be called back by Nodejs server whenever it receives a response packet/error for this request
var mckReq = https.request(mckReqOptions, function(mckRes){
var allchunks ='';
mckRes.on('data', function (chunk) {
allchunks += chunk;
});
mckRes.on('end', function () {
var shortlist = extractJobs(JSON.parse(allchunks));
//Extract first 5 jobs asa new JSON object - j1 to j5
var obj = {}
obj = _.object(['j1','j2','j3','j4','j5'],shortlist);
//XLapp JSON_NVP understands only Flat JSON, so flatten the result by appending _
obj = flatten(obj,{});
//McKesson WS returns location values as "location": "[\"United States\"]"
//The " and [ cause security loopholes in Zott Bot server - so these are to be removed
//Remove the pattern [\" and \"] using regex
var str = JSON.stringify(obj);
str = str.replace(/\[\\\"/gm,'');
str = str.replace(/\\\"\]/gm,'');
str = str.replace(/\//gm,' ');
str = str.replace(/\\/gm,' ');
obj = JSON.parse(str);
console.log(obj);
response.send(obj);
});
mckRes.on('error', function (err) {
response.send(err);
console.log('mck Error:::',err);
});
});
mckReq.on('error', function(e) { console.error(e); });
// Finally POST the data and end the request!
mckReq.write(mckPostData);
mckReq.end();
}
//Function to flatten a JSON nested object
//ref: https://stackoverflow.com/questions/19628912/flattening-nested-arrays-objects-in-underscore-js
function flatten(x, result, prefix) {
if(_.isObject(x)) {
_.each(x, function(v, k) {
flatten(v, result, prefix ? prefix + '_' + k : k)
})
} else {
result[prefix] = x
}
return result
}
function extractJobs(allchunks){
var shortlist=[];
var row ={};
_.each(allchunks.requisitionList, function(job) {
row['jobid'] = job.jobId;
row['role'] = job.column[0];
row['location'] = job.column[1];
row['date'] = job.column[2];
row={};
shortlist.push(row);
});
return shortlist;
}
var port = process.env.PORT | 3000;
// listen for requests :)
var listener = app.listen(port, function () {
console.log('Your app is listening on port ', listener.address().port);
});