-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (148 loc) · 4.42 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const fs= require('fs');
const path = require('path');
const https = require('https');
const sqlite3 = require('sqlite3');
const API = require('./apiAuth');
//trying to fix listener issue
require('events').EventEmitter.prototype._maxListeners = 100;
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const db = new sqlite3.Database('C:/Projects/NodeHookServer/jobs.db');
//const bee = 'Start-Job { C:/Projects/NodeHookServer/WorkerBee.ps1'
const bee = 'Start-Job -filepath "C:/Projects/NodeHookServer/WorkerBee.ps1" -ArgumentList'
const terminator = 'Start-Job -filepath "C:/Projects/NodeHookServer/Terminator.ps1" -ArgumentList'
const { PowerShell } = require('node-powershell');
//initialize a shell instance
const ps = new PowerShell({
executionPolicy: 'Bypass',
noProfile: true
});
//catch-up on missed new hire jobs on server startup
ps.invoke('Start-Job -filepath "C:/Projects/NodeHookServer/WorkerBeeCatchup.ps1"');
//when POST on newemployee API (no longer using this, triggers too late)
app.post('/api/newemployee', API.authenticateKey, (req, res) => {
res.status(200).send({
data: 'accepted',
});
console.log("New Employee API:");
console.log(req);
/*
let new_emp_id = parseInt(req.body.Object_Identifier);
console.log(new_emp_id);
let sql = 'INSERT INTO new_employees VALUES(?,?,?,?)';
let date = new Date();
let fullDateTime = `${date}`;
db.run(sql, new_emp_id, '', '', fullDateTime, function(err) {
if(err)
{
return console.log(err.message);
}
else
{
console.log('can create');
//ps.invoke(bee+' '+new_hire_id+'}');
ps.invoke(bee+' '+new_emp_id);
console.log('finished');
return
}
});
*/
});
//when POST on termdate API
app.post('/api/termdate', API.authenticateKey, (req, res) => {
res.status(200).send({
data: 'accepted',
});
console.log("Term Date API:");
console.log(req);
let termed_emp_id = parseInt(req.body.Object_Identifier);
let sql = 'INSERT INTO terminations VALUES(?,?,?,?,?)';
let date = new Date();
let fullDateTime = `${date}`;
db.run(sql, termed_emp_id, '', '', fullDateTime, '', function(err) {
if(err)
{
console.log('update');
//return console.log(err.message);
ps.invoke(terminator+' '+termed_emp_id);
}
else
{
console.log('new');
//ps.invoke(bee+' '+new_hire_id+'}');
ps.invoke(terminator+' '+termed_emp_id);
//console.log('finished');
return
}
});
});
//when POST on newhire API
app.post('/api/newhire', API.authenticateKey, (req, res) => {
res.status(200).send({
data: 'accepted',
});
console.log("New Hire API:");
console.log(req);
let new_hire_id = parseInt(req.body.Object_Identifier);
console.log(new_hire_id);
let sql = 'INSERT INTO new_hires VALUES(?,?,?,?)';
let date = new Date();
let fullDateTime = `${date}`;
db.run(sql, new_hire_id, '', '', fullDateTime, function(err) {
if(err)
{
return console.log(err.message);
}
else
{
console.log('can create');
//ps.invoke(bee+' '+new_hire_id+'}');
ps.invoke(bee+' '+new_hire_id);
console.log('finished');
return
}
});
});
//Staff departure, will need to reference the staff change event and check for an updated end date?
/*
app.post('/api/staffchange', API.authenticateKey, (req, res) => {
res.status(200).send({
data: 'accepted',
});
let staff_id = parseInt(req.body.Object_Identifier);
console.log(staff_id);
let sql = 'INSERT INTO terminations VALUES(?,?,?,?)';
let date = new Date();
let fullDateTime = `${date}`;
db.run(sql, staff_id, '', '', fullDateTime, function(err) {
if(err)
{
return console.log(err.message);
}
else
{
console.log('can create');
ps.invoke(terminator+' '+staff_id+'}');
console.log('finished');
return
}
});
});
*/
const httpsOptions = {
cert: fs.readFileSync('PATH_TO_YOUR_cert.pem'),
ca: fs.readFileSync('PATH_TO_YOUR_fullchain.pem'),
key: fs.readFileSync('PATH_TO_YOUR_privkey.pem')
};
const httpsServer = https.createServer(httpsOptions,app);
httpsServer.listen(8080, () => {
console.log('HTTPS Server running on port 8080');
});
/*
let server = app.listen(8080, function() {
console.log('Listening on port %d', server.address().port);
});
*/