-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass_server.js
More file actions
377 lines (339 loc) · 10.8 KB
/
pass_server.js
File metadata and controls
377 lines (339 loc) · 10.8 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
var http = require('http');
const express = require("express");
const app = express();
const bodyparser = require("body-parser");
const PassFactory = require("./core/pass_factory");
const DataIntegrity = require("./core/data_integrity");
const RTManager = require("./core/rt_manager");
const BlockCalculator = require("./core/block_calculator");
const TransitHandler = require("./core/transit_handler");
const ProdMode = require('./core/prod_mode');
const DataLoader = require('./core/data_loader');
/*
* These are the only 2 lines of code that are needed
* to start up the websocket server.
*/
var theRTManager;
var pm;
var theTransitHandler;
const DBHandler = require("./core/db_handler");
var x;
async function foo() {
try {
theRTManager = new RTManager(1337);
await theRTManager.initialize();
/* Initializing the transithandler. */
console.log("Initializing the Transit Handler...");
pm = new ProdMode();
theTransitHandler=new TransitHandler();
console.log("HERE");
await theTransitHandler.initialize();
console.log("Done initializing TransitHandler.");
theRTManager.setTransitHandler(theTransitHandler);
console.log("XXXXXXXXXXXXXXXXXXXX");
x = new DBHandler();
x.initialize();
} catch (e) {
console.log("eeee->" + e.stack);
}
}
console.log("before foo");
foo();
console.log("starting server...");
//Middle ware
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: false }));
var publicDir = require('path').join(__dirname,'/public');
console.log("PUBLIC DIR->" + publicDir);
app.use(express.static(publicDir));
app.set('view engine', 'ejs');
app.get("/hello_world", (req, res) => {
(async() => {
res.status(200).send("Hello Jane.");
} )();
});
app.get('/', (req, res)=>{
// The render method takes the name of the HTML
// page to be rendered as input
// This page should be in the views folder
// in the root directory.
res.render('home');
});
app.get('/home2', (req, res)=>{
// The render method takes the name of the HTML
// page to be rendered as input
// This page should be in the views folder
// in the root directory.
res.render('home2');
});
console.log("setting up get_faculty_for");
app.get('/get_faculty_for', (req, res)=>{
var fid = req.query.facultyId;
var fem = req.query.facultyEmail;
var res;
if ( fid != null ) {
res = thePassFactory.theSchoolFactory.theFacultyHandler.theFaculty.get(parseInt(fid));
} else {
res = thePassFactory.theSchoolFactory.theFacultyHandler.theFacultyByEmail.get(fem);
}
if ( res==null ) { res="not found"; }
res.status(200).send("Here->" + JSON.stringify(res));
});
console.log("get_master_schedule_for");
app.get('/get_master_schedule_for', (req, res)=>{
var pd = req.query.courseView;
res.status(200).send("Here->" + JSON.stringify(thePassFactory.theSchoolFactory.theMasterScheduleHandler.theMasterSchedule.get(pd)));
});
console.log("get_student_for");
app.get('/get_student_for', (req, res)=>{
var pd = req.query.studentId;
console.log("getting student for->"+ pd);
res.status(200).send("Here->" + JSON.stringify(thePassFactory.theSchoolFactory.theStudentHandler.theStudents.get(parseInt(pd))));
});
/*
* This provides a direct link to user dev card.
*/
console.log("mail_test...");
app.get("/mail_test", (req, res)=>{
var name = req.query.name;
var title= req.query.title;
var data={};
data.name = name;
data.title = title;
res.status(200).send(JSON.stringify(data));
});
console.log("mail_passes");
app.get("/mail_pases", (req, res)=>{
res.status(200).send(JSON.stringify(data));
});
app.get('/page1', (req, res)=>{
res.render('page1');
});
app.get('/scanhome', (req, res)=>{
res.render('scanhome');
});
app.get('/page2', (req, res) => {
res.render('page2');
});
console.log("initialize_data");
app.post("/initialize_data", (req, res)=>{
(async() => {
var j = JSON.stringify(req.body);
var data = JSON.parse(j);
console.log("in initialize_data call->" + JSON.stringify(data));
try {
await thePassFactory.initialize( data.forDate);
console.log("DONE INITIALIZING PASSFACTORY");
} catch(e) {
console.log("ERRORx->" + e.stack);
}
res.status(200).send(JSON.stringify(DataIntegrity.issues));
} )();
});
app.post("/email_passes", (req, res)=>{
(async() => {
console.log("in email_passes");
try {
await thePassFactory.emailPasses();
console.log("EMAIL PASSES");
} catch(e) {
console.log("ERRORx->" + e.stack);
}
res.status(200).send({"message" : "success"});
} )();
});
app.post("/email_test_passes", (req, res)=>{
(async() => {
console.log("in email_test_passes");
try {
await thePassFactory.emailTestStudentPass();
console.log("EMAIL TEST PASSES");
} catch(e) {
console.log("ERRORx->" + e.stack);
}
res.status(200).send({"message" : "success"});
} )();
});
console.log("get_student_data");
app.post("/get_student_data", (req, res)=>{
console.log("in get_student_data call");
(async() => {
var j = JSON.stringify(req.body);
var data = JSON.parse(j);
res.status(200).send(JSON.stringify(Array.from(thePassFactory.theSchoolFactory.theStudentHandler.theStudents.values())));
} )();
});
console.log("get_student_block_names");
app.post("/get_student_block_names", (req, res)=>{
console.log("in get_student_block_names call");
(async() => {
var j = JSON.stringify(req.body);
var data = JSON.parse(j);
var rr= Array.from(thePassFactory.theSchoolFactory.theMasterScheduleHandler.allStudentBlockNames.entries());
res.status(200).send(JSON.stringify(rr));
} )();
});
console.log("get_passes");
app.post("/get_passes", (req, res)=>{
console.log("in get_passes call");
(async() => {
var j = JSON.stringify(req.body);
var data = JSON.parse(j);
var rr= Array.from(thePassFactory.thePassHandler.thePasses.values());
res.status(200).send(JSON.stringify(rr));
} )();
});
console.log("get_decorate_passes");
app.post("/get_decorated_passes", (req, res)=>{
console.log("in get_decorated_passes call");
(async() => {
var j = JSON.stringify(req.body);
var data = JSON.parse(j);
var rr=await thePassFactory.getDecoratedPasses();
res.status(200).send(JSON.stringify(rr));
} )();
});
app.post("/get_rt_faculty", (req, res)=>{
console.log("get_rt_faculty");
(async() => {
var j = JSON.stringify(req.body);
var rr = Array.from(RTManager.schoolFactory.theFacultyHandler.theFaculty);
res.status(200).send(JSON.stringify(rr));
} )();
});
app.post("/get_rt_temp_users", (req, res)=>{
console.log("get_rt_temp_users");
(async() => {
var j = JSON.stringify(req.body);
var rr = await DataLoader.getTempUserData();
res.status(200).send(JSON.stringify(rr));
} )();
});
app.post("/get_rt_room_info", (req, res)=>{
console.log("get_rt_room_info");
(async() => {
var j = JSON.stringify(req.body);
var rr = await DataLoader.getRoomInfoData();
res.status(200).send(JSON.stringify(rr));
} )();
});
app.post("/add_rt_temp_user", (req, res)=>{
console.log("add_rt_temp_user");
(async() => {
var j = JSON.stringify(req.body);
var data=JSON.parse(j);
var rr = await DataLoader.addTempUser(data.name);
res.status(200).send(JSON.stringify(rr));
} )();
});
app.post("/get_rt_students", (req, res)=>{
console.log("get_rt_students");
(async() => {
var j = JSON.stringify(req.body);
var rr = Array.from(RTManager.schoolFactory.theStudentHandler.theStudents);
res.status(200).send(JSON.stringify(rr));
} )();
});
app.post("/get_rt_rooms", (req, res)=>{
console.log("get_rt_rooms");
(async() => {
var j = JSON.stringify(req.body);
var rr = Array.from(RTManager.schoolFactory.theRoomHandler.theRooms);
res.status(200).send(JSON.stringify(rr));
} )();
});
app.post("/get_rt_curr_block", (req, res)=>{
console.log("get_rt_curr_block");
(async() => {
var now = new Date();
var ab = "A";
if ( BlockCalculator.isBDay() ) { ab="B"; }
var b = BlockCalculator.getCurrBlockString(now);
res.status(200).send(JSON.stringify({ block:b, ABDay: ab }));
} )();
});
app.post("/get_report_data", (req, res)=>{
console.log("in get_report_data");
(async() => {
var j = JSON.stringify(req.body);
console.log("data->" + j);
var data = JSON.parse(j);
var ret = await DataLoader.getReportData(data.repDate,data.location,data.block1,data.block2,data.blockLunch,data.block3,data.block4,data.block5, data.includePassing);
res.status(200).send(ret);
} )();
});
app.get("/send_test_email", (req, res)=>{
console.log("in send_test_email");
(async() => {
var j = JSON.stringify(req.body);
var data = JSON.parse(j);
var rr=await thePassFactory.theEmailHandler.sendTestEmail();
res.status(200).send("Sent test email");
} )();
});
app.post("/run_prod_batch", (req, res)=>{
console.log("in run_prod_batch");
(async() => {
// Try loading data maxTries times.
var maxTries=3;
var atTry=1;
var stillTrying = true;
res.status(200).send(JSON.stringify({"message":"Kicked Job Off"}));
while ( stillTrying ) {
try {
var x = new Date();
/* if after 2pm then add a day. */
console.log("hours->" + x.getHours());
if ( x.getHours() >= 13 ) {
x.setDate(x.getDate()+1);
}
console.log("x->" + x);
var y = x.getFullYear();
var m = x.getMonth(); m++; if ( m.toString().length == 1) { m="0" + m.toString();}
var d = x.getDate(); if ( d.toString().length == 1 ) { d = "0" + d.toString();}
var dtStr = y + "-" + m + "-" + d ;
console.log("Running Prod with ->" + dtStr);
thePassFactory = new PassFactory();
await thePassFactory.initialize( dtStr );
console.log("DONE INITIALIZING PASSFACTORY At Try#->" + atTry);
if ( ! BlockCalculator.isADay() && !BlockCalculator.isBDay() ) {
console.log("not a school day");
var rr=await thePassFactory.theEmailHandler.sendProdStatusEmail("Non School Day","Production ran but it is not a school day (" + dtStr + ") so no mailings.");
stillTrying = false;
} else {
var j = JSON.stringify(req.body);
var data = JSON.parse(j);
if ( data.flag == "ERROR") {
var e = new Error("test");
throw e;
}
console.log("EMAILING PASSES");
var resStr=await thePassFactory.emailPassesWithRetry(3);
console.log("DONE EMAILING PASSES");
var rr=await thePassFactory.theEmailHandler.sendProdStatusEmail("PassFactory: Success" ,"Emails successful on try#->" + atTry + "\n" + resStr);
stillTrying=false;
}
} catch ( e ) {
console.log(e.stack);
var rr=await thePassFactory.theEmailHandler.sendProdStatusEmail("Error",e.stack);
if ( atTry < maxTries ) {
stillTrying=true;
} else {
stillTrying=false;
}
atTry++;
}
}
} )();
console.log("leaving prod run");
});
/*
* Start the passFactory
*/
console.log("starting PassFactory...");
var thePassFactory = new PassFactory();
/*
* Startup the server!
*/
var http_port = 8002;
http.createServer(app).listen(http_port);