-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
501 lines (395 loc) · 13.6 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
const express = require('express')
const app = express()
require('dotenv').config()
const cors = require('cors')
const cookieParser = require('cookie-parser')
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb')
const jwt = require('jsonwebtoken')
const morgan = require('morgan')
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)
const port = process.env.PORT || 5000
// middleware
// const corsOptions = {
// origin: [
// 'http://localhost:5173',
// 'http://localhost:5174',
// 'https://soul-mate-connect.web.app/',
// 'https://soul-mate-connect.firebaseapp.com/'
// ],
// // credentials: true,
// optionSuccessStatus: 200,
// }
// app.use(cors(corsOptions))
app.use(cors())
app.use(express.json())
app.use(cookieParser())
app.use(morgan('dev'))
const uri = `mongodb+srv://${process.env.DB_NAME}:${process.env.DB_PASS}@cluster0.qiowubl.mongodb.net/?retryWrites=true&w=majority`;
// 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,
}
});
const userCollection = client.db('SoulMateConnectDB').collection('users')
const biodatasCollection = client.db('SoulMateConnectDB').collection('biodatas')
const favouritesCollection = client.db('SoulMateConnectDB').collection('favourites')
const paymentCollection = client.db('SoulMateConnectDB').collection('payments')
const successStoryCollection = client.db('SoulMateConnectDB').collection('successStory')
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
// await client.connect();
// jwt related api
app.post('/jwt', async (req, res) => {
const user = req.body;
const token = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '365d' })
res.send({ token })
})
// middlewares
const verifyToken = (req, res, next) => {
// console.log('inside verify token', req.headers.authorization);
if (!req.headers.authorization) {
return res.status(401).send({ message: 'unauthorized access' })
}
const token = req.headers.authorization.split(' ')[1]
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
if (err) {
return res.status(401).send({ message: 'unauthorized access' })
}
req.decoded = decoded;
next()
})
}
// verify admin (have to verify after verify token)
const verifyAdmin = async (req, res, next) => {
const email = req.decoded.email;
const query = { email: email }
const user = await userCollection.findOne(query)
const isAdmin = user?.role === 'admin'
if (!isAdmin) {
return res.send(403).send({ message: 'forbidden access' })
}
next()
}
app.post('/users', async (req, res) => {
const user = req.body;
// insert email if user doesn't exists
const query = { email: user.email }
const existingUser = await userCollection.findOne(query)
if (existingUser) {
return res.send({ message: 'user already exists', insertedId: null })
}
const result = await userCollection.insertOne(user)
res.send(result)
})
app.get('/users', verifyToken, verifyAdmin, async (req, res) => {
let query = {}
if (req.query.search) {
const searchResult = new RegExp(req.query.search, 'i')
query = { name: searchResult }
}
const result = await userCollection.find(query).toArray()
res.send(result)
})
app.get('/users/:email', async (req, res) => {
const email = req.params.email;
const query = { email: email }
const user = await userCollection.findOne(query)
res.send(user)
})
app.patch('/users/:id', async (req, res) => {
const id = req.params.id;
const userRole = req.body.userRole;
const query = { _id: new ObjectId(id) }
const updateUserRole = {
$set: {
role: userRole,
}
}
const updatedUserRole = await userCollection.updateOne(query, updateUserRole)
res.send({ updatedUserRole, })
})
// biodatas api
app.get('/biodatas', async (req, res) => {
console.log(req.query);
const { minAge, maxAge, biodataType, division } = req.query
const page = parseInt(req.query.page)
const size = parseInt(req.query.size)
const filter = {}
if (minAge && maxAge) {
filter.age = {
$gte: parseInt(minAge),
$lte: parseInt(maxAge)
}
}
if (biodataType) {
filter.biodataType = biodataType
}
if (division) {
filter.permanentDivision = division
}
const result = await biodatasCollection.find(filter).skip(page * size).limit(size).toArray()
res.send(result)
})
app.get('/biodata-count', async (req, res) => {
const count = await biodatasCollection.estimatedDocumentCount()
res.send({ count })
})
app.get('/premium-biodatas', async (req, res) => {
const premiumMembersBiodata = await userCollection.aggregate([
{
$match: { role: 'premium' },
},
{
$lookup: {
from: 'biodatas',
localField: 'email',
foreignField: 'email',
as: 'biodata'
},
},
{
$unwind: '$biodata'
},
{
$sort: { 'biodata.age': 1 }
},
{
$limit: 6,
},
{
$project: {
_id: '$biodata._id',
name: '$biodata.name',
biodataId: '$biodata.biodataId',
biodataType: '$biodata.biodataType',
profileImage: '$biodata.profileImage',
permanentDivision: '$biodata.permanentDivision',
age: '$biodata.age',
occupation: '$biodata.occupation',
}
}
]).toArray()
console.log(premiumMembersBiodata);
res.send(premiumMembersBiodata)
})
app.get('/biodatas/:email', async (req, res) => {
const email = req.params.email
const result = await biodatasCollection.findOne({ email })
res.send(result)
})
app.get('/biodata-details/:id', async (req, res) => {
const id = req.params.id
const query = { _id: new ObjectId(id) }
const result = await biodatasCollection.findOne(query)
res.send(result)
})
app.put('/biodatas', async (req, res) => {
const biodataInfo = req.body;
const email = biodataInfo.email
// const updateDoc = {
// $set: {
// ...biodataInfo,
// }
// }
const isExist = await biodatasCollection.findOne({ email })
if (isExist) {
const result = await biodatasCollection.updateOne({ email }, { $set: req.body })
res.send(result)
console.log('line 144 console', result);
} else {
const biodataCount = await biodatasCollection.estimatedDocumentCount()
const newBiodata = {
...biodataInfo,
biodataId: biodataCount + 1
}
const result = await biodatasCollection.insertOne(newBiodata)
res.send(result)
console.log('line 153 console', result);
}
})
app.patch('/biodatas/status/:id', async (req, res) => {
const id = req.params.id;
const biodataStatus = req.body.biodataStatus;
const query = { _id: new ObjectId(id) }
const updateDoc = {
$set: {
biodataStatus
}
}
const result = await biodatasCollection.updateOne(query, updateDoc)
// console.log(result);
res.send(result)
})
app.patch('/biodata/make-premium/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) }
const updateDoc = {
$set: {
biodataStatus: 'premium'
}
}
const result = await biodatasCollection.updateOne(query, updateDoc)
// console.log(result);
res.send(result)
})
// favourites api
app.post('/favourites', async (req, res) => {
const favouriteBiodata = req.body;
const result = await favouritesCollection.insertOne(favouriteBiodata)
res.send(result)
})
app.get('/favourites/:email', async (req, res) => {
const email = req.params.email;
const query = { email }
const result = await favouritesCollection.find(query).toArray()
res.send(result)
})
app.delete('/favourites/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) }
const result = await favouritesCollection.deleteOne(query)
console.log(result);
res.send(result)
})
// contact request api
app.get('/contact-request', async (req, res) => {
const pendingContactRequest = { status: 'pending' }
const result = await paymentCollection.find(pendingContactRequest).toArray()
console.log(result);
res.send(result)
})
app.patch('/contact-request/:id', async (req, res) => {
const id = req.params.id
const query = { _id: new ObjectId(id) }
const updateDoc = {
$set: {
status: 'approved'
}
}
const result = await paymentCollection.updateOne(query, updateDoc)
res.send(result)
})
app.delete('/contact-request/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) }
const result = await paymentCollection.deleteOne(query)
res.send(result)
})
// payment api
app.post('/create-payment-intent', async (req, res) => {
const { price } = req.body;
const amount = parseInt(price * 100);
console.log(process.env.STRIPE_SECRET_KEY, 'stripe key')
console.log(amount, 'amount inside the intent')
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: 'BDT',
"payment_method_types": [
"card"
],
});
console.log(paymentIntent);
res.send({
clientSecret: paymentIntent.client_secret
})
});
app.post('/payments', async (req, res) => {
const payment = req.body;
const paymentResult = await paymentCollection.insertOne(payment)
res.send({ paymentResult })
})
app.get('/payments/:email', verifyToken, async (req, res) => {
const email = req.params.email;
const query = { email }
const result = await paymentCollection.find(query).toArray()
res.send(result)
})
// success story api
app.post('/success-story', async (req, res) => {
const successStory = req.body
const result = await successStoryCollection.insertOne(successStory)
res.send(result)
})
app.get('/success-story', async (req, res) => {
const sortedByTimestamp = { marriageTimestamp: -1 }
const result = await successStoryCollection.find().sort(sortedByTimestamp).toArray()
res.send(result)
})
app.get('/admin-success-story', async (req, res) => {
const result = await successStoryCollection.aggregate([
{
$lookup: {
from: 'biodatas',
localField: 'selfBiodataNumber',
foreignField: 'biodataId',
as: 'selfBiodata',
},
},
{
$unwind: '$selfBiodata',
},
{
$lookup: {
from: 'biodatas',
localField: 'partnerBiodataNumber',
foreignField: 'biodataId',
as: 'partnerBiodata',
},
},
{
$unwind: '$partnerBiodata',
},
{
$project: {
maleBiodataId: '$selfBiodata.biodataId',
femaleBiodataId: '$partnerBiodata.biodataId',
maleBiodataType: '$selfBiodata.biodataType',
femaleBiodataType: '$partnerBiodata.biodataType',
successStoryText: 1
},
},
]).toArray();
res.send(result)
})
// admin stats api
app.get('/admin-stats', async (req, res) => {
const totalBiodataCount = await biodatasCollection?.estimatedDocumentCount()
const maleQuery = { biodataType: 'male' }
const totalMaleBiodataCount = (await biodatasCollection.find(maleQuery).toArray()).length
const femaleQuery = { biodataType: 'female' }
const totalFemaleBiodataCount = (await biodatasCollection.find(femaleQuery).toArray()).length
const premiumQuery = { biodataStatus: 'premium' }
const totalPremiumBiodataCount = (await biodatasCollection.find(premiumQuery).toArray()).length
const totalRevenueCount = await paymentCollection.aggregate([
{
$group: {
_id: null,
totalRevenue: {
$sum: '$price'
}
}
}
]).toArray()
const revenue = totalRevenueCount.length > 0 ? totalRevenueCount[0].totalRevenue : 0
console.log(totalBiodataCount, totalMaleBiodataCount, totalFemaleBiodataCount);
res.send({ totalBiodataCount, totalMaleBiodataCount, totalPremiumBiodataCount, totalFemaleBiodataCount, revenue })
})
// Send a ping to confirm a successful connection
// await client.db("admin").command({ ping: 1 });
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('Hello from SoulMateConnect Server..')
})
app.listen(port, () => {
console.log(`SoulMateConnect is running on port ${port}`)
})