-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.js
More file actions
70 lines (60 loc) · 2.16 KB
/
handler.js
File metadata and controls
70 lines (60 loc) · 2.16 KB
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
'use strict';
const https = require('https')
module.exports.main = async event => {
const BASE_URL = `api.${process.env.ENV}.auckland.ac.nz`;
// POST (Create) a new ServiceNow ticket
if (event.httpMethod === "POST" && event.body) {
// TODO: Enable POST to ServiceNow
return {
statusCode: 200,
body: JSON.stringify({
message: 'Creating ticket',
object: event.body
})
};
}
// GET a ServiceNow ticket by ticket ID URL parameter
if (event.queryStringParameters && event.queryStringParameters.ticketId) {
try {
// TODO: Replace hardcoded ticket with ${event.queryStringParameters.ticketId}
return await getRes(`/service/servicenow-readonly/table/u_request?sysparm_query=number=REQ1216647&sysparm_display_value=all`, process.env.SN_API_KEY_R)
.then(res => ([res] = res.result) ? // Destructure to first object in result array (first ticket)
{ statusCode: 200, body: JSON.stringify(res) } :
{ statusCode: 500, body: JSON.stringify('Error retrieving ticket from ServiceNow') })
} catch (error) {
console.error(error);
return { statusCode: 500, body: JSON.stringify('Task failed successfully: ', error) };
}
}
// Default '/' page
return {
statusCode: 200,
body: JSON.stringify({
message: 'Welcome to serverless-now',
aws_message: process.env.EXAMPLE_KEY
})
};
// Function for getting data and returning the JSON result
// Will make a POST request if the optional data argument is passed
async function getRes(path, apiKey, data = null) {
// Request options
const options = {
method: data ? 'POST' : 'GET',
hostname: BASE_URL,
path: path,
headers: {
apiKey: apiKey
}
};
return new Promise((resolve, reject) => {
let request = https.request(options, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => resolve(JSON.parse(body)));
res.on('error', e => reject((e)));
});
data && request.write(JSON.stringify(data)) || request.end(); // Optionally write POST data then execute
});
}
};