This repository has been archived by the owner on Nov 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
168 lines (144 loc) · 4.36 KB
/
handler.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
'use strict';
const rp = require('request-promise');
const _ = require('lodash');
const BMS_API_USERNAME = process.env.BMS_API_USERNAME;
const BMS_API_PASSWORD = process.env.BMS_API_PASSWORD;
const BMS_API_SERVER = process.env.BMS_API_SERVER ? process.env.BMS_API_SERVER : 'https://bms.kaseya.com'
const BMS_API_TENANT = process.env.BMS_API_TENANT;
const BMS_API_TOP = process.env.BMS_API_TOP ? process.env.BMS_API_TOP : "15";
const POWERBI_API = process.env.POWERBI_API;
global.access_token = '';
function web_call(method, url, payload) {
return new Promise(function (resolve, reject) {
const opt = {
method: method,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token
},
json: true
}
// ugh this is utter shite
if (url.indexOf("https") != 0) {
Object.assign(opt, { uri: BMS_API_SERVER + url });
} else {
Object.assign(opt, { uri: url });
}
if (url.indexOf("/api/token" >= 0)) {
Object.assign(opt, { form: payload });
} else {
Object.assign(opt, { body: payload });
}
if (access_token == '') {
delete opt.headers['Content-Type'];
delete opt.headers['Authorization'];
}
console.log('web_call(): calling ' + opt.uri);
rp(opt).then(function (res) {
resolve(res);
}).catch(function (err) {
console.log('web_call(): FAIL: ', err);
reject(err);
})
});
}
function login() {
return new Promise(function (resolve, reject) {
const login_Promise = web_call(
"POST",
"/api/token",
{
username: BMS_API_USERNAME,
password: BMS_API_PASSWORD,
grant_type: "password",
tenant: BMS_API_TENANT
}
);
login_Promise.then(function (res) {
access_token = res.access_token;
console.log('login(): access token received -> ', access_token);
resolve(access_token);
}).catch(function (err) {
console.log('login(): FAIL: ', err);
reject(false);
})
});
}
function getTickets() {
return new Promise(function (resolve, reject) {
const getTicket_Promise = web_call(
"GET",
"/api/servicedesk/tickets?$orderby=Id desc&$top=" + BMS_API_TOP,
{}
);
getTicket_Promise.then(function (res) {
var ticket_payload = res;
console.log('getTickets(): received payload, no. of results: ', ticket_payload.TotalRecords);
var reduced_payload = _.map(ticket_payload.Result, function(object) {
return _.pick(object, ['AccountName', 'DueDate', 'LastActivityUpdate', 'OpenDate', 'Queue', 'Status', 'TicketNumber', 'Title']);
});
var ticketPromises = [];
// _.forEach(reduced_payload, function(v) {
// // don't set json: true or it'll override encoding: null
// var opt = {
// method: "POST",
// uri: POWERBI_API,
// json: v
// }
// ticketPromises.push(
// rp(opt)
// .then(function(res) {
// console.log(`${v.TicketNumber}: ${res.statusCode}`);
// resolve('Success');
// }).catch(function (res) {
// console.log(`${v.TicketNumber}: ${res.statusCode}`);
// console.log(res);
// reject(res);
// })
// );
// });
var opt = {
method: "POST",
uri: POWERBI_API,
json: reduced_payload
}
ticketPromises.push(
rp(opt)
.then(function(res) {
resolve('Success');
}).catch(function (res) {
console.log(`fail`);
console.log(res);
reject(res);
})
);
Promise.all(ticketPromises).then(function() {
console.log("getTickets(): all promises should have triggered...");
resolve();
});
}).catch(function (err) {
console.log('getTickets(): sendToPowerBI(): FAIL: ', err);
reject(false);
});
});
}
module.exports.getBMStickets = async (event) => {
return new Promise((resolve) => {
console.log('getBMStickets: triggered');
login()
.then((token) => {
access_token = token;
getTickets()
.then((res) => {
console.log("ticket work done");
resolve({
statusCode: 200,
body: JSON.stringify({
message: 'job complete'
})
});
})
})
});
};