-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
1298 lines (989 loc) · 37.2 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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { JSDOM } = require("jsdom");
const { window } = new JSDOM("");
const $ = require("jquery")(window);
var bodyParser = require('body-parser')
const { check, body, validationResult } = require('express-validator');
const express = require('express');
const morgan = require('morgan');
const mysql = require('mysql');
const { response } = require("express");
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'wgsa_company'
});
// express app
const app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.set('view engine', 'ejs');
app.use(express.static('public'));
db.connect((err) => {
if (err) {
throw err;
}
console.log('my sql connected');
});
// listen for requests
app.listen(3000, () => {
console.log('server started on port 3000');
});
app.get('/', (req, res) => {
res.render('index');
});
app.get('/FAQS', (req, res) => {
const myquery1 = "SELECT Question_code, Question, Answer, Fname,Lname from wgsa_company.FAQs , wgsa_company.employee where Answered_by=Ssn";
db.query(myquery1, (err, result, field) => {
if (err) throw err;
res.render('FAQS', { result })
});
});
app.get('/Login', (req, res) => {
res.render('Login', { errmessage: '', successMes: '' })
});
app.get('/Recharge', (req, res) => {
res.render('Recharge')
});
app.get('/DataEmployee', (req, res) => {
res.render('DataEmployee', { successMes: '', errmessage: '' })
});
app.get('/HR', (req, res) => {
res.render('HR', { successMes: '', errmessage: '' })
});
app.get('/', (req, res) => {
res.render('index')
});
app.get('/CustomerService', (req, res) => {
let query = `select Fname,Lname from wgsa_company.employee where SSn=${895623}`;
let query2 = `select plan_name,plan_code from wgsa_company.plan `;
db.query(query, (err, emp) => {
if (err) {
console.log(err)
}
db.query(query2, (err, plans) => {
if (err) {
console.log(err)
return res.render('404', { err });
}
res.render('CustomerService', { plans, result: '', errmessage: '', successMes: '' })
});
});
});
app.post('/getCustomerinfo', urlencodedParser, (req, res) => {
const user_number = req.body.Phone_number;
const myquery1 = `Select * from wgsa_company.customer
where customer.Phone_num=${user_number}`;
db.query(myquery1, (err, result, field) => {
if (err) {
console.log(err);
throw err;
}
if (result[0].Renewal_date) {
result[0].Renewal_date = result[0].Renewal_date.toString().substr(0, 15);
}
let query2 = `select * from wgsa_company.plan `;
db.query(query2, (err, plans) => {
if (err) {
console.log(err)
return res.render('404', { err });
}
console.log(plans);
console.log(result);
if (result[0].Gender) {
if (result[0].Gender == 'M')
result[0].Gender = 'Male';
else
result[0].Gender = 'Female';
}
res.render('CustomerService', { result, plans, errmessage: '', successMes: 'Done' })
});
});
});
app.post('/ViewCustomerData', urlencodedParser, (req, res) => {
var user_number = req.body.Phone_number;
let myquery1 = `Select * from wgsa_company.customer as c ,wgsa_company.plan as p
where c.plan_code=p.plan_code AND c.Phone_num=${user_number}`;
db.query(myquery1, (err, result, field) => {
if (err) {
console.log(err);
res.render('404', { err });
}
if (result[0].Gender) {
if (result[0].Gender == 'M')
result[0].Gender = 'Male';
else
result[0].Gender = 'Female';
}
console.log(result);
res.render('TechnicalSupport', { result, successMes: 'Done', errmessage: '' })
});
});
app.get('/Offers', (req, res) => {
let myquery1 = "SELECT Launch_date, Price, Minutes, Expire_date, Megas, Offer_describ, Offer_num from wgsa_company.Offer";
db.query(myquery1, (err, result, field) => {
if (err) throw err;
res.render('Offers', { result })
});
});
app.get('/plans', (req, res) => {
let myquery1 = "SELECT * from wgsa_company.plan";
db.query(myquery1, (err, result, field) => {
if (err) throw err;
res.render('plans', { result })
});
});
app.get('/Register', (req, res) => {
res.render('Register', { errmessage: '', successMes: '' })
});
app.get('/Customer', (req, res) => {
var user_number = us;
let myquery1 = `Select customer.Balance, customer.Renewal_date, customer.Gender ,customer.Fname,customer.Lname, plan.Plan_name,customer.Used_megas,customer.Used_min
from wgsa_company.plan,wgsa_company.customer
where customer.plan_code=plan.Plan_code and customer.Phone_num=${user_number}`;
db.query(myquery1, (err, result, field) => {
if (err) throw err;
if (result.length > 0) {
result[0].Renewal_date = result[0].Renewal_date.toString().substr(0, 15);
console.log(result);
res.render('Customer', { result })
}
});
});
app.post('/Update_Plan_Cost', urlencodedParser, function (req, res) {
var Plannewprice = req.body.Plan_New_Price;
var plancode = req.body.Plan_Code;
let myquery = `UPDATE wgsa_company.Plan SET Plan.Price= ${Plannewprice} where Plan.Plan_code=${plancode}`;
db.query(myquery, (err, result, field) => {
if (err) throw err;
console.log(result);
res.render('DataEmployee', { result, successMes: 'Done', errmessage: '' });
});
});
app.post('/Delete_Plan', urlencodedParser, function (req, res) {
var Plan_code = req.body.Plan_Code;
let myquery = `delete FROM wgsa_company.PLAN WHERE PLAN.PLAN_CODE =${Plan_code}`;
db.query(myquery, (err, result, field) => {
if (err) throw err;
console.log(result);
res.render('DataEmployee', { result, successMes: 'Done', errmessage: '' });
});
});
app.post('/Remove_customer', urlencodedParser, function (req, res) {
var phone_num = parseInt(req.body.Phone_num, 10);
function CheckNumbersEntered123() {
if (phone_num <= 0) {
return res.render('HR', { errmessage: 'Enter valid number please', successMes: '' });
}
else {
CheckCustomer123();
}
}
function CheckCustomer123() {
let query123 = `select *
from wgsa_company.customer
where customer.phone_num=${phone_num}; `;
db.query(query123, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length === 0) {
console.log("There is no customer with that phone number");
return res.render('HR', { errmessage: 'There is no customer with that phone number', successMes: '' });
}
else {
ExecuteDeleteCustomer();
}
});
}
function ExecuteDeleteCustomer() {
let myquery = `delete FROM wgsa_company.customer WHERE customer.Phone_num =${phone_num}`;
db.query(myquery, (err, result, field) => {
if (err) {
console.log(err)
res.render('404', { err });
}
console.log(result);
res.render('HR', { successMes: 'Done', errmessage: '' });
});
}
CheckNumbersEntered123();
});
app.post('/Remove_employee', urlencodedParser, function (req, res) {
var ssn = parseInt(req.body.SSN, 10);
function CheckssnEntered1233() {
if (ssn <= 0) {
return res.render('HR', { errmessage: 'Enter valid ssn please', successMes: '' });
}
else {
checkssnifexistbefore();
}
}
function checkssnifexistbefore() {
let query123 = `select *
from wgsa_company.employee
where employee.ssn=${ssn};`;
db.query(query123, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length === 0) {
console.log("There is no Employee with that ssn");
return res.render('HR', { errmessage: 'There is no Employee with that ssn', successMes: '' });
}
else {
ExecuteDeleteforEmployee();
}
});
}
function ExecuteDeleteforEmployee() {
let myquery = `delete FROM wgsa_company.EMPLOYEE WHERE EMPLOYEE.SSN =${ssn}`;
db.query(myquery, (err, result, field) => {
if (err) {
console.log(err)
res.render('404', { err });
}
console.log(result);
res.render('HR', { successMes: `The employee with SSN ${ssn} is deleted successfully`, errmessage: '' });
});
}
CheckssnEntered1233();
});
app.post('/Add_New_Plan', urlencodedParser, function (req, res) {
var Plan_name = req.body.Plan_name;
var Plan_code = req.body.Plan_code;
var Minutes = req.body.Minutes;
var Megas = req.body.Megas;
var Price = req.body.Price;
if (!Minutes)
Minutes = "NULL";
if (!Megas)
Megas = "NULL";
let myquery = `Insert into wgsa_company.plan (Plan_code, Price, Minutes, Megas, Plan_name) values (${Plan_code},${Price},${Minutes},${Megas},"${Plan_name.toString()}")`;
db.query(myquery, (err, result, field) => {
if (err) res.render('404', { err });
else {
console.log(result);
res.render('DataEmployee', { successMes: 'Done', errmessage: '' });
}
});
});
var us;
app.post('/Recharge_Process', urlencodedParser, function (req, res) {
var Card_Serial_Num = req.body.Card_Serial_Num;
var user_number = us;
let myquery = `Update wgsa_company.customer
set customer.balance=customer.balance+ (select Card_value
from wgsa_company.card
where card.Serial_number=${Card_Serial_Num})
where customer.Phone_num=${user_number}`;
db.query(myquery, (err, result, field) => {
if (err) res.render('404', { err });
else {
res.render('Recharge', { status: 1 });
}
});
});
app.post('/Add_New_Offer', urlencodedParser, function (req, res) {
var Offer_num = req.body.Offer_num;
var Offer_describtion = req.body.Offer_describtion;
var Minutes = req.body.Minutes;
var Megas = req.body.Megas;
var Price = req.body.Price;
var Launch_date = req.body.Launch_date;
var Expire_date = req.body.Expire_date;
if (!Offer_describtion)
Offer_describtion = "NULL";
if (!Minutes)
Minutes = "NULL";
if (!Megas)
Megas = "NULL";
if (!Price)
Price = "NULL";
if (!Launch_date)
Launch_date = "NULL";
if (!Expire_date)
Expire_date = "NULL";
let myquery = `
Insert into wgsa_company.Offer(Launch_date, Price, Minutes, Expire_date, Megas, Offer_describ, Offer_num) values('${Launch_date}', ${Price}, ${Minutes}, '${Expire_date}', ${Megas}, '${Offer_describtion}', ${Offer_num})
`;
db.query(myquery, (err, result, field) => {
if (err) throw err;
console.log(result);
res.render('DataEmployee', { successMes: 'Done', errmessage: '' });
});
});
app.post('/Complain_process', urlencodedParser, (req, res) => {
var Complaint = req.body.User_Complaint;
var user_pn = us;
current_date = '2021-08-08';
let Querytogetmaxcode = ` SELECT MAX(wgsa_company.complaint.C_Code) as m from wgsa_company.complaint`;
db.query(Querytogetmaxcode, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
var maxcode = result[0].m;
maxcode = maxcode + 1;
console.log(Complaint);
let myquery1 = `Insert into wgsa_company.complaint (C_Code, C_Descrip, Complaint_by, Complaint_date) values (${maxcode},'${Complaint}','${user_pn}','${current_date}')`;
db.query(myquery1, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
console.log(result);
});
});
});
app.get('/Complain', (req, res) => {
let query = `Select *
from wgsa_company.complaint where Complaint_by=${us}`;
db.query(query, (err, result, field) => {
console.log(result);
if (err) {
res.render('404', { err })
} else {
res.render('Complain', { result });
console.log(result);
}
});
});
app.post('/Delete_Complaint', urlencodedParser, (req, res) => {
var C_Code = req.body.C_Code_to_be_deleted;
let query = `DELETE FROM wgsa_company.complaint WHERE complaint.C_Code=${C_Code}`;
db.query(query, (err, result, field) => {
if (err) {
res.render('404', { err })
} else {
app.get('/Complain', (req, res) => {
let query1 = `Select *
from wgsa_company.complaint`;
db.query(query1, (err, result, field) => {
if (err) {
res.render('404', { err })
} else {
res.render('Complain', { result });
console.log(result);
}
});
});
}
});
});
app.get('/Transfer', (req, res) => {
res.render('Transfer', { errmessage: '', successMes: '' })
});
app.post('/Transfer_balance', urlencodedParser, (req, res) => {
var user_phone_number = us; //Gehan sadat
console.log(us);
var Recipient_Phone_number = parseInt(req.body.Recipient_phone_num, 10);
var Balance_to_be_transfered = parseInt(req.body.Balance_to_be_transfered, 10);
function CheckNumbersEntered() {
console.log("yes");
if (Balance_to_be_transfered <= 0 || Recipient_Phone_number <= 0) {
return res.render('Transfer', { errmessage: 'Please enter valid numbers', successMes: '' });
}
else {
CheckRecipient();
}
}
function CheckRecipient() {
let queryrecipient = `select *
from wgsa_company.customer
where customer.phone_num=${Recipient_Phone_number}; `;
db.query(queryrecipient, (err, result, field) => { //3
if (err) {
res.render('404', { err });
throw err;
}
if (result.length === 0) {
console.log("The recipient number is not fount");
return res.render('Transfer', { errmessage: 'There is no recipent with such a number', successMes: '' });
}
else {
CheckSufficientbalance();
}
});
}
function CheckSufficientbalance() {
let query_customer_curr_balance = `select customer.Balance
from wgsa_company.customer
where customer.phone_num='${user_phone_number}';`;
db.query(query_customer_curr_balance, (err, result1, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result1[0].Balance < Balance_to_be_transfered) {
console.log("your balance is not sufficient to complete the transfer process");
return res.render('Transfer', { errmessage: 'your balance is not sufficient to complete the transfer process', successMes: '' });
}
else {
ExecuteTransfer();
}
});
}
function ExecuteTransfer() {
let query1 = `Update wgsa_company.customer
set customer.Balance=customer.Balance-${Balance_to_be_transfered}
where customer.Phone_num=${user_phone_number}`;
db.query(query1, (err, result, field) => {//5
if (err) {
res.render('404', { err });
console.log(result);
} else {
let query2 = `update wgsa_company.customer
set customer.Balance=customer.Balance+${Balance_to_be_transfered}
where customer.phone_num=${Recipient_Phone_number};`
db.query(query2, (err, result, field) => {//6
if (err) {
res.render('404', { err });
console.log(result);
} else {
return res.render('Transfer', { errmessage: '', successMes: 'Balance is Transfered successfully' });
}
});
}
});
}
CheckNumbersEntered();
});
//login and register
app.post('/registernew', urlencodedParser, (req, res) => {
const { Fname, Lname, Phone_number, password, ID } = req.body;
var gender = req.body.gender;
if (!Fname || !Lname || !Phone_number || !password || !ID || !gender) {
return res.render('Register', { errmessage: 'Please enter all fields', successMes: '' });
}
if (!parseInt(Phone_number) || !parseInt(ID)) {
return res.render('Register', { errmessage: 'Please enter a Valid number', successMes: '' });
}
db.query('select phone_num from customer where phone_num=? or ID=?', [Phone_number, ID], (err, result) => { // GH: why or ID
if (err) throw err;
if (result.length > 0) {
res.render('Register', { errmessage: 'This Phone is Already registerd', successMes: '' });
}
else { //valid
if (gender == 'Male')
gender = 'M';
else
gender = 'F';
if (password.length < 7)
return res.render('Register', { errmessage: 'Password should be at least 7 characters', successMes: '' });
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 2).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
db.query('insert into customer(Fname,Lname,Phone_num,ID,gender,password,Renewal_date) values(?,?,?,?,?,?,?)', [Fname, Lname, Phone_number, ID, gender, password,today], err => {
if (err) throw err;
res.render('Register', { errmessage: '', successMes: 'Registerd You Can now Log in ' });
});
}
}
);
});
app.post('/loginSubmit', urlencodedParser, (req, res) => {
const { Phone_number, password } = req.body;
if (!Phone_number || !password) {
return res.render('Login', { errmessage: 'Please enter all fields', successMes: '' });
}
let myquery1 = `Select *
from wgsa_company.plan,wgsa_company.customer
where customer.plan_code=plan.Plan_code and customer.Phone_num=${Phone_number}`;
db.query(myquery1, (err, result) => {
if (err) throw err;
if (result.length == 0) {
return res.render('Login', { errmessage: 'This Phone is not Registered yet', successMes: '' });
}
if (result[0].Password != password)
return res.render('Login', { errmessage: 'Wrong Password', successMes: '' });
result[0].Renewal_date = result[0].Renewal_date.toString().substr(0, 15);
us=Phone_number;
return res.render('Customer', { result });
}
);
});
app.get('/CustomerAccount', (req, res) => {
res.render('CustomerAccount')
});
/*----------------------------------------------------------------HR-----------------------------------*/
app.post('/Add_employee', urlencodedParser, function (req, res) {
var Fname = req.body.Fname;
var Lname = req.body.Lname;
var Dnum = req.body.Dnum;
var Address = req.body.Address;
var ssn = req.body.SSN;
var Position = req.body.Position;
var Salary = req.body.Salary;
var bnum = parseInt(req.body.bnum, 10);
var hours = req.body.hours;
var gender = req.body.Gender;
var phone_num = req.body.Phone_number;
var s_ssn = req.body.Super_ssn;
var Password = req.body.password;
// NULL VALUES SYNTAX
if (!s_ssn)
s_ssn = "NULL";
if (!Address)
Address = "NULL";
if (!hours)
hours = "NULL";
if (!gender)
gender = "NULL";
if (!phone_num)
phone_num = "NULL";
function SSNifexistasln() {
let query123 = `SELECT
employee.Ssn
FROM wgsa_company.employee
WHERE employee.Ssn=${ssn};`;
db.query(query123, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length > 0) {
return res.render('HR', { errmessage: 'There is an Employee with that SSN', successMes: '' });
}
else {
CHECK_PHONE_NUM_FOR_EMPLOYEE();
}
});
}
function CHECK_PHONE_NUM_FOR_EMPLOYEE() {
let query12345 = `select employee.phone_num
from employee
where phone_num=${phone_num}`;
db.query(query12345, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length > 0) {
return res.render('HR', { errmessage: 'There is another one with that phone number', successMes: '' });
}
else {
CHECK_PHONE_NUM_FOR_EMPLOYEE_customer();
}
});
}
function CHECK_PHONE_NUM_FOR_EMPLOYEE_customer() {
let query123456 = `select *
from wgsa_company.customer
where customer.phone_num=${phone_num}; `;
db.query(query123456, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length > 0) {
return res.render('HR', { errmessage: 'There is another one with that phone number', successMes: '' });
}
else {
CHECK_BNUM();
}
});
}
function CHECK_BNUM() {
let query1234567 = `select * from wgsa_company.branch where branch.bnum=${bnum}`;
db.query(query1234567, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length === 0) {
return res.render('HR', { errmessage: 'There is no branch with that number', successMes: '' });
}
else {
CHECK_superssn();
}
});
}
function CHECK_superssn() {
if (s_ssn != "NULL") {
let query12345678 = `SELECT *
FROM wgsa_company.employee
WHERE employee.ssn=${s_ssn};`;
db.query(query12345678, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length === 0) {
return res.render('HR', { errmessage: 'There is Super ssn with that number', successMes: '' });
}
else {
Check_dnum();
}
});
}
else {
Check_dnum();
}
}
function Check_dnum() {
let query123456789 = `select department.Dnum
from wgsa_company.department
where dnum=${Dnum}`;
db.query(query123456789, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length === 0) {
return res.render('HR', { errmessage: 'There is no department with that number', successMes: '' });
}
else {
ExecuteADDnewemployee();
}
});
}
function ExecuteADDnewemployee() {
let myquery = `INSERT INTO wgsa_company.employee(Fname, Lname, Dnum, Address, Ssn, Postion, Salary, Branch_num, Hours, Gender, Phone_num, Super_ssn, Password) VALUES("${Fname}", "${Lname}", ${Dnum}, "${Address}", ${ssn}, "${Position}", ${Salary}, ${bnum}, ${hours}, "${gender}", ${phone_num}, ${s_ssn},"${Password}")`;
db.query(myquery, (err, result, field) => {
if (err) {
res.render('404', { err });
} else {
res.render('HR', { successMes: `${Fname} is added as a new employee`, errmessage: '' });
}
});
}
ExecuteADDnewemployee();
});
app.post('/Add_branch', urlencodedParser, function (req, res) {
var phone_num = req.body.phone_num;
var bnum = req.body.bnum;
var location = req.body.location;
if (!phone_num)
phone_num = "NULL";
if (!location)
location = "NULL";
function br_num_exist_in_branch() {
let qq = `select branch.phone_num
from wgsa_company.Branch
where branch.phone_num=${phone_num};`;
db.query(qq, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length > 0) {
return res.render('HR', { errmessage: 'Branch Phone Num Already exist', successMes: '' });
}
else {
br_num_exist_in_customer();
}
});
}
function br_num_exist_in_customer() {
let qq = `select customer.phone_num
from wgsa_company.customer
where customer.phone_num=${phone_num};`;
db.query(qq, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length > 0) {
return res.render('HR', { errmessage: 'Phone Num Already exists in customer', successMes: '' });
}
else {
br_num_exist_in_employee();
}
});
}
function br_num_exist_in_employee() {
let qq = `select employee.phone_num
from wgsa_company.employee
where employee.phone_num=${phone_num};`;
db.query(qq, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length > 0) {
return res.render('HR', { errmessage: 'Phone Num Already exists in employee', successMes: '' });
}
else {
br_bnum_exist_in_branch();
}
});
}
function br_bnum_exist_in_branch() {
let qq = `select branch.bnum
from wgsa_company.branch
where branch.bnum=${bnum};`;
db.query(qq, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length > 0) {
return res.render('HR', { errmessage: 'branch Num Already exists', successMes: '' });
}
else {
exec();
}
});
}
function exec() {
let myquery = `INSERT INTO wgsa_company.branch(Phone_num, Bnum, Location) VALUES(${phone_num}, ${bnum}, "${location}")`;
db.query(myquery, (err, result, field) => {
if (err) {
console.log(err)
res.render('404', { err });
}
console.log(result);
res.render('HR', { successMes: 'Done', errmessage: '' });
});
}
br_num_exist_in_branch();
});
app.post('/Remove_branch', urlencodedParser, function (req, res) {
var bnum = req.body.bnum;
function br_bnum_exist_in_branch() {
let qq = `select branch.bnum
from wgsa_company.branch
where branch.bnum=${bnum};`;
db.query(qq, (err, result, field) => {
if (err) {
res.render('404', { err });
throw err;
}
if (result.length == 0) {
console.log("branch Num doesnt exists");