-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
651 lines (538 loc) · 18.5 KB
/
server.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
const express = require('express');
const admin = require('firebase-admin');
const bcrypt = require('bcrypt');
const path = require('path');
const nodemailer = require('nodemailer');
const Razorpay = require('razorpay')
const razorpay = new Razorpay({
key_id: 'YOUR RAZORPAY ID',
key_secret: 'YOUR RAZORPAY SECRET KEY'
})
let serviceAccount = require("./firebase_secret.json")
const { randomInt } = require('crypto');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "gs://pilltracker-ce296.appspot.com"
});
let staticPath = path.join(__dirname, "");
let db = admin.firestore();
const app = express();
app.use(express.static(staticPath));
app.use(express.json());
app.listen(3000, () => {
console.log('Listening on port 3000.......');
})
app.get("/", (req, res) => {
res.sendFile(path.join(staticPath, "html/index.html"));
})
app.get('/signup', (req, res) => {
res.sendFile(path.join(staticPath, "html/signup.html"));
})
app.post('/signup', (req, res) => {
let { name, email, password, number, tac } = req.body;
if(name.length < 3){
return res.json({'alert': 'Name Must be 3 Letters Long'});
} else if(!email.length){
return res.json({'alert': 'Enter Your E - Mail'});
} else if(password.length < 8){
return res.json({'alert': 'Password Should be 8 Letters Long'});
} else if(!number.length){
return res.json({'alert': 'Enter Your Phone Number'});
} else if(!Number(number) || number.length < 10){
return res.json({'alert': 'Invalid Number'});
} else if(!tac){
return res.json({'alert': 'You Must Agree All Terms and Conditions'});
}
db.collection('users').doc(email).get().then(user => {
if(user.exists){
return res.json({'alert': 'E- Mail Already Exists'});
} else{
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(password, salt, (err, hash) => {
req.body.password = hash;
db.collection('users').doc(email).set(req.body)
.then(data => {
res.json({
name: req.body.name,
email: req.body.email,
seller: req.body.seller,
})
})
})
})
}
})
})
app.get('/login', (req, res) => {
res.sendFile(path.join(staticPath, "html/login.html"));
})
app.get('/about', (req, res) => {
res.sendFile(path.join(staticPath, "html/about.html"));
})
app.get('/contact', (req, res) => {
res.sendFile(path.join(staticPath, "html/contact.html"));
})
app.get('/prescription', (req, res) => {
res.sendFile(path.join(staticPath, "html/prescription.html"));
})
app.post('/upload-presc', (req, res) => {
let { name, addr, pincode, number} = req.body;
if(!name.length){
return res.json({'alert': 'Enter Your Name'});
} else if(addr.length > 100 || addr.length < 10){
return res.json({'alert': 'Enter Full Address'});
} else if(pincode.length != 6){
return res.json({'alert': 'Enter 6 Digit PinCode'});
} else if(number.length != 10){
return res.json({'alert': 'Please enter your 10 digit number'});
}
db.collection('prescOrders').doc(Date.now().toString()).set(req.body)
.then(data => {
res.json({'product': name});
})
.catch(err => {
return res.json({'alert': 'Some Error Occurred. Try Again'});
})
})
app.post('/login', (req, res) => {
let { email, password } = req.body;
if(!email.length || !password.length){
return res.json({'alert': 'Fill All the Inputs'});
}
db.collection('users').doc(email).get().then(user => {
if(!user.exists){
return res.json({'alert': 'E - Mail Does Not Exists'});
} else{
bcrypt.compare(password, user.data().password, (err, result) =>{
if(result){
let data=user.data();
return res.json({
name: data.name,
email: data.email,
seller: data.seller,
})
} else{
return res.json({'alert' : 'Incorrect Password'})
}
})
}
})
})
app.get('/seller', (req, res) => {
res.sendFile(path.join(staticPath, "html/seller.html"));
})
app.post('/seller', (req, res) => {
let { name, about, address, number, tac, email } = req.body;
if(!name.length || !about.length || !address.length || !number.length == 10||
!Number(number)){
return res.json({'alert':'Some Information(s) is/are Invalid'});
} else if(!tac){
return res.json({'alert':'You Must Agree All Terms and Conditions'});
} else {
db.collection('sellers').doc(email).set(req.body).then(data => {
db.collection('users').doc(email).update({
seller: true
}).then(data => {
res.json(true);
})
})
}
})
app.post('/add-product', (req, res) => {
let { name, shortDes, des, actualPrice, discount, sellPrice, stock, use, cate, tac, id} = req.body;
if(!name.length){
return res.json({'alert': 'Enter Product Name'});
} else if(shortDes.length > 100 || shortDes.length < 10){
return res.json({'alert': 'Short Description Must be Between 10 to 100 Letters Long'});
} else if(!des.length){
return res.json({'alert': 'Enter Detailed Description About the Product'});
} else if(!actualPrice.length || !discount.length || !sellPrice.length){
return res.json({'alert': 'You Must Add Pricings'});
} else if(stock < 20){
return res.json({'alert': 'You Should Have at Least 20 Items in Stock'});
} else if(!use.length){
return res.json({'alert': 'Enter Use of the Products'});
} else if(!cate.length){
return res.json({'alert': 'Enter Category of the Product'});
} else if(!tac){
return res.json({'alert': 'You Must Agree Our Terms and Conditions'});
}
db.collection('products').doc(id).set(req.body)
.then(data => {
res.json({'product': name});
})
.catch(err => {
return res.json({'alert': 'Some Error Occurred. Try Again'});
})
})
app.get('/add-product', (req, res) => {
res.sendFile(path.join(staticPath, "html/addproduct.html"));
})
app.get('/add-product/:id', (req, res) => {
res.sendFile(path.join(staticPath, "html/addproduct.html"));
})
app.post('/get-products', (req, res) => {
const {email, id, cate} = req.body;
if(id){
docref = db.collection('products').doc(id);
} else if(cate){
docref = db.collection('products').where('cate', '==' ,cate);
} else if(email){
docref = db.collection('products').where('email', '==' ,email);
}
docref.get().then(products => {
if(products.empty){
return res.json('no-products');
}
let prArr = [];
if(id){
return res.json(products.data());
} else{
products.forEach(items => {
let data = items.data();
data.id = items.id;
prArr.push(data);
})
res.json(prArr);
}
})
})
app.get('/product/:id', (req, res) => {
res.sendFile(path.join(staticPath, "html/product.html"));
})
app.post('/get-product', (req, res) => {
const {id} = req.body;
let docref = db.collection('products').doc(id);
docref.get().then(product => {
if(product.empty){
return res.json('not-available');
} else{
res.json(product.data());
}
})
})
app.post('/get-seller', (req, res) => {
const { email } = req.body;
let docref = db.collection('sellers').doc(email);
docref.get().then(products => {
return res.json(products.data());
})
})
app.post('/get-all-products', (req, res) => {
let {tac} = req.body;
let docref = db.collection('products').where('tac', '==' ,tac);
docref.get().then(products => {
if(products.empty){
return res.json('no-products');
}
let prArr = [];
products.forEach(items => {
let data = items.data();
data.id = items.id;
prArr.push(data);
})
res.json(prArr);
})
})
app.post('/get-search-result', (req, res) => {
let {key} = req.body;
let prArr = [];
let docref = db.collection('products').where('cate', '==' ,key);
let docref2 = db.collection('products').where('use', '==' ,key);
let docref3 = db.collection('products').where('name', '==' ,key);
docref.get().then(products => {
products.forEach(items => {
let data = items.data();
data.id = items.id;
if(data.length != 0){
prArr.push(data);
}
});
}).then(() => {
docref2.get().then(products => {
products.forEach(items => {
let data = items.data();
data.id = items.id;
if(data.length != 0){
prArr.push(data);
}
});
})
}).then(() => {
docref3.get().then(products => {
products.forEach(items => {
let data = items.data();
data.id = items.id;
if(data.length != 0){
prArr.push(data);
}
});
}).then(() => {
if(prArr.length != 0){
res.json(prArr);
} else{
res.json('no-products');
}
})
})
})
app.post('/delete-product', (req, res) => {
const {id} = req.body;
db.collection('products').doc(id).delete()
.then(data => {
res.json('success');
}).catch(err => {
res.json('err');
})
})
app.post('/order-online', (req, res)=>{
const {amount, currency, receipt, notes} = req.body;
razorpay.orders.create({amount, currency, receipt, notes},
(err, order)=>{
if(!err)
res.json(order)
else
res.send(err);
}
)
});
app.post('/disEna-product', (req, res) => {
const {data} = req.body;
let id='';
let status = '';
id = data.id;
status = data.status;
db.collection("products").doc(id).update({status: status});
res.json('success');
})
app.post('/submit-query', (req, res) => {
const {name, email, phone, message} = req.body;
let docName = email + '-' + randomInt(10000, 99999);
db.collection("grievance").doc(docName).set(req.body);
res.json('success');
})
app.post('/get-date', (req, res) => {
const {id} = req.body;
let docref = db.collection('products').where('id', '==' ,id);
let prArr = [];
docref.get().then(products => {
products.forEach(items => {
let data = items.data();
data.id = items.id;
if(data.length != 0){
prArr.push(data);
}
});
}).then(() => {
res.json(prArr);
})
})
app.post('/cancel-order', (req, res) => {
const {order, email} = req.body;
let docref = db.collection("orders")
.where("email", "==", email)
.where("order", "array-contains", order);
docref.get().then(orders => {
orders.forEach(items => {
let data = items.data();
for(let i=0; i<data.order.length; i++){
if(data.order[i].id == order.id){
data.order[i].status = 'Cancelled';
db.collection("orders").doc(items.id).update({
order: data.order
});
}
}
})
}).then(() => {
res.json('success');
})
})
app.post('/update-status', (req, res) => {
const {order, email} = req.body;
if(order.status == 'Waiting for Dispatch'){
let docref = db.collection("orders")
.where("email", "==", email)
.where("order", "array-contains", order);
docref.get().then(orders => {
orders.forEach(items => {
let data = items.data();
for(let i=0; i<data.order.length; i++){
if(data.order[i].id == order.id){
data.order[i].status = 'Out for Delivery';
db.collection("orders").doc(items.id).update({
order: data.order
});
}
}
})
}).then(() => {
res.json('success');
})
} else if(order.status == 'Out for Delivery'){
let docref = db.collection("orders")
.where("email", "==", email)
.where("order", "array-contains", order);
docref.get().then(orders => {
orders.forEach(items => {
let data = items.data();
for(let i=0; i<data.order.length; i++){
if(data.order[i].id == order.id){
data.order[i].status = 'Delivered';
db.collection("orders").doc(items.id).update({
order: data.order
});
}
}
})
}).then(() => {
res.json('success');
})
}
})
app.post('/fetch-orders', (req, res) => {
const {email} = req.body;
let docref = db.collection('orders').where('email', '==' ,email);
let prArr = [];
docref.get().then(orders => {
orders.forEach(items => {
let data = items.data();
data.id = items.id;
if(data.length != 0){
prArr.push(data);
} else{
res.json('no-orders');
}
});
}).then(() => {
res.json(prArr);
})
})
app.post('/get-orders', (req, res) => {
let prArr = [];
let docref1 = db.collection('orders');
docref1.get().then(orders => {
orders.forEach(items => {
let data = items.data();
data.id = items.id;
if(data.length != 0){
prArr.push(data);
} else{
res.json('no-orders');
}
});
}).then(() => {
res.json(prArr);
})
})
app.get('/product/:id', (req, res) => {
res.sendFile(path.join(staticPath, "html/product.html"));
})
app.get('cart', (req, res) => {
res.sendFile(path.join(staticPath, "html/cart.html"));
})
app.get('/search', (req, res) => {
res.sendFile(path.join(staticPath, "html/search.html"));
})
app.get('/search/:key', (req, res) => {
res.sendFile(path.join(staticPath, "html/search.html"));
})
app.get('/checkout', (req, res) => {
res.sendFile(path.join(staticPath, "html/checkout.html"));
})
app.post('/order', (req, res) => {
const { email, order, address, mode, status} = req.body;
let docName = email + '-' + randomInt(10000, 99999);
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
auth: {
user: 'YOUR EMAIL ID',
pass: 'YOUR EMAIL PASSWORD'
}
})
const mailOptions = {
from: 'YOUR EMAIL ID',
to: email,
subject: 'Order Confirmation',
html: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Medicomm</title>
<style>
body{
background-color: #f2f2f2;
min-height: 90vh;
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
}
.heading{
text-align: center;
font-size: 40px;
margin: 30px auto 60px;
width: fit-content;
display: block;
line-height: 50px;
}
.span{
text-align: center;
font-size: 20px;
margin: 30px auto 60px;
width: fit-content;
display: block;
line-height: 30px;
}
.btn{
display: block;
margin: 0 auto;
padding: 10px 20px;
background-color: #f2f2f2;
border: 1px solid #000;
border-radius: 5px;
font-size: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="mail">
<h1 class="heading">Thank You For Shopping With Us</h1>
<p class="span">Your Order Has Been Placed Successfully. We Will Contact You Soon.</p>
<p class="span">Regards, Medicomm</p>
<button class="btn">Check Status</button>
</div>
<script>
</script>
</body>
</html>
`
}
db.collection('orders').doc(docName).set(req.body)
.then(data => {
transporter.sendMail(mailOptions, (err, info) => {
if(err){
console.log(err);
res.json({'alert': 'Opps! It seems like there is some problem with our server. Please try again later.'});
} else{
res.json({'alert': 'Your Order Was Placed Successfully.'});
}
})
}).catch(err => {
res.json('err');
})
})
app.post('/update-stock', (req, res) => {
const {id , stock} = req.body;
db.collection("products").doc(id).update({stock: stock});
})
app.get('/404', (req, res) => {
res.sendFile(path.join(staticPath, "html/404.html"));
})
app.use((req, res) => {
res.redirect('/404');
})