-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
485 lines (361 loc) · 13.6 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
const express = require('express');
const cors = require('cors');
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
require('dotenv').config();
const jwt = require('jsonwebtoken');
const port = process.env.PORT || 5000;
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const app = express();
// middleware
app.use(cors());
app.use(express.json());
console.log(process.env.DB_USER);
console.log(process.env.DB_PASS);
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.jjkmnej.mongodb.net/?retryWrites=true&w=majority`;
console.log(uri);
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('unauthorized access');
// }
// const token = authHeader.split(' ')[1];
// jwt.verify(token, process.env.ACCESS_TOKEN, function (err, decoded) {
// if (err) {
// return res.status(403).send({ message: 'forbidden access' })
// }
// req.decoded = decoded;
// next();
// })
// }
async function run() {
try {
const userCollection = client.db("SellCell").collection("user");
const productCollection = client.db("SellCell").collection("product");
const categoryCollection = client.db("SellCell").collection("category");
const bookedProductCollection = client.db("SellCell").collection("booked");
const reportProductCollection = client.db("SellCell").collection("report");
const paymentsCollection = client.db("SellCell").collection("payments");
// const advertiseCollection = client.db('SellCell').collection('advertise');
/* User */
// Post a User
app.post("/user", async (req, res) => {
const user = req.body;
const result = await userCollection.insertOne(user);
res.send(result);
});
// Update a User
app.put("/user", async (req, res) => {
const email = req.body.email;
const data = req.body;
const query = { email: email };
const options = { upsert: true };
const updateDoc = {
$set: {
name: data.name,
email: data.email,
photoURL: data.photoURL,
role: data.role,
verify: data.verify,
},
};
const result = await userCollection.updateOne(query, updateDoc, options);
res.send(result);
});
// Get All User
// http://localhost:5000/user
app.get("/user", async (req, res) => {
const query = {};
const users = await userCollection.find(query).toArray();
res.send(users);
});
// delete a user
app.delete("/user/:id", async (req, res) => {
const id = req.params.id;
// console.log(id);
const query = { _id: ObjectId(id) };
const result = await userCollection.deleteOne(query);
res.send(result);
});
// Get User Role
// http://localhost:5000/user/[email protected]
app.get("/user/seller/:email", async (req, res) => {
const email = req.params.email;
// console.log(email);
const query = { email };
const user = await userCollection.findOne(query);
// console.log(user);
res.send({ isSeller: user?.role === 'Seller' });
});
// Get Admin Role
app.get("/user/admin/:email", async (req, res) => {
const email = req.params.email;
// console.log(email);
const query = { email };
const user = await userCollection.findOne(query);
// console.log(user);
res.send({ isAdmin: user?.role === 'Admin' });
});
/* */
// err
// Get seller or buyer
app.get("/allUsers/:role", async (req, res) => {
const role = req.params.role;
const query = { role: role };
const result = await userCollection.find(query).toArray();
res.send(result);
});
/* */
// Get User Verified
app.put("/user/verify/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: ObjectId(id) };
const options = { upsert: true };
const updatedDoc = {
$set: {
verify: "true",
},
};
const result = await userCollection.updateOne(
filter,
updatedDoc,
options
);
res.send(result);
});
/* Category */
// Get All Category
// http://localhost:5000/category
app.get("/category", async (req, res) => {
const query = {};
const result = await categoryCollection.find(query).toArray();
res.send(result);
});
// http://localhost:5000/brand?Brand=Nokia
// app.get("/brand", async (req, res) => {
// const Brand = req.query.Brand;
// console.log(Brand);
// const query = { Brand };
// const result = await productCollection.find(query).toArray();
// res.send(result);
// });
// Get a category collection
app.get("/category/:category_name", async (req, res) => {
const category_name = req.params.category_name;
// console.log(category_name);
const query = { category_name: category_name };
const result = await productCollection.find(query).toArray();
// console.log(result);
res.send(result);
});
/* Product */
// Post a product
app.post("/products", async (req, res) => {
const product = req.body;
// console.log(product);
const result = await productCollection.insertOne(product);
res.send(result);
});
// Get All Products
app.get("/products", async (req, res) => {
const query = {};
const result = await productCollection.find(query).toArray();
res.send(result);
});
// get my Product
// app.get("/dashboard/myProduct", async (req, res) => {
// const email = req.query.email;
// console.log(email);
// const query = { email };
// const result = await productCollection.find(query).toArray();
// res.send(result);
// });
// err
// Get My product
// http://localhost:5000/dashboard/[email protected]
app.get("/dashboard/myProduct", async (req, res) => {
const email = req.query.email;
const query = { userEmail: email };
// console.log(query);
const result = await productCollection.find(query).toArray();
res.send(result);
});
// app.delete("/products/:id", async (req, res) => {
// const id = req.params.id;
// const query = { _id: ObjectId(id) };
// const service = await productCollection.deleteOne(query);
// res.send(service);
// });
// Delete a product
app.delete("/products/:id", async (req, res) => {
const id = req.params.id;
// console.log(id);
const query = { _id: ObjectId(id) };
const result = await productCollection.deleteOne(query);
res.send(result);
});
// Add Booked Product
app.post('/bookProduct', async (req, res) => {
const product = req.body;
// console.log(product);
const query = {
productId: product.productId,
email: product.email
}
const alreadyBooked = await bookedProductCollection.find(query).toArray();
if (alreadyBooked.length) {
const message = `You already have a booking for ${product.productName}`
return res.send({ acknowledged: false, message })
}
const result = await bookedProductCollection.insertOne(product);
res.send(result);
});
/* orders */
/* My Orders */
app.get("/orders", async (req, res) => {
const email = req.query.email;
const query = { email: email };
const bookings = await bookedProductCollection.find(query).toArray();
res.send(bookings);
});
// Delete a Order
app.delete("/orders/:id", async (req, res) => {
const id = req.params.id;
// console.log(id);
const query = { _id: ObjectId(id) };
const result = await bookedProductCollection.deleteOne(query);
res.send(result);
});
/* Report */
// Add Report Product
app.post("/reports", async (req, res) => {
const product = req.body;
// console.log(product);
const result = await reportProductCollection.insertOne(product);
res.send(result);
});
// Get Report
app.get("/reports", async (req, res) => {
const query = {};
const result = await reportProductCollection.find(query).toArray();
res.send(result);
});
// Delete A Report
app.delete("/reports/:id", async (req, res) => {
const id = req.params.id;
// console.log(id);
const query = { _id: ObjectId(id) };
const result = await reportProductCollection.deleteOne(query);
res.send(result);
});
// /* */
// Payments Orders
app.get("/orders/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const orders = await bookedProductCollection.findOne(query);
res.send(orders);
});
// create-payment-intent
app.post("/create-payment-intent", async (req, res) => {
const orders = req.body;
const price = orders.resalePrice;
const amount = price * 100;
const paymentIntent = await stripe.paymentIntents.create({
currency: "usd",
amount: amount,
payment_method_types: ["card"],
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
//payments
app.post("/payments", async (req, res) => {
const payment = req.body;
const result = await paymentsCollection.insertOne(payment);
const id = payment.bookingId;
const filter = { _id: ObjectId(id) };
const updatedDoc = {
$set: {
paid: true,
transactionId: payment.transactionId,
},
};
const updatedResult = await bookedProductCollection.updateOne(
filter,
updatedDoc
);
res.send(result);
});
// /* */
/* */
// advertise
app.get('/myAdvertise/:advertise', async (req, res) => {
const advertise = req.params.advertise;
const query = { advertise: advertise };
const data = productCollection.find(query)
const result = await data.toArray()
res.send(result);
});
// update ad
app.put('/myAdvertise/:id', async (req, res) => {
const id = req.params.id;
const filter = { _id: ObjectId(id) }
const options = { upsert: true };
const updatedDoc = {
$set: {
advertise: 'true'
}
}
const result = await productCollection.updateOne(filter, updatedDoc, options);
res.send(result);
});
// remove ad
app.put("/myAdvertiseRemove/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: ObjectId(id) };
const options = { upsert: true };
const updatedDoc = {
$set: {
advertise: "false",
},
};
const result = await productCollection.updateOne(
filter,
updatedDoc,
options
);
// console.log("remove");
res.send(result);
});
// sold product
// Product sold status set
app.put("/soldProduct/sold/:id", async (req, res) => {
const id = req.params.id;
console.log(id);
const filter = { _id: ObjectId(id) };
const option = { upsert: true };
const updatedDoc = {
$set: {
// report: true,
sold: true,
advertise: 'false',
},
};
const result = await productCollection.updateOne(
filter,
updatedDoc,
option
);
res.send(result);
});
}
finally {
}
}
run().catch((err) => console.error(err));
app.get('/', async (req, res) => {
res.send('ast-12-sellcell server is running');
})
app.listen(port, () => console.log(`ast-12-sellcell running on ${port}`))