-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
274 lines (266 loc) · 8.65 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
const express = require("express");
const cors = require("cors");
const jwt = require("jsonwebtoken");
require("dotenv").config();
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const app = express();
const port = process.env.PORT || 5000;
//middleware
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.a8vmu.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
function verifyJWT(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).send({ message: "UnAuthorized Access" });
}
const token = authHeader.split(" ")[1];
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, function (err, decoded) {
if (err) {
return res.status(403).send({ message: "Forbidden Access" });
}
req.decoded = decoded;
next();
});
}
async function run() {
try {
// await client.connect();
client.connect();
const toolCollection = client.db("metalik").collection("tools");
const reviewCollection = client.db("metalik").collection("reviews");
const orderCollection = client.db("metalik").collection("orders");
const userCollection = client.db("metalik").collection("users");
const profileCollection = client.db("metalik").collection("profiles");
// verify admin middleware function
const verifyAdmin = async (req, res, next) => {
const requester = req.decoded.email;
const requesterAccount = await userCollection.findOne({
email: requester,
});
if (requesterAccount.role === "admin") {
next();
} else {
res.status(403).send({ message: "Forbidden" });
}
};
// payment intent api
app.post("/create-payment-intent", verifyJWT, async (req, res) => {
const order = req.body;
const price = order.price;
const amount = price * 100;
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: "usd",
payment_method_types: ["card"],
});
res.send({ clientSecret: paymentIntent.client_secret });
});
// update profile
app.put("/profile", verifyJWT, async (req, res) => {
const email = req.query.email;
const profile = req.body;
const filter = { email: email };
const options = { upsert: true };
const updatedDoc = {
$set: profile,
};
const result = await profileCollection.updateOne(
filter,
updatedDoc,
options
);
res.send(result);
});
// load all tools
app.get("/tool", async (req, res) => {
const query = {};
const cursor = toolCollection.find(query);
const tools = (await cursor.toArray()).reverse();
res.send(tools);
});
// post a new tool
app.post("/tool", verifyJWT, verifyAdmin, async (req, res) => {
const newTool = req.body;
const result = await toolCollection.insertOne(newTool);
res.send(result);
});
// get all users
app.get("/user", verifyJWT, verifyAdmin, async (req, res) => {
const query = {};
const cursor = userCollection.find(query);
const users = await cursor.toArray();
res.send(users);
});
// put users
app.put("/user/:email", async (req, res) => {
const email = req.params.email;
const user = req.body;
const filter = { email: email };
const options = { upsert: true };
const updateDoc = {
$set: user,
};
const result = await userCollection.updateOne(filter, updateDoc, options);
const token = jwt.sign(
{ email: email },
process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: "12h" }
);
res.send({ result, token });
});
// find admins
app.get("/admin/:email", async (req, res) => {
const email = req.params.email;
const user = await userCollection.findOne({ email: email });
const isAdmin = user.role === "admin";
res.send({ admin: isAdmin });
});
// put admin role
app.put("/user/admin/:email", verifyJWT, verifyAdmin, async (req, res) => {
const email = req.params.email;
const filter = { email: email };
const updateDoc = {
$set: { role: "admin" },
};
const result = await userCollection.updateOne(filter, updateDoc);
res.send(result);
});
// find one by id
app.get("/tool/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const tool = await toolCollection.findOne(query);
res.send(tool);
});
// single tool quantity update api
app.put("/tool/:id", async (req, res) => {
const id = req.params.id;
const updatedTool = req.body;
const filter = { _id: ObjectId(id) };
const options = { upsert: true };
const updatedDoc = {
$set: {
name: updatedTool.name,
img: updatedTool.img,
description: updatedTool.description,
min_order: updatedTool.min_order,
available_quantity: updatedTool.available_quantity,
price: updatedTool.price,
},
};
const result = await toolCollection.updateOne(
filter,
updatedDoc,
options
);
res.send(result);
});
// single order status update api
app.put("/order/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: ObjectId(id) };
const options = { upsert: true };
const updatedDoc = {
$set: {
status: "shipped",
},
};
const result = await orderCollection.updateOne(
filter,
updatedDoc,
options
);
res.send(result);
});
// load reviews from db
app.get("/review", async (req, res) => {
const query = {};
const cursor = reviewCollection.find(query);
const reviews = (await cursor.toArray()).reverse();
res.send(reviews);
});
// post review from ui to db
app.post("/review", async (req, res) => {
const newReview = req.body;
const result = await reviewCollection.insertOne(newReview);
res.send(result);
});
// post order from ui to db
app.post("/order", async (req, res) => {
const newOrder = req.body;
const result = await orderCollection.insertOne(newOrder);
res.send(result);
});
// update order data
app.patch("/order/:id", verifyJWT, async (req, res) => {
const id = req.params.id;
const payment = req.body;
const query = { _id: ObjectId(id) };
const updatedDoc = {
$set: {
paid: true,
transactionId: payment.transactionId,
},
};
const updatedOrder = await orderCollection.updateOne(query, updatedDoc);
res.send(updatedOrder);
});
// my orders from order collection
app.get("/orders", verifyJWT, async (req, res) => {
const email = req.query.email;
const decodedEmail = req.decoded.email;
if (email === decodedEmail) {
const query = { email };
const cursor = orderCollection.find(query);
const orders = await cursor.toArray();
return res.send(orders);
} else {
return res.status(403).send({ message: "Forbidden Access" });
}
});
// all orders from order collection
app.get("/order", verifyJWT, verifyAdmin, async (req, res) => {
const query = {};
const cursor = orderCollection.find(query);
const orders = await cursor.toArray();
res.send(orders);
});
// delete a specific order finding by id
app.delete("/orders/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await orderCollection.deleteOne(query);
res.send(result);
});
// find a specific order by id
app.get("/orders/:id", verifyJWT, async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await orderCollection.findOne(query);
res.send(result);
});
// delete a specific product finding by id
app.delete("/tool/:id", verifyJWT, verifyAdmin, async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await toolCollection.deleteOne(query);
res.send(result);
});
} finally {
// await client.close();
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("Hello from metalik server");
});
app.listen(port, () => {
console.log(`Metalik app listening on port ${port}`);
});