-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
709 lines (637 loc) · 25.2 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
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
import express from 'express';
import cors from 'cors';
import jwt from "jsonwebtoken";
import cookieParser from "cookie-parser";
import dotenv from 'dotenv';
dotenv.config();
// stripe
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// mongodb
import { MongoClient, ObjectId, ServerApiVersion } from 'mongodb';
const app = express();
const port = process.env.PORT || 5000;
//middleware
app.use(cors({
origin: [
// "http://localhost:5173", // TODO: add production url
"https://nikah-noor-sazidulalam47.vercel.app"
],
credentials: true
}));
app.use(express.json());
app.use(cookieParser());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.xyqwep0.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`;
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
// await client.connect(); // TODO: disable on production
const database = client.db("nikahNoorDB");
const biodataCollection = database.collection("biodatas");
const favoriteCollection = database.collection("favorites");
const userCollection = database.collection("users");
const reviewCollection = database.collection("reviews");
const paymentCollection = database.collection("payments");
// my middlewares
const verifyToken = async (req, res, next) => {
const token = req.cookies?.token;
console.log('Verifying token', token);
if (!token) {
return res.status(401).send({ message: 'Not authorized' });
}
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
if (err) {
console.log(err);
return res.status(401).send({ message: 'Not authorized' });
}
req.user = decoded;
console.log(decoded);
next();
})
};
// admin check middleware
const verifyAdmin = async (req, res, next) => {
const email = req.user?.email;
const query = { email: email };
const user = await userCollection.findOne(query);
const isAdmin = user?.role === "admin";
console.log({ isAdmin: isAdmin });
console.log("check email", email);
if (!isAdmin) {
return res.status(403).send({ message: 'Forbidden' });
}
next();
};
//jwt auth
app.post("/jwt", async (req, res) => {
const user = req.body;
console.log(user);
const token = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '10h' });
res
.cookie("token", token, {
httpOnly: true,
secure: true, // TODO: "true" on production
sameSite: "none", // TODO: uncomment on production
})
.send({ success: true });
});
app.get("/logout", async (req, res) => {
res.clearCookie("token")
.send({ success: true });
});
// biodata collection
app.get("/biodatas", async (req, res) => {
const biodataType = req.query?.biodataType;
const permanentDivision = req.query?.permanentDivision;
const ageFrom = parseInt(req.query?.from);
const ageTo = parseInt(req.query?.to);
const page = parseInt(req.query?.page);
const size = parseInt(req.query?.size);
const query = {};
if (biodataType) query.biodataType = biodataType;
if (permanentDivision) query.permanentDivision = permanentDivision;
if (ageFrom && ageTo) query.age = { $gte: ageFrom, $lte: ageTo };
const options = {
projection: {
biodataId: 1,
_id: 0,
name: 1,
biodataType: 1,
profileImage: 1,
age: 1,
occupation: 1,
permanentDivision: 1
},
sort: {
age: 1
}
};
const result = await biodataCollection.find(query, options).skip(page * size).limit(size).toArray();
res.send(result);
});
app.get("/biodatasCount", async (req, res) => {
const biodataType = req.query?.biodataType;
const permanentDivision = req.query?.permanentDivision;
const ageFrom = parseInt(req.query?.from);
const ageTo = parseInt(req.query?.to);
const query = {};
if (biodataType) query.biodataType = biodataType;
if (permanentDivision) query.permanentDivision = permanentDivision;
if (ageFrom && ageTo) query.age = { $gte: ageFrom, $lte: ageTo };
const count = await biodataCollection.countDocuments(query);
res.send({ count });
});
app.get("/biodatasSidebar", async (req, res) => {
const type = req.query.type;
const count = parseInt(req.query.count);
const biodataIdToSkip = parseInt(req.query.skip);
const result = await biodataCollection.aggregate([
{
$match: {
biodataType: type,
biodataId: { $ne: biodataIdToSkip }
}
},
{
$sample: {
size: count
}
},
{
$project: {
biodataId: 1,
_id: 0,
name: 1,
biodataType: 1,
profileImage: 1,
age: 1,
occupation: 1,
permanentDivision: 1
}
}
]).toArray();
res.send(result);
});
app.get("/biodatas/:biodataId", verifyToken, async (req, res) => {
const biodataId = parseInt(req.params.biodataId);
const email = req.user?.email;
const query = { biodataId: biodataId };
const result = await biodataCollection.findOne(query);
// check user admin or premium or self
// if not don't send email and phone number
const user = await userCollection.findOne({ email: email });
if (!(user?.role === 'admin' || user?.premium === 'Approved' || result.contactEmail === email)) {
result.contactEmail = "";
result.mobileNumber = "";
}
res.send(result);
});
// for getting self biodata
app.get("/biodatas/email/:email", verifyToken, async (req, res) => {
const email = req.params.email;
if (req.user.email !== email) {
return res.status(403).send({ message: 'Forbidden access' });
}
const query = { contactEmail: email };
const result = await biodataCollection.findOne(query);
res.send(result);
});
app.put("/biodatas/:email", verifyToken, async (req, res) => {
const email = req.params.email;
const biodata = req.body;
console.log({ email, biodata });
// check if the user is already have biodata
const UserQuery = { contactEmail: email };
const userOptions = {
projection: { biodataId: 1, _id: 0 },
};
const userBioIdObj = await biodataCollection.findOne(UserQuery, userOptions);
let userBiodataId = userBioIdObj?.biodataId;
// if user don't have biodata then give him new biodataId
if (!userBiodataId) {
const lastQuery = {};
const lastOptions = {
projection: { biodataId: 1, _id: 0 },
};
const lastIdObj = await biodataCollection.find(lastQuery, lastOptions).sort({ biodataId: -1 }).limit(1).toArray();
userBiodataId = lastIdObj[0].biodataId + 1;
}
// update biodata
const filter = { contactEmail: email };
const options = { upsert: true };
const UpdatedBiodata = {
$set: {
biodataId: userBiodataId,
biodataType: biodata.biodataType,
name: biodata.name,
profileImage: biodata.profileImage,
dateOfBirth: biodata.dateOfBirth,
height: biodata.height,
weight: biodata.weight,
age: biodata.age,
occupation: biodata.occupation,
race: biodata.race,
fathersName: biodata.fathersName,
mothersName: biodata.mothersName,
permanentDivision: biodata.permanentDivision,
presentDivision: biodata.presentDivision,
expectedPartnerAge: biodata.expectedPartnerAge,
expectedPartnerHeight: biodata.expectedPartnerHeight,
expectedPartnerWeight: biodata.expectedPartnerWeight,
contactEmail: biodata.contactEmail,
mobileNumber: biodata.mobileNumber,
}
};
const result = await biodataCollection.updateOne(filter, UpdatedBiodata, options);
res.send(result);
});
// favorite collection
app.post("/favorites", verifyToken, async (req, res) => {
const favorite = req.body;
const newFavoriteId = favorite.favoriteId;
//check already exists in the favorites
const query = { email: favorite.email };
const options = {
projection: { favoriteId: 1, _id: 0 },
};
const userFavoritesObj = await favoriteCollection.find(query, options).toArray();
const userOldFavoritesArray = userFavoritesObj.map(obj => obj.favoriteId);
if (userOldFavoritesArray.indexOf(newFavoriteId) !== -1) {
return res.send({ exists: true });
}
// add to favorites
const result = await favoriteCollection.insertOne(favorite);
res.send(result);
});
app.get("/favorites/email/:email", verifyToken, async (req, res) => {
const email = req.params.email;
if (req.user.email !== email) {
return res.status(403).send({ message: 'Forbidden access' });
}
const result = await favoriteCollection.aggregate([
{
$match: { email: email }
},
{
$project: { favoriteId: 1, _id: 0 }
},
{
$lookup: {
from: "biodatas",
localField: "favoriteId",
foreignField: "biodataId",
as: "biodata"
}
},
{
$unwind: {
path: "$biodata",
preserveNullAndEmptyArrays: true
}
},
{
$project: {
biodataId: "$biodata.biodataId",
name: "$biodata.name",
permanentDivision: "$biodata.permanentDivision",
occupation: "$biodata.occupation",
}
}
]).toArray();
res.send(result);
});
app.delete("/favorites/:id", verifyToken, async (req, res) => {
const id = parseInt(req.params.id);
const query = { favoriteId: id };
const result = await favoriteCollection.deleteOne(query);
res.send(result);
});
// user collection
app.get("/users", verifyToken, verifyAdmin, async (req, res) => {
const result = await userCollection.find().toArray();
res.send(result);
});
app.put("/users", verifyToken, async (req, res) => {
const user = req.body;
console.log(user);
const filter = { email: user.email };
const options = { upsert: true };
const UpdatedUser = {
$set: {
name: user.name,
email: user.email,
}
};
const result = await userCollection.updateOne(filter, UpdatedUser, options);
res.send(result);
});
// check user admin
app.get("/users/admin", verifyToken, async (req, res) => {
const email = req.user.email;
const query = { email: email };
const user = await userCollection.findOne(query);
const admin = user?.role === "admin";
res.send({ admin });
});
// check user premium
app.get("/users/premium/:email", verifyToken, async (req, res) => {
const email = req.params.email;
if (req.user?.email !== email) {
return res.status(403).send({ message: 'Forbidden' });
}
const query = { email: email };
const user = await userCollection.findOne(query);
const premium = user?.premium === "Approved";
res.send({ premium });
});
// make user premium request
app.get("/users/premiumReq/:email", verifyToken, async (req, res) => {
const email = req.params.email;
const biodata = req.body;
console.log(email, biodata);
const filter = { email: email };
const UpdatedBiodata = {
$set: {
premium: "Pending",
}
};
const result = await userCollection.updateOne(filter, UpdatedBiodata);
res.send(result);
});
// make user premium
app.get("/users/makePremium/:email", verifyToken, verifyAdmin, async (req, res) => {
const email = req.params.email;
const biodata = req.body;
console.log(email, biodata);
const filter = { email: email };
const UpdatedBiodata = {
$set: {
premium: "Approved",
}
};
const result = await userCollection.updateOne(filter, UpdatedBiodata);
res.send(result);
});
// make user admin
app.patch("/users/admin", verifyToken, verifyAdmin, async (req, res) => {
const user = req.body;
console.log(user);
const filter = { email: user.email };
const UpdatedUser = {
$set: {
role: "admin",
}
};
const result = await userCollection.updateOne(filter, UpdatedUser);
res.send(result);
});
// update user name
app.patch("/users", verifyToken, async (req, res) => {
const user = req.body;
console.log(user);
const filter = { email: user.email };
const UpdatedUser = {
$set: {
name: user.name,
}
};
const result = await userCollection.updateOne(filter, UpdatedUser);
res.send(result);
});
// get 6 random premium users
app.get("/premiums", async (req, res) => {
const count = parseInt(req.query.count);
const result = await userCollection.aggregate([
{
$match: {
premium: "Approved"
}
},
{
$lookup: {
from: "biodatas",
localField: "email",
foreignField: "contactEmail",
as: "biodata"
}
},
{
$unwind: "$biodata"
},
{
$project: {
_id: 0,
profileImage: "$biodata.profileImage",
biodataId: "$biodata.biodataId",
biodataType: "$biodata.biodataType",
age: "$biodata.age",
occupation: "$biodata.occupation",
permanentDivision: "$biodata.permanentDivision",
}
},
{
$sample: {
size: count
}
},
]).toArray();
res.send(result);
});
app.post("/payments", verifyToken, async (req, res) => {
const payment = req.body;
console.log(payment);
const result = await paymentCollection.insertOne(payment);
res.send(result);
});
app.get("/payments/user", verifyToken, async (req, res) => {
const email = req.user?.email;
const response = await paymentCollection.aggregate([
{
$match: {
email: email
}
},
{
$lookup: {
from: "biodatas",
localField: "contactRequestId",
foreignField: "biodataId",
as: "biodata"
}
},
{
$unwind: "$biodata"
},
{
$lookup: {
from: "users",
localField: "email",
foreignField: "email",
as: "user"
}
},
{
$unwind: "$user"
},
{
$project: {
status: "$status",
requestedId: "$contactRequestId",
requestedName: "$biodata.name",
requestedEmail: "$biodata.contactEmail",
requestedMobileNumber: "$biodata.mobileNumber",
}
}
]).toArray();
const result = response.map(req => {
if (req?.status !== 'Approved') {
req.requestedEmail = "";
req.requestedMobileNumber = "";
}
return req;
})
res.send(result);
});
app.get("/payments/admin", verifyToken, verifyAdmin, async (req, res) => {
const result = await paymentCollection.aggregate([
{
$match: {
status: "Pending"
}
},
{
$lookup: {
from: "users",
localField: "email",
foreignField: "email",
as: "user"
}
},
{
$unwind: "$user"
},
{
$project: {
userName: "$user.name",
userEmail: "$email",
requestedId: "$contactRequestId",
}
}
]).toArray();
res.send(result);
});
app.get("/payments/exist/:biodataId", verifyToken, async (req, res) => {
const biodataId = parseInt(req.params.biodataId);
const email = req.user?.email;
const query = { email: email };
const response = await paymentCollection.find(query).toArray();
const reqIds = response?.map(req => req.contactRequestId);
const exist = reqIds.includes(biodataId);
res.send({ exist });
});
// approve request
app.get("/payments/approve/:id", verifyToken, verifyAdmin, async (req, res) => {
const id = req.params.id;
const payment = req.body;
console.log(id, payment);
const filter = { _id: new ObjectId(id) };
const UpdatedPayment = {
$set: {
status: "Approved",
}
};
const result = await paymentCollection.updateOne(filter, UpdatedPayment);
res.send(result);
});
app.delete("/payments/:id", verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await paymentCollection.deleteOne(query);
res.send(result);
});
// review collection
app.get("/reviews", async (req, res) => {
const query = {};
const options = {
sort: {
marriageDate: 1
}
};
const result = await reviewCollection.find(query, options).toArray();
res.send(result);
});
app.post("/reviews", verifyToken, async (req, res) => {
const review = req.body;
console.log(review);
const result = await reviewCollection.insertOne(review);
res.send(result);
});
app.get("/public-stats", async (req, res) => {
const totalBiodata = await biodataCollection.estimatedDocumentCount();
const maleBiodata = await biodataCollection.countDocuments({ biodataType: "Male" });
const femaleBiodata = await biodataCollection.countDocuments({ biodataType: "Female" });
const totalReview = await reviewCollection.estimatedDocumentCount();
res.send({ totalBiodata, maleBiodata, femaleBiodata, totalReview });
});
app.get("/admin-stats", verifyToken, verifyAdmin, async (req, res) => {
const totalBiodata = await biodataCollection.estimatedDocumentCount();
const maleBiodata = await biodataCollection.countDocuments({ biodataType: "Male" });
const femaleBiodata = await biodataCollection.countDocuments({ biodataType: "Female" });
const premiumBiodata = await userCollection.countDocuments({ premium: "Approved" });
const totalReview = await reviewCollection.estimatedDocumentCount();
const totalRevenueArry = await paymentCollection.find({}, {
projection: {
price: 1,
_id: 0
}
}).toArray();
const totalRevenue = totalRevenueArry.reduce((acc, curr) => acc + curr.price, 0);
res.send({ totalBiodata, maleBiodata, femaleBiodata, premiumBiodata, totalReview, totalRevenue });
});
// payment intent
app.post("/create-payment-intent", verifyToken, async (req, res) => {
const { price } = req.body;
const amount = parseInt(price * 100);
console.log({ amount });
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: "usd",
payment_method_types: ['card']
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
// check premium pending requests
app.get("/usersPremium", verifyToken, verifyAdmin, async (req, res) => {
const result = await userCollection.aggregate([
{
$match: {
premium: "Pending"
}
},
{
$lookup: {
from: "biodatas",
localField: "email",
foreignField: "contactEmail",
as: "biodata"
}
},
{
$unwind: "$biodata"
},
{
$project: {
_id: 0,
biodataId: "$biodata.biodataId",
name: "$name",
email: "$email",
}
}
]).toArray();
res.send(result);
});
// Send a ping to confirm a successful connection
// await client.db("admin").command({ ping: 1 }); // TODO: disable on production
// console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
// await client.close();
}
}
run().catch(console.dir);
app.get('/', (req, res) => {
res.send('Nikah Noor server is running');
});
app.listen(port, () => {
console.log(`Nikah Noor server is running on port ${port}`);
});