-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
327 lines (294 loc) · 9.51 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
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const jwt = require("jsonwebtoken");
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const stripe = require("stripe")(process.env.PAYMENT_SECRET);
const app = express();
const port = process.env.PORT || 5000;
// middleware
app.use(cors());
app.use(express.json());
// JWT Verification
const verifyJwt = async (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).send({ message: "Unauthorized Access" });
}
const token = authHeader.split(" ")[1];
await jwt.verify(token, process.env.SECRET_TOKEN, (err, decoded) => {
if (err) {
return res.status(403).send({ message: "Forbidden Access" });
}
req.decoded = decoded;
next();
});
};
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@alliedcluster.blmq2.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
const run = async () => {
try {
await client.connect();
const partsCollection = client.db("AlliedParts").collection("parts");
const userCollection = client.db("AlliedParts").collection("users");
const reviewCollection = client.db("AlliedParts").collection("reviews");
const orderCollection = client.db("AlliedParts").collection("orders");
const blogCollection = client.db("AlliedParts").collection("blogs");
// Verify Admin
const verifyAdmin = async (req, res, next) => {
const requesterUid = req.decoded.uid;
const requesterAccount = await userCollection.findOne({
uid: requesterUid,
});
if (requesterAccount.role === "admin") {
next();
} else {
res.status(401).send({ message: "Unauthorized Access" });
}
};
// Create a payment Intent
app.post("/create-payment-intent", verifyJwt, async (req, res) => {
const { price } = req.body;
const amount = price * 100;
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency: "usd",
payment_method_types: ["card"],
});
res.send({ clientSecret: paymentIntent.client_secret });
});
// Create User
app.put("/user/:uid", async (req, res) => {
let updateInfo = {};
const user = req.body.loggedUser;
const uid = req.params.uid;
const query = { uid };
const options = { upsert: true };
const existUser = await userCollection.findOne(query);
if (existUser) {
updateInfo = {
$set: existUser,
};
} else if (!user.role) {
user.role = "user";
updateInfo = {
$set: user,
};
} else {
updateInfo = {
$set: user,
};
}
const result = await userCollection.updateOne(query, updateInfo, options);
const token = jwt.sign({ uid }, process.env.SECRET_TOKEN, {
expiresIn: "12h",
});
res.send({ result, token });
});
// Get all parts
app.get("/parts", async (req, res) => {
const result = await partsCollection.find({}).sort({ _id: -1 }).toArray();
res.send(result);
});
// Get 3 parts
app.get("/parts/3", async (req, res) => {
const result = await partsCollection
.find({})
.sort({ _id: -1 })
.limit(3)
.toArray();
res.send(result);
});
// Get all Review
app.get("/reviews", async (req, res) => {
const result = await reviewCollection
.find({})
.sort({ _id: -1 })
.toArray();
res.send(result);
});
// Get 6 review
app.get("/review/6", async (req, res) => {
const result = await reviewCollection
.find({})
.sort({ _id: -1 })
.limit(6)
.toArray();
res.send(result);
});
// Get single item
app.get("/item/:itemId", verifyJwt, async (req, res) => {
const itemId = req.params.itemId;
const query = { _id: { $in: [ObjectId(itemId)] } };
const item = await partsCollection.findOne(query);
res.send(item);
});
// Create an order
app.post("/order/:itemId", async (req, res) => {
const itemId = req.params.itemId;
const details = req.body;
const result = await orderCollection.insertOne(details);
res.send(result);
});
// Get a single user orders
app.get("/my-order", verifyJwt, async (req, res) => {
const uid = req.query.uid;
const query = { uid };
const result = await orderCollection
.find(query)
.sort({ _id: -1 })
.toArray();
res.send(result);
});
// Get an order Details
app.get("/order/:orderId", verifyJwt, async (req, res) => {
const orderId = req.params.orderId;
const query = { _id: ObjectId(orderId) };
const order = await orderCollection.findOne(query);
res.send(order);
});
// Paid an order
app.put("/payment/:orderId", verifyJwt, async (req, res) => {
const orderId = req.params.orderId;
const transactionId = req.body.transactionId;
const query = { _id: ObjectId(orderId) };
const options = { upsert: false };
const updatePayment = { $set: { status: "Pending", transactionId } };
const result = await orderCollection.updateOne(
query,
updatePayment,
options
);
res.send(result);
});
// Delete an order
app.delete("/delete/:orderId", verifyJwt, async (req, res) => {
const orderId = req.params.orderId;
const query = { _id: ObjectId(orderId) };
const result = await orderCollection.deleteOne(query);
res.send(result);
});
// Add Review
app.post("/add-review/:uid", verifyJwt, async (req, res) => {
const uid = req.params.uid;
const review = req.body.review;
const exist = await reviewCollection.findOne({ uid });
if (exist) {
return res.send({
success: false,
message: "Your Review Already exist !",
});
} else {
const result = await reviewCollection.insertOne(review);
return res.send({ success: true, result });
}
});
// Check Admin
app.get("/admin/:uid", async (req, res) => {
const uid = req.params.uid;
const user = await userCollection.findOne({ uid });
const isAdmin = user?.role === "admin";
res.send({ admin: isAdmin });
});
// Get All Orders
app.get("/orders", verifyJwt, verifyAdmin, async (req, res) => {
const query = {};
const result = await orderCollection.find(query).toArray();
res.send(result);
});
// Status Update
app.put("/shift/:orderId", verifyJwt, verifyAdmin, async (req, res) => {
const orderId = req.params.orderId;
const query = { _id: ObjectId(orderId) };
const options = { upsert: false };
const update = { $set: { status: "Shifted" } };
const result = await orderCollection.updateOne(query, update, options);
res.send(result);
});
// Get All user
app.get("/users", verifyJwt, async (req, res) => {
const query = {};
const users = await userCollection.find(query).toArray();
res.send(users);
});
// Delete an user
app.delete(
"/delete-user/:uid",
verifyJwt,
verifyAdmin,
async (req, res) => {
const uid = req.params.uid;
const query = { _id: ObjectId(uid) };
const result = await userCollection.deleteOne(query);
res.send(result);
}
);
// Add an item
app.post("/add-item", verifyJwt, verifyAdmin, async (req, res) => {
const item = req.body.item;
const result = await partsCollection.insertOne(item);
res.send(result);
});
// Delete an Item
app.delete(
"/delete-item/:itemId",
verifyJwt,
verifyAdmin,
async (req, res) => {
const itemId = req.params.itemId;
const query = { _id: ObjectId(itemId) };
const result = await partsCollection.deleteOne(query);
res.send(result);
}
);
// Make Admin
app.put("/make-admin/:uid", verifyJwt, verifyAdmin, async (req, res) => {
const uid = req.params.uid;
const query = { _id: ObjectId(uid) };
const options = { upsert: false };
const update = { $set: { role: "admin" } };
const result = await userCollection.updateOne(query, update, options);
res.send(result);
});
// Get Blogs
app.get("/blogs", async (req, res) => {
const query = {};
const result = await blogCollection.find(query).toArray();
res.send(result);
});
// Get user Details
app.get("/profile/:uid", verifyJwt, async (req, res) => {
const uid = req.params.uid;
const query = { uid };
const result = await userCollection.findOne(query);
res.send(result);
});
// UPdate user data
app.put("/profile-update/:uid", async (req, res) => {
const uid = req.params.uid;
const details = req.body.details;
const query = { uid };
const options = { upsert: false };
const update = { $set: details };
const result = await userCollection.updateOne(query, update, options);
res.send(result);
});
//
app.all("*", (req, res) => {
res.status(404).send("404 Not Found");
});
} finally {
// client.close()
}
};
run().catch(console.dir);
app.get("/", async (req, res) => {
res.send("Let's Explore Allied Server");
});
app.listen(port, () => {
console.log("Allied server is running on : ", port);
});