├── .gitignore ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "assignment-12-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "start-dev": "nodemon index.js", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "keywords": [], 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "cors": "^2.8.5", 16 | "date-fns": "^2.28.0", 17 | "dotenv": "^16.0.1", 18 | "express": "^4.18.1", 19 | "jsonwebtoken": "^8.5.1", 20 | "mongodb": "^4.6.0", 21 | "stripe": "^9.4.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const cors = require('cors'); 3 | const jwt = require('jsonwebtoken'); 4 | require('dotenv').config(); 5 | const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb'); 6 | const app = express(); 7 | const port = process.env.PORT || 5000; 8 | const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); 9 | 10 | // middleware 11 | app.use(cors()); 12 | app.use(express.json()); 13 | 14 | 15 | 16 | 17 | 18 | const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.fl8bx.mongodb.net/?retryWrites=true&w=majority`; 19 | const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 }); 20 | 21 | // veryfy Token 22 | function verifyJWT(req, res, next) { 23 | const authHeader = req.headers.authorization; 24 | if (!authHeader) { 25 | return res.status(401).send({ message: 'UnAuthorized Access' }); 26 | } 27 | const token = authHeader.split(' ')[1]; 28 | jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, function (err, decoded) { 29 | if (err) { 30 | return res.status(403).send({ message: 'Forbidden Access' }) 31 | } 32 | req.decoded = decoded; 33 | next(); 34 | }); 35 | 36 | } 37 | 38 | // Async Function 39 | async function run() { 40 | try { 41 | await client.connect(); 42 | const itemsCollection = client.db('rapid_manufacturer').collection('items'); 43 | const purchaseCollection = client.db('rapid_manufacturer').collection('purchases'); 44 | const reviewsCollection = client.db('rapid_manufacturer').collection('reviews'); 45 | const userCollection = client.db('rapid_manufacturer').collection('users'); 46 | const paymentCollection = client.db('rapid_manufacturer').collection('payments'); 47 | const productCollection = client.db('rapid_manufacturer').collection('products'); 48 | 49 | // To load all items to the client site 50 | app.get('/items', async (req, res) => { 51 | const query = {}; 52 | const cursor = itemsCollection.find(query); 53 | const items = await cursor.toArray(); 54 | res.send(items); 55 | }) 56 | 57 | 58 | // using post API to purchase a particular item 59 | app.post('/purchase', async (req, res) => { 60 | const purchase = req.body; 61 | const result = purchaseCollection.insertOne(purchase); 62 | res.send(result); 63 | 64 | }) 65 | 66 | app.get('/purchases', async (req, res) => { 67 | const query = {}; 68 | const result = await purchaseCollection.find(query).toArray(); 69 | res.send(result); 70 | 71 | }) 72 | 73 | 74 | app.patch('/purchase/:id', async (req, res) => { 75 | const id = req.params.id; 76 | const filter = { _id: ObjectId(id) }; 77 | const updatedDoc = { 78 | $set: { 79 | shipped: true 80 | } 81 | } 82 | const updatedPurchase = await purchaseCollection.updateOne(filter, updatedDoc); 83 | res.send(updatedPurchase); 84 | }) 85 | 86 | 87 | app.get('/user', async (req, res) => { 88 | const users = await userCollection.find().toArray(); 89 | res.send(users); 90 | }) 91 | 92 | app.get('/user/:email', async (req, res) => { 93 | const email = req.params.email; 94 | const user = await userCollection.findOne({ email: email }); 95 | res.send(user); 96 | }) 97 | 98 | 99 | 100 | app.delete('/user/:email', async (req, res) => { 101 | const email = req.params.email; 102 | const query = { email: email } 103 | const users = await userCollection.deleteOne(query); 104 | res.send(users); 105 | }) 106 | 107 | app.put('/user/admin/:email', async (req, res) => { 108 | const email = req.params.email; 109 | const filter = { email: email }; 110 | const updateDoc = { 111 | $set: { role: 'admin' }, 112 | }; 113 | const result = await userCollection.updateOne(filter, updateDoc); 114 | 115 | res.send({ result }); 116 | }) 117 | 118 | 119 | 120 | 121 | app.get('/admin/:email', async (req, res) => { 122 | const email = req.params.email; 123 | const user = await userCollection.findOne({ email: email }); 124 | const isAdmin = user.role === 'admin'; 125 | res.send({ admin: isAdmin }); 126 | }) 127 | 128 | 129 | app.put('/user/:email', async (req, res) => { 130 | const email = req.params.email; 131 | const user = req.body; 132 | const filter = { email: email }; 133 | const options = { upsert: true }; 134 | const updateDoc = { 135 | $set: user, 136 | }; 137 | const result = await userCollection.updateOne(filter, updateDoc, options); 138 | const token = jwt.sign({ email: email }, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1h' }) 139 | res.send({ result, token }); 140 | }) 141 | 142 | 143 | app.patch('/updateUser/:email', async (req, res) => { 144 | const email = req.params.email; 145 | const userData = req.body; 146 | const filter = { email: email }; 147 | const updateDoc = { 148 | $set: userData, 149 | }; 150 | const result = await userCollection.updateOne(filter, updateDoc); 151 | res.send(result); 152 | }) 153 | 154 | 155 | 156 | app.get('/item/:id', async (req, res) => { 157 | const id = req.params.id; 158 | const query = { _id: ObjectId(id) }; 159 | const users = await itemsCollection.findOne(query); 160 | res.send(users); 161 | }) 162 | 163 | 164 | 165 | 166 | app.get('/purchase/:email', async (req, res) => { 167 | const email = req.params.email; 168 | 169 | const query = { userEmail: email }; 170 | const cursor = purchaseCollection.find(query); 171 | const purchase = await cursor.toArray(); 172 | res.send(purchase); 173 | }) 174 | 175 | 176 | 177 | app.delete('/purchase/:id', async (req, res) => { 178 | const id = req.params.id; 179 | const query = { _id: ObjectId(id) }; 180 | const purchase = await purchaseCollection.deleteOne(query); 181 | res.send(purchase); 182 | }) 183 | 184 | 185 | app.post('/review', async (req, res) => { 186 | const review = req.body; 187 | const result = reviewsCollection.insertOne(review); 188 | res.send(result); 189 | 190 | }) 191 | 192 | 193 | app.get('/reviews', async (req, res) => { 194 | const query = {}; 195 | const cursor = reviewsCollection.find(query); 196 | const reviews = await cursor.toArray(); 197 | res.send(reviews); 198 | }) 199 | 200 | 201 | 202 | // for payment 203 | app.post('/create-payment-intent', async (req, res) => { 204 | const service = req.body; 205 | const price = service.price; 206 | const newPrice = price || 1 207 | const amount = parseInt(newPrice) * 100; 208 | const paymentIntent = await stripe.paymentIntents.create({ 209 | amount: amount, 210 | currency: 'usd', 211 | payment_method_types: ['card'] 212 | }); 213 | res.send({ clientSecret: paymentIntent.client_secret }) 214 | }) 215 | 216 | 217 | 218 | app.patch('/payment/:id', async (req, res) => { 219 | const id = req.params.id; 220 | const payment = req.body; 221 | const filter = { _id: ObjectId(id) }; 222 | const updatedDoc = { 223 | $set: { 224 | paid: true, 225 | transactionId: payment.transactionId 226 | } 227 | } 228 | const result = await paymentCollection.insertOne(payment); 229 | const updatedPayment = await purchaseCollection.updateOne(filter, updatedDoc); 230 | res.send(updatedPayment); 231 | }) 232 | 233 | 234 | 235 | app.get('/buy/:id', async (req, res) => { 236 | const id = req.params.id; 237 | console.log(id); 238 | const query = { _id: ObjectId(id) }; 239 | const result = await purchaseCollection.findOne(query); 240 | res.send(result); 241 | }) 242 | 243 | 244 | app.post('/product', async (req, res) => { 245 | const product = req.body; 246 | const result = await itemsCollection.insertOne(product); 247 | res.send(result); 248 | 249 | }) 250 | 251 | 252 | app.delete('/item/:id', async (req, res) => { 253 | const id = req.params.id; 254 | const query = { _id: ObjectId(id) }; 255 | const deleteResult = await itemsCollection.deleteOne(query); 256 | res.send(deleteResult); 257 | }) 258 | 259 | 260 | } 261 | finally { 262 | // await client.close(); 263 | } 264 | } 265 | 266 | run().catch(console.dir); 267 | 268 | 269 | 270 | 271 | 272 | 273 | app.get('/', (req, res) => { 274 | res.send('Hello from Rapid Manufacturer!') 275 | }) 276 | 277 | app.listen(port, () => { 278 | console.log(`Alhamdulilah Server is Running`) 279 | }) 280 | 281 | //${port} --------------------------------------------------------------------------------