-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
219 lines (195 loc) · 6.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
const { MongoClient, ServerApiVersion } = require("mongodb");
const admin = require("firebase-admin");
const express = require("express");
const app = express();
const ObjectId = require("mongodb").ObjectId;
require("dotenv").config();
const cors = require("cors");
const port = process.env.PORT || 5000;
// initialize firebase admin
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, "\n"),
}),
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
// middleware
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.6jlv6.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
// Verify token
async function verifyToken(req, res, next) {
if (req.headers?.authorization?.startsWith("Bearer ")) {
const idToken = req.headers.authorization.split("Bearer ")[1];
try {
const decodedIdToken = await admin.auth().verifyIdToken(idToken);
req.decodedEmail = decodedIdToken.email;
} catch (error) {
console.error("Error while verifying token:", error);
res.status(403).send("Unauthorized");
}
}
next();
}
async function run() {
try {
await client.connect();
const database = client.db("jerins_parlour");
const servicesCollection = database.collection("services");
const bookingsCollection = database.collection("bookings");
const reviewsCollection = database.collection("reviews");
const usersCollection = database.collection("users");
const contactCollection = database.collection("contacts");
// add services to database
app.post("/services", async (req, res) => {
const service = req.body;
const result = await servicesCollection.insertOne(service);
res.send(result);
});
// get service by id
app.get("/services/:id", async (req, res) => {
const id = req.params.id;
const result = await servicesCollection.findOne({
_id: new ObjectId(id),
});
res.send(result);
});
// delete service by id
app.delete("/services/:id", async (req, res) => {
const id = req.params.id;
const result = await servicesCollection.deleteOne({
_id: new ObjectId(id),
});
res.send(result);
});
// get 3 services
app.get("/services", async (req, res) => {
const result = await servicesCollection.find({}).limit(3).toArray();
res.send(result);
});
// get all services
app.get("/allServices", async (req, res) => {
const result = await servicesCollection.find({}).toArray();
res.send(result);
});
// add booking to database
app.post("/bookings", async (req, res) => {
const booking = req.body;
const result = await bookingsCollection.insertOne(booking);
res.send(result);
});
// get all bookings from database
app.get("/bookings", async (req, res) => {
const result = await bookingsCollection.find({}).toArray();
res.send(result);
});
// get bookings by email
app.get("/bookings/:email", async (req, res) => {
const email = req.params.email;
const result = await bookingsCollection.find({ email: email }).toArray();
res.send(result);
});
// // get single booking by id
app.get("/bookings/:id", async (req, res) => {
const id = req.params.id;
const result = await bookingsCollection.findOne({
_id: new ObjectId(id),
});
res.send(result);
});
// update order status in bookings collection
app.put("/bookings/:id", async (req, res) => {
const id = req.params.id;
const status = req.body;
const result = await bookingsCollection.updateOne(
{ _id: new ObjectId(id) },
{ $set: { orderStatus: status } }
);
res.send(result);
});
// delete bookings by id
app.delete("/bookings/:id", async (req, res) => {
const id = req.params.id;
const result = await bookingsCollection.deleteOne({
_id: new ObjectId(id),
});
res.send(result);
});
// send service reviews
app.post("/reviews", async (req, res) => {
const review = req.body;
const result = await reviewsCollection.insertOne(review);
res.send(result);
});
// save users to database
app.post("/users", async (req, res) => {
const user = req.body;
const result = await usersCollection.insertOne(user);
res.send(result);
});
// check if user roll is admin or not
app.get("/users/:email", async (req, res) => {
const email = req.params.email;
const query = { email: email };
const user = await usersCollection.findOne(query);
let isAdmin = false;
if (user?.role === "admin") {
isAdmin = true;
}
res.json({ admin: isAdmin });
});
// uodate user if not existed
app.put("/users", async (req, res) => {
const user = req.body;
const query = { email: user.email };
const result = await usersCollection.updateOne(
query,
{ $set: user },
{ upsert: true }
);
res.json(result);
});
// add admin roll to user
app.put("/users/makeAdmin", verifyToken, async (req, res) => {
const user = req.body;
const requester = req.decodedEmail;
if (requester) {
const requesterAccount = await usersCollection.findOne({
email: requester,
});
if (requesterAccount.role === "admin") {
const filter = { email: user.email };
const updateDoc = { $set: { role: "admin" } };
const result = await usersCollection.updateOne(filter, updateDoc);
res.json(result);
}
} else {
res
.status(403)
.json({ message: "you do not have access to make admin" });
}
});
// post contact message to database
app.post("/contacts", async (req, res) => {
const contact = req.body;
const result = await contactCollection.insertOne(contact);
res.send(result);
});
} finally {
// await client.close();
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("Jerins Parlour");
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});