-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
826 lines (727 loc) · 29.9 KB
/
app.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//app.js
/*
===GET routes/views===
[view]get all po boxes by customer email.
===POST routes/views===
[route]post login request -> responds with a token
[route]post a user login
[route]post a customer
[route]post a employee
[route]post a shipment (when a shipment is created by employee,
it attaches the employee email, customer email,
tracking status (hardcode this), creation date,
num packages, region (hardcode this), shipment status,
current location -> branch,
)
- when 10 shipments are in store and the 11th is made -> trigger (store is full).
NOTE -> PO boxes are initially hardcode (ex: branch A has 10 po boxes that are all unowned).
NOTE -> branches are hardcoded and the system will not control that.
===UPDATE/PUT/PATCH routes===
NOTE-> (no deletions will be made, we will mark it as deleted within the table)
[route]update shipment status (current location) -> trigger to send email.
[route]update shipment location.
[route]update po box customer email when bought.
[route]delete employee
[route]delete shipment
*/
const http = require("http");
const url = require('url');
const { UserController } = require("./Controllers/userController");
const { ShipmentController } = require("./Controllers/shipmentController");
const { POBoxController } = require("./Controllers/poBoxController");
const { TracksController } = require("./Controllers/tracksController");
const { JobController } = require("./Controllers/jobController");
const { locHistController } = require("./Controllers/locHistController");
const { ReportController } = require("./Controllers/reportController");
const { authenticate, init_jwt } = require("./jwt");
const { getReqData } = require("./utils");
//USAGE: read JSON to parse ex:
// const data = await getReqData(req);
const PORT = process.env.PORT || 5000;
// const PORT = process.env.PORT || 3000;
const server = http.createServer(async (req, res) => {
const reqUrl = url.parse(req.url, true);
const path = reqUrl.path;
const method = req.method;
console.log(`Route hit: ${path}`);
console.log(method);
// HANDLE CORS PREFLIGHT REQUEST
if (method === "OPTIONS") {
res.writeHead(204, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, DELETE, PUT, PATCH",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, authorization",
"Access-Control-Max-Age": 2592000
});
res.end();
return;
}
//GET HANDLERS
//Testing home to return 'Hello World'
else if (path === "/" && method === "GET") {
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json" });
// send the data
res.end(JSON.stringify("Hello Post Office"));
}
// /api/users : GET
else if (path === "/api/users" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
// get the users
const users = await new UserController().getAllUsers();
// send the data
res.end(JSON.stringify(users));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//get all customers
// /api/customers : GET
else if (path === "/api/customers" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
// get the users
const users = await new UserController().getAllCustomers();
// send the data
res.end(JSON.stringify(users));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Get all shipments route
else if (path === "/api/shipments" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
let shipments = await new ShipmentController().getAllShipments();
//send the shipments
res.end(JSON.stringify(shipments));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Get all employees route
else if (path === "/api/employees" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
let employees = await new UserController().getAllEmployees();
//send the employees payload
res.end(JSON.stringify(employees));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// api/po-boxes : GET
// Get all po boxes route
else if (path === "/api/po-boxes" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
let boxes = await new POBoxController().getAllPOBoxes();
//send the boxes
res.end(JSON.stringify(boxes));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// Get all tracks
else if (path === "/api/tracks" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
let tracks = await new TracksController().getAllTracks();
//send the tracks
res.end(JSON.stringify(tracks));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//POST HANDLERS
// Returns customer info with a email input
// /api/userinfo : POST
else if (path === "/api/userinfo" && method === "POST") {
try {
// set the status code and content-type
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Request-Method", "POST");
res.setHeader("Access-Control-Request-Headers", "Content-Type");
// Receiving input data
const data = await getReqData(req);
const user_email = JSON.parse(data)
//console.log(user_email);
const user_info = await new UserController().getUserByEmail(user_email.email);
//console.log(user_info);
if (!user_info) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify(`No user found: ${user_email.email}`));
}
else {
res.writeHead(202, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
res.end(JSON.stringify(user_info));
}
} catch (error) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// returns employee info
//api/employee-info POST (email as input)
else if (path === "/api/employee-info" && method === "POST") {
try {
// set the status code and content-type
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Request-Method", "POST");
res.setHeader("Access-Control-Request-Headers", "Content-Type");
// Receiving input data
const data = await getReqData(req);
const user_email = JSON.parse(data)
//console.log(user_email);
const user_info = await new UserController().getEmployeeByEmail(user_email.email);
//console.log(user_info);
if (!user_info) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify(`No user found: ${user_email.email}`));
}
else {
res.writeHead(202, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
res.end(JSON.stringify(user_info));
}
} catch (error) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Given customer email, return box num and branch address
// /api/userbox : POST
else if (path === "/api/userbox" && method === "POST") {
try {
// set the status code and content-type
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Request-Method", "POST");
res.setHeader("Access-Control-Request-Headers", "Content-Type");
// Receiving input data
const data = await getReqData(req);
const user_email = JSON.parse(data);
//console.log(user_email);
const user_info = await new POBoxController().getPOBoxByEmail(user_email.email);
//console.log(user_info);
if (!user_info) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify(`No user found: ${user_email.email}`));
}
else {
res.writeHead(202, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
res.end(JSON.stringify(user_info));
}
} catch (error) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// Returns shipment with a tracking id input
// /api/shipment : POST
else if (path === "/api/shipment" && method === "POST") {
try {
// set the status code and content-type
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Request-Method", "POST");
res.setHeader("Access-Control-Request-Headers", "Content-Type");
const data = await getReqData(req);
//const tracking_id = JSON.parse(data);
const tracking_id = JSON.parse(data)
console.log(tracking_id);
const shipment = await new ShipmentController().getShipmentByID(tracking_id.tracking_id);
console.log(shipment);
if (!shipment) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify(`No shipment found with TID: ${tracking_id.tracking_id}`));
}
else {
res.writeHead(202, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
res.end(JSON.stringify(shipment));
}
} catch (error) {
res.writeHead(404, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// creates a customer with a user login
// /api/register-customer : POST
else if (path === "/api/register-customer" && method === "POST") {
try {
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
console.log(data);
const result = await new UserController().createCustomer(data);
// status code 201 -> created
res.end(result);
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// creates an employee with a user login
// /api/register-employee : POST
else if (path === "/api/register-employee" && method === "POST") {
try {
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
console.log(data);
const result = await new UserController().createEmployee(data);
res.end(result);
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// /api/login : POST
else if (path === "/api/login" && method === "POST") {
try {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
//receive email/password and check in db
//create JWT and return it to the frontend
// (then every protected route uses the JWT for its role)
const data = await getReqData(req);
const user = JSON.parse(data);
// console.log(user);
//get user.type from database
const role = await new UserController().getUserType(user);
// console.log(role);
const verified_user = { ...user, ...role }
//console.log(verified_user);
if (!verified_user.type) {
throw new Error('Wrong Email/Password Combination.');
}
//UNCOMMENT for debugging
// const temp_user =
// {
// type: "admin"
// }
//console.log(init_jwt(verified_user));
res.end(JSON.stringify({ token: init_jwt(verified_user), role: verified_user.type, email: verified_user.email }));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// /api/create-shipment : POST
// API route for creating a shipment in the database
else if (path === "/api/create-shipment" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const result = await new ShipmentController().createShipment(data);
res.end(result);
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//DEPRECATED on single use
// /api/add-tracks : POST
// API route for creating a tracks in the database
else if (path === "/api/add-tracks" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const result = await new TracksController().createTracks(data);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//given user email, retrieve : {shipment_tracking_id, tracking_status, est_delivery_date} from TRACKS,
// {shipment_status, num_packages} from SHIPMENT
// api/user-shipments : POST
else if (path === "/api/user-shipments" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const user_email = JSON.parse(data);
//console.log(user_email);
const result = await new UserController().getUserShipmentsByEmail(user_email.email);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Marks a shipment as deleted.
// api/delete-shipment : PUT
else if (path === "/api/delete-shipment" && method === "PUT") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = JSON.parse(await getReqData(req));
const tracking_id = data.tracking_id;
const isDeleted = data.mark_deletion;
const result = await new ShipmentController().deleteShipment(tracking_id, isDeleted);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Updates the status of a shipment.
// api/update-status : PUT
else if (path === "/api/update-status" && method === "PUT") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = JSON.parse(await getReqData(req));
const result = await new ShipmentController().updateShipmentStatus(data.tracking_id, data.status, data.location);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Update status of a shipment
// api/update-shipment : PUT
else if (path === "/api/update-shipment" && method === "PUT")
{
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = JSON.parse(await getReqData(req));
if (data.key === 'tracking_id') {
throw new Error('Cannot update the tracking ID.');
}
const result = await new ShipmentController().updateShipment(data.tracking_id, data.key, data.new_value);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Update an attribute of an employee.
// api/update-employee : PUT
else if (path === "/api/update-employee" && method === "PUT") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = JSON.parse(await getReqData(req));
if (data.key === 'email') {
throw new Error('Cannot update the email of an employee.');
}
const result = await new UserController().updateEmployee(data.email, data.key, data.new_value);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Update an attribute of a customer
// api/update-customer : PUT
else if (path === "/api/update-customer" && method === "PUT") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = JSON.parse(await getReqData(req));
if (data.key === 'email') {
throw new Error('Cannot update the email of a customer.');
}
const result = await new UserController().updateCustomer(data.email, data.key, data.new_value);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
//Update an attribute of a job
// api/update-job : PUT
else if (path === "/api/update-job" && method === "PUT") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = JSON.parse(await getReqData(req));
if (data.key === 'work_id') {
throw new Error('Cannot update the id of a job.');
}
const result = await new JobController().updateJob(data.id, data.key, data.new_value);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// POST Job
else if (path === "/api/job" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const result = await new JobController().createJob(data);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// get Job by id
else if (path === "/api/get-job-with-id" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = JSON.parse(await getReqData(req));
//console.log(data.id);
const result = await new JobController().getJobByID(data.id);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// GET All Jobs for all employee
else if (path === "/api/job" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
let job = await new JobController().getAllJobs();
//send the packages
res.end(JSON.stringify(job));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// GET Employee Self report (1 employee)
else if (path === "/api/self-report" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const result = await new JobController().getSelfReport(data);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// GET All Location history
else if (path === "/api/loc-history" && method === "GET") {
try {
// set the status code and content-type
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
let locHist = await new locHistController().getAllLocHist();
//send the packages
res.end(JSON.stringify(locHist));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// GET Loc History by tracking ID
else if (path === "/api/loc-history-id" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const result = await new locHistController().getLocHistbyID(data);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// GET Shipment report
else if (path === "/api/shipment-report" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const result = await new ReportController().getShipmentReport(data);
console.log("Result :" + result);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// GET Employee report
else if (path === "/api/employee-report" && method === "POST") {
try {
// set the status code and content-type
res.writeHead(201, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
const data = await getReqData(req);
const result = await new ReportController().getEmployeeReport(data);
console.log("Result :" + result);
res.end(JSON.stringify(result));
} catch (error) {
// set error status code and content-type
res.writeHead(500, { "Content-Type": "application/json" });
// send error
res.end(JSON.stringify({ message: error.message }));
}
}
// No route present
else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Route not found" }));
}
});
server.listen(PORT, () => {
console.log(`server started on port: ${PORT}`);
});