-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
414 lines (365 loc) · 12 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
const express = require("express");
require("dotenv").config();
const cors = require("cors");
const nodemailer = require("nodemailer");
const jwt = require("jsonwebtoken");
const port = process.env.PORT || 5000;
const { formatISO } = require("date-fns");
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
// Init Express
const app = express();
// Middleware
app.use(express.json());
app.use(
cors({
origin: [
"http://localhost:5173",
"https://diagnostic-management-312cf.web.app",
],
credentials: true,
})
);
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.apymwsq.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,
},
});
// Send Mail
const sendEmail = async (userEmail, emailData) => {
const transporter = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
port: 587,
secure: false, // Use `true` for port 465, `false` for all other ports
auth: {
user: process.env.APP_EMAIL,
pass: process.env.APP_KEY,
},
});
const emailBody = {
from: `"Sheba Diagnostic Center" <${process.env.APP_EMAIL}>`, // sender address
to: userEmail, // list of receivers
subject: emailData.subject, // Subject line
// plain text body
html: emailData.message, // html body
};
transporter.sendMail(emailBody, (error, info) => {
if (error) {
console.log(error);
} else {
console.log("from mailer", info.response);
}
});
};
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
// await client.connect();
// Send a ping to confirm a successful connection
// Create Database and Collection
const userCollection = client.db("diagnosticDB").collection("users");
const testCollection = client.db("diagnosticDB").collection("tests");
const bannerCollection = client.db("diagnosticDB").collection("banners");
const bookingCollection = client.db("diagnosticDB").collection("bookings");
const newsCollection = client.db("diagnosticDB").collection("news");
const healthRecommendationsCollection = client
.db("diagnosticDB")
.collection("healthRecommendations");
// Admin Verify
const adminVerify = async (req, res, next) => {
const email = req.decoded.email;
const filter = { email: email };
const user = await userCollection.findOne(filter);
const isAdmin = user?.role === "admin";
if (!isAdmin) {
return res.status(403).send({ message: "Forbidden Access" });
}
next();
};
// JWT Related Route
app.post("/jwt", (req, res) => {
// Get User Info
const email = req.body;
// Create Token
const token = jwt.sign(email, process.env.ACCESS_TOKEN, {
expiresIn: "1h",
});
res.send({ token });
});
// Token Verify
const verifyToken = (req, res, next) => {
// Check 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, (err, decoded) => {
if (err) {
return res.status(401).send({ message: "Unauthorized Access" });
}
req.decoded = decoded;
next();
});
};
// Get Admin User
app.get("/user-admin/:email", verifyToken, async (req, res) => {
const email = req.params.email;
if (email !== req.decoded.email) {
return res.status(403).send({ message: "Unauthorized Access" });
}
const query = { email: email };
const user = await userCollection.findOne(query);
let admin = false;
if (user) {
admin = user?.role === "admin";
}
res.send({ admin });
});
// Create User
app.post("/user", async (req, res) => {
const userInfo = req.body;
const result = await userCollection.insertOne(userInfo);
res.send(result);
});
// Get All User
app.get("/user", verifyToken, async (req, res) => {
const result = await userCollection.find().toArray();
res.send(result);
});
// Get Single User
app.get("/user/current/:email", verifyToken, async (req, res) => {
const email = req.params.email;
const filter = { email: email };
const result = await userCollection.findOne(filter);
res.send(result);
});
// Update User Status
app.patch("/user/:id", verifyToken, adminVerify, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const { status } = req.body;
const updateDoc = {
$set: {
status: status,
},
};
const result = await userCollection.updateOne(query, updateDoc);
res.send(result);
});
// Update User Role
app.patch("/user/role/:id", verifyToken, adminVerify, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const { role } = req.body;
const updateDoc = {
$set: {
role: role,
},
};
const result = await userCollection.updateOne(query, updateDoc);
res.send(result);
});
// Update User Status
app.put("/user/update/:id", verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const userInfo = req.body;
const updateDoc = {
$set: {
name: userInfo.name,
photo: userInfo.photo,
},
};
const result = await userCollection.updateOne(query, updateDoc);
res.send(result);
});
// Create Test
app.post("/tests", verifyToken, adminVerify, async (req, res) => {
const testInfo = req.body;
const result = await testCollection.insertOne(testInfo);
res.send(result);
});
app.get("/tests", async (req, res) => {
const result = await testCollection.find().toArray();
res.send(result);
});
// Get Single Test
app.get("/tests/:id", verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await testCollection.findOne(query);
res.send(result);
});
// Update Test
app.put("/tests/update/:id", verifyToken, adminVerify, async (req, res) => {
const id = req.params.id;
const testData = req.body;
const filter = { _id: new ObjectId(id) };
const options = { upsert: true };
// Update
const updateDoc = {
$set: {
...testData,
},
};
// Now Send To DB
const result = await testCollection.updateOne(filter, updateDoc, options);
res.send(result);
});
// Delete Test
app.delete(
"/test/delete/:id",
verifyToken,
adminVerify,
async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await testCollection.deleteOne(query);
res.send(result);
}
);
// Create Banner
app.post("/banner", verifyToken, adminVerify, async (req, res) => {
const bannerInfo = req.body;
const result = await bannerCollection.insertOne(bannerInfo);
res.send(result);
});
// Get All Banners
app.get("/banner", async (req, res) => {
const result = await bannerCollection.find().toArray();
res.send(result);
});
// Update Banner Status
app.patch("/banner/:id", verifyToken, adminVerify, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const info = req.body;
const updateDoc = {
$set: {
isActive: info.status,
},
};
const result = await bannerCollection.updateOne(query, updateDoc);
res.send(result);
});
// Delete Banner
app.delete("/banner/:id", verifyToken, adminVerify, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await bannerCollection.deleteOne(query);
res.send(result);
});
// Booking Related Api
app.post("/booking", verifyToken, async (req, res) => {
const bookingInfo = req.body;
// Update Slots
await testCollection.updateOne(
{ _id: new ObjectId(bookingInfo.testId) },
{ $inc: { slots: -1 } }
);
const result = await bookingCollection.insertOne(bookingInfo);
res.send(result);
});
// Get All Bookings
app.get("/bookings", async (req, res) => {
const filter = req.query;
let query = {};
if (filter.search) {
query = {
"patientInfo.email": { $regex: filter?.search, $options: "i" },
};
}
const result = await bookingCollection.find(query).toArray();
res.send(result);
});
// Get All Delivered Booking Data
app.get("/bookings/delivered", async (req, res) => {
let query = { report: "Delivered" };
const result = await bookingCollection.find(query).toArray();
res.send(result);
});
// Update Booking
app.patch("/booking/status/:id", verifyToken, async (req, res) => {
const id = req.params.id;
const testReport = req.body;
const query = { _id: new ObjectId(id) };
// Get Booked User From DB
const bookedUser = await bookingCollection.findOne(query);
// Send Email
if (testReport.status == "Delivered") {
sendEmail(bookedUser?.patientInfo?.email, {
subject: "Report Delivered Successful",
message: `Hello ${bookedUser?.patientInfo?.name} Your Report Delivered Successful`,
});
}
const updateDoc = {
$set: {
report: testReport.status,
},
};
const result = await bookingCollection.updateOne(query, updateDoc);
res.send(result);
});
// Get All Upcoming Bookings
app.get("/booking/upcomin/:email", verifyToken, async (req, res) => {
const email = req.params.email;
const filter = { "patientInfo.email": email };
const result = await bookingCollection.find(filter).toArray();
res.send(result);
});
// Delete Booking
app.delete("/booking/delete/:id", verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await bookingCollection.deleteOne(query);
res.send(result);
});
// Get All Reservations
app.get("/reservation/:id", verifyToken, async (req, res) => {
const id = req.params.id;
const filter = { testId: id };
const result = await bookingCollection.find(filter).toArray();
res.send(result);
});
// Create Payment Intent
app.post("/create-payment-intent", async (req, res) => {
const { price } = req.body;
const amount = parseInt(price * 100);
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: "usd",
payment_method_types: ["card"],
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
// Get All Health Recommendations
app.get("/health/recommendation", async (req, res) => {
const result = await healthRecommendationsCollection.find().toArray();
res.send(result);
});
// Get All News
app.get("/news", async (req, res) => {
const result = await newsCollection.find().toArray();
res.send(result);
});
// 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);
// Listen Server
app.listen(port, () => {
console.log(`Server is Running On PORT ${port}`);
});