-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
431 lines (384 loc) · 13.4 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
const express = require("express");
const app = express();
const cors = require("cors");
const nodemailer = require("nodemailer");
const mg = require("nodemailer-mailgun-transport");
require("dotenv").config();
const port = process.env.PORT || 5000;
const stripe = require("stripe")(process.env.PAYMENT_SECRET_KEY);
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const moment = require("moment");
// middleware
app.use(cors());
app.use(express.json());
const auth = {
auth: {
api_key: process.env.EMAIL_PRIVATE_KEY,
domain: process.env.EMAIL_DOMAIN,
},
};
const transporter = nodemailer.createTransport(mg(auth));
// send payment confirmation email
const sendPaymentConfirmationEmail = (
payment,
className,
instructorName,
price
) => {
const startDate = moment().add(7, "days");
const endDate = moment(startDate).add(25, "days");
const durationInDays = endDate.diff(startDate, "days");
transporter.sendMail(
{
from: "tamim200091@gmail.com", // verified sender email
to: payment.studentEmail, // recipient email
subject: "Thank you for your course purchase!", // Subject line
text: "Hello world!", // plain text body
html: `
<div
style="width: 85%; background-color: #0e0d0d; color: white; padding: 20px; border-radius: 20px;">
<div style="text-align: center;">
<img style="width: 48%; margin-bottom: 20px;" src="https://i.ibb.co/7gCjkHF/pmbia-logo-word-reverse.png" />
</div>
<p>${moment(new Date()).format("Do MMMM, YYYY")}</p>
<p>Dear ${payment.studentName},</p>
<br />
<p>
Thank you for choosing PMBIA (Professional Mountain Biking Instructors Association) for your mountain biking
course. We are thrilled to have you join us and embark on this exciting journey of learning and adventure!
</p>
<br />
<p>Course Details:</p>
<p>Course Name: ${className}</p>
<p>Instructor: ${instructorName}</p>
<p>Start Date: ${startDate.format("Do MMMM YYYY")}</p>
<p>End Date: ${endDate.format("Do MMMM YYYY")}</p>
<p>Duration: ${durationInDays} days</p>
<p>Location: Duifkruid 84, 4007 SZ Tiel, Netherlands</p>
<br />
<p>Payment Details:</p>
<p>Transaction ID: ${payment.transactionId}</p>
<p>Payment Amount: $ ${price}</p>
<p>Date of Payment: ${moment(payment.date).format(
"dddd, Do MMMM YYYY, hh:mm a"
)}</p>
<br />
<p>Best regards,</p>
<p>Tanzeebul Tamim,</p>
<p>Chief Executive Officer,</p>
<p>PMBIA ltd</p>
<br />
<div style="text-align: center;">
<h2>Team PMBIA</h2>
<p>Delivering exceptional services since 2006.</p>
<p><strong>Site :</strong> <span
style="text-decoration-line: underline; color: #4285F4;">https://pmbia-55816.web.app/</span></p>
<p><strong>Address :</strong> Duifkruid 84, 4007 SZ Tiel, Netherlands</p>
<p><strong>Phone :</strong> +31644460635 | <strong>Email :</strong> info@pmbia.com</p>
</div>
</div>
`, // html body
},
function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
}
);
};
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.y70m6ei.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,
},
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
// await client.connect();
const userCollection = client.db("PMBIA").collection("users");
const bookingsCollection = client.db("PMBIA").collection("bookings");
// save user in db
app.put("/users/:email", async (req, res) => {
try {
const email = req.params.email;
const user = req.body;
const query = { email: email };
const options = { upsert: true };
let updateDoc = {};
if (user.name) {
updateDoc.name = user.name;
}
if (user.image) {
updateDoc.image = user.image;
}
if (user.email) {
updateDoc.email = user.email;
}
if (user.gender) {
updateDoc.gender = user.gender;
}
if (user.contactNo) {
updateDoc.contactNo = user.contactNo;
}
if (user.address) {
updateDoc.address = user.address;
}
if (user.role) {
updateDoc.role = user.role;
}
if (user.quote) {
updateDoc.quote = user.quote;
}
const result = await userCollection.updateOne(
query,
{ $set: updateDoc },
options
);
res.send(result);
} catch (error) {
console.error(error);
res.status(500).send("Error updating user data.");
}
});
// get user from db
app.get("/users/:email", async (req, res) => {
const email = req.params.email;
const query = { email: email };
const result = await userCollection.findOne(query);
res.send(result);
});
// instructors api's--------------------------------------------------------------
// get all instructors
app.get("/instructors", async (req, res) => {
const count = parseInt(req.query.count) || 0;
const search = req.query.search;
const query = search
? { role: "Instructor", name: { $regex: search, $options: "i" } }
: { role: "Instructor" };
const result = await userCollection.find(query).limit(count).toArray();
res.send(result);
});
// get number of instructors
app.get("/instructors/total", async (req, res) => {
const query = { role: "Instructor" };
const count = await userCollection.countDocuments(query);
res.send({ totalInstructors: count });
});
// get top 6 instructors & get instructors with total students
app.get("/instructors/top", async (req, res) => {
const query = { role: "Instructor" };
const instructors = await userCollection.find(query).toArray();
const instructorsWithTotalStudents = instructors.map((instructor) => {
const totalStudents = instructor?.classes?.reduce(
(total, classItem) => total + classItem.totalStudent,
0
);
return { ...instructor, totalStudents };
});
const sortedInstructors = instructorsWithTotalStudents.sort(
(a, b) => b.totalStudents - a.totalStudents
);
const topInstructors = sortedInstructors.slice(0, 6);
res.send({ topInstructors, instructorsWithTotalStudents });
});
// get single instructor
app.get("/instructor/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id), role: "Instructor" };
const result = await userCollection.findOne(query);
res.send(result);
});
// update instructors available seat
app.put("/instructor/updateStudentCount", async (req, res) => {
const { instructorId, classIndex } = req.body;
const query = { _id: new ObjectId(instructorId), role: "Instructor" };
const instructor = await userCollection.findOne(query);
let classItem = instructor.classes[classIndex];
classItem.totalStudent += 1;
const update = { $set: { classes: instructor.classes } };
const result = await userCollection.updateOne(query, update);
res.send(result);
});
// classes api's------------------------------------------------------------------------------
// get all classes
app.get("/classes", async (req, res) => {
const count = parseInt(req.query.count);
const search = req.query.search;
const query = { role: "Instructor", classes: { $exists: true, $ne: [] } };
const instructors = await userCollection.find(query).toArray();
let classes = instructors.flatMap((instructor) => {
const instructorName = instructor.name;
const instructorId = instructor._id;
return instructor?.classes?.map((classItem, classIndex) => ({
...classItem,
instructorName,
instructorId,
classIndex,
}));
});
if (search) {
classes = classes.filter((classItem) =>
classItem?.name.toLowerCase().includes(search.toLowerCase())
);
}
classes = classes.slice(0, count);
res.send(classes);
});
// get number of classes
app.get("/classes/total", async (req, res) => {
const query = { role: "Instructor" };
const instructors = await userCollection.find(query).toArray();
const totalClasses = instructors.reduce((total, instructor) => {
return total + (instructor?.classes?.length || 0);
}, 0);
res.send({ totalClasses });
});
// get top 6 classes
app.get("/classes/top", async (req, res) => {
const query = { role: "Instructor" };
const instructors = await userCollection.find(query).toArray();
let allClasses = instructors.flatMap((instructor) => {
const instructorName = instructor.name;
const instructorImg = instructor.image;
const instructorId = instructor._id;
return instructor?.classes?.map((classItem) => ({
...classItem,
instructorName,
instructorImg,
instructorId,
}));
});
const sortedClasses = allClasses.sort(
(a, b) => b.totalStudent - a.totalStudent
);
const topClasses = sortedClasses.slice(0, 6);
res.send(topClasses);
});
// post a booking
app.put("/book-class", async (req, res) => {
const {
studentId,
instructorId,
studentEmail,
studentName,
classIndex,
paymentStatus,
transactionId,
date,
} = req.body;
const query = { _id: new ObjectId(instructorId), role: "Instructor" };
const options = { upsert: true };
const instructor = await userCollection.findOne(query);
const selectedClass = instructor?.classes[classIndex];
const booking = {
studentId: new ObjectId(studentId),
studentEmail,
studentName,
instructorName: instructor?.name,
instructorId: instructor?._id,
"class-name": selectedClass?.name,
classImage: selectedClass?.image,
classFee: selectedClass?.price,
paymentStatus,
classIndex,
transactionId,
date,
};
const updateDoc = { $set: booking };
const result = await bookingsCollection.updateOne(
{
"class-name": selectedClass.name,
studentId: new ObjectId(studentId),
instructorId: new ObjectId(instructorId),
classIndex,
},
updateDoc,
options
);
// send confirmation email
if (paymentStatus == "paid") {
sendPaymentConfirmationEmail(
req.body,
selectedClass.name,
instructor.name,
selectedClass.price
);
}
res.send(result);
});
// get user bookings
app.get("/book-class/:studentId", async (req, res) => {
const { studentId } = req.params;
const query = { studentId: new ObjectId(studentId) };
const bookings = await bookingsCollection.find(query).toArray();
res.send(bookings);
});
// get a booking
app.get("/book-class/:studentId/:itemId", async (req, res) => {
const { studentId, itemId } = req.params;
const query = {
studentId: new ObjectId(studentId),
_id: new ObjectId(itemId),
};
const booking = await bookingsCollection.findOne(query);
res.send(booking);
});
// delete a booking
app.delete("/book-class/:studentId", async (req, res) => {
const { studentId } = req.params;
const { instructorId, classIndex } = req.body;
const query = {
instructorId: new ObjectId(instructorId),
classIndex,
studentId: new ObjectId(studentId),
};
const result = await bookingsCollection.deleteOne(query);
res.send(result);
});
// delete all bookings of a user
app.delete("/booking/:studentId", async (req, res) => {
const { studentId } = req.params;
const query = {
studentId: new ObjectId(studentId),
paymentStatus: "unpaid",
};
const result = await bookingsCollection.deleteMany(query);
res.send(result);
});
// create payment intent
app.post("/create-payment-intent", async (req, res) => {
const { price } = req.body;
const amount = parseFloat(price) * 100;
if (!price) return;
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: "usd",
payment_method_types: ["card"],
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
// 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(`PMBIA server is active on port: ${port}`);
});
app.listen(port, () => {
console.log(`PMBIA server is running on port: ${port}`);
});