-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
297 lines (253 loc) · 9.85 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
//Imports
const express = require('express');
const { MongoClient } = require('mongodb');
const cors = require('cors');
require('dotenv').config()
const ObjectId = require('mongodb').ObjectId;
const nodemailer = require("nodemailer");
const admin = require("firebase-admin");
const stripe = require('stripe')(process.env.STRIPE_SECRET);
const app = express();
const port = process.env.PORT || 8000;
//Firebase Admin Initialization
const serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
//Node mailer details
let transporter = nodemailer.createTransport({
service: 'outlook',
auth: {
user: process.env.MY_MAIL,
pass: process.env.MY_PASS
}
});
//Middleware use for server course
app.use(cors());
app.use(express.json());
//MONGO DB
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.up7gk.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// Function to verify user token so that API stays secure
async function verifyToken(req, res, next) {
if (req.headers?.authorization?.startsWith('Bearer')) {
// Splitting the idToken by removing string "Bearer"
const courseIdToken = req.headers.authorization.split('Bearer ')[1];
try {
const decodedUser = await admin.auth().verifyIdToken(courseIdToken);
req.decodedUserEmail = decodedUser.email;
}
catch {
}
}
next();
}
async function run() {
try {
await client.connect();
const database = client.db("courseApp");
const usersCollection = database.collection("users");
const coursesCollection = database.collection("courses");
const orderCollection = database.collection("orders");
const reviewsCollection = database.collection("reviews");
//GET COURSES FROM DB
app.get('/courses', async (req, res) => {
const cursor = coursesCollection.find({});
const page = req.query.page;
const size = parseInt(req.query.size);
const count = await cursor.count();
let courses;
if (page) {
courses = await cursor.skip(page * size).limit(size).toArray();
}
else {
courses = await cursor.toArray();
}
res.send({
count,
courses
});
})
//GET SINGLE COURSE BY ID
app.get('/course/:id', async (req, res) => {
const id = req.params.id;
const query1 = { _id: ObjectId(id) }
const course = await coursesCollection.findOne(query1);
const userEmail = req?.query?.email;
let purchased = false;
if (userEmail) {
const query2 = { email: (userEmail) };
const orderDetails = await orderCollection.find(query2).toArray();
if (orderDetails.length) {
const keys = Object.keys(orderDetails[0].order)
for (const key of keys) {
if (key == course.courseID) {
purchased = true;
}
}
}
}
res.json({
course,
purchased
});
})
//USE POST to get data by keys
app.post('/courses/byKeys', async (req, res) => {
const keys = req.body;
const query = { courseID: { $in: keys } };
const courses = await coursesCollection.find(query).toArray();
res.json(courses);
})
//Add users to database those who signed up with Email Password
app.post('/users', async (req, res) => {
const user = req.body;
const result = await usersCollection.insertOne(user);
res.json(result);
})
//Add users to database those who signed up with External Provider
app.put('/users', async (req, res) => {
const user = req.body;
const filter = { email: user.email };
const options = { upsert: true };
const updateDoc = {
$set: user
};
const result = await usersCollection.updateOne(filter, updateDoc, options);
res.json(result);
})
//Add Orders API
app.post('/orders', async (req, res) => {
const order = req.body;
const name = order.name;
//Taking the keys of order course ID in an array
const orderIds = (Object.keys(req.body.order));
//Checking whether any previous order exists of same user
const query = { email: (order.email) };
const orderDetails = await orderCollection.findOne(query);
console.log(order);
console.log(orderDetails)
let result;
// If user exists with previous orders then Updating user's previous order details rather creating new one
if (orderDetails) {
for (const id of orderIds) {
(orderDetails.order)[id] = 1;
}
const filter = { _id: orderDetails._id };
const updateDoc = {
$set: {
order: orderDetails.order,
payment: [...orderDetails.payment, order.payment]
},
};
result = await orderCollection.updateOne(filter, updateDoc);
}
//If user doesn't exists with orders then inserting new order
else {
order.payment = [order.payment];
result = await orderCollection.insertOne(order);
}
// Sending email to user
let mailOptions = {
from: 'shadmansaalim321@outlook.com',
to: order.email,
subject: 'Course Application By Developer Saalim',
text: `Dear ${name}, You have successfully purchased course from us. Now you can view your course in MyClasses section on our website. Thank you for being with us. This application is developed by Saalim Shadman, a Computer Science student at RMIT, Australia
`,
};
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log('Error occurred ', err)
}
else {
console.log('Email Sent');
}
})
res.json(result);
})
app.get('/myClasses', verifyToken, async (req, res) => {
const userEmail = req.query.email;
if (req.decodedUserEmail === userEmail) {
const query1 = { email: userEmail };
//Using options to get only the order field making code efficient
const options = {
projection: { _id: 0, order: 1 }
}
const cursor = orderCollection.find(query1, options);
const orderDetails = await cursor.toArray();
// Checking if user has any purchased course
if (orderDetails.length) {
const keys = Object.keys(orderDetails[0].order)
const query2 = { courseID: { $in: keys } };
const courses = await coursesCollection.find(query2).toArray();
res.json(courses)
}
else {
res.json(0);
}
}
else {
//Sending status of unauthorization
res.status(401).json({ message: 'User Not Authorized' })
}
})
app.get('/reviews/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) }
const cursor = reviewsCollection.find(query);
const result = await cursor.toArray();
console.log(result);
res.json(result);
})
app.put('/reviews/:id', async (req, res) => {
const id = req.params.id;
const review = req.body;
console.log(id, review)
const filter = { _id: ObjectId(id) };
const course = await reviewsCollection.findOne(filter);
const courseReviews = course?.reviews
const options = { upsert: true };
let result;
if (courseReviews) {
const updateDoc = {
$set: {
reviews: [...courseReviews, review]
},
};
result = await reviewsCollection.updateOne(filter, updateDoc, options);
}
else {
const updateDoc = {
$set: {
reviews: [review]
},
};
result = await reviewsCollection.updateOne(filter, updateDoc, options);
}
res.json(result);
})
app.post("/create-payment-intent", async (req, res) => {
const paymentInfo = req.body;
const amount = paymentInfo.price * 100;
const paymentIntent = await stripe.paymentIntents.create({
currency: "usd",
amount: amount,
payment_method_types: ['card']
})
res.json({
clientSecret: paymentIntent.client_secret,
});
});
}
finally {
// await client.close();
}
}
run().catch(console.dir);
app.get('/', (req, res) => {
console.log('Hitting backend');
res.send('Course App Backend Coming Soon')
})
app.listen(port, () => {
console.log('Listening to port number ', port);
})