-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
353 lines (300 loc) · 11.2 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
const express = require('express')
const app = express()
const cors = require('cors')
const port = process.env.PORT || 3000
const jwt = require('jsonwebtoken')
require('dotenv').config()
app.use(cors())
app.use(express.json())
//database connection setup
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.vybo3pc.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`;
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
// await client.connect();
// Send a ping to confirm a successful connection
const userCollection = client.db('delivery-service').collection('users')
const bookedParcelCollection = client.db('delivery-service').collection('booked-Parcel')
const reviewCollection = client.db('delivery-service').collection('reviews')
//post users to database upon login
app.post('/user', async(req,res)=>{
const data = req.body
console.log(req.body)
const result = await userCollection.insertOne(data)
res.send(result)
})
//get all the users
//make an user a delivery rider
app.patch("/user/deliveryMan/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const updatedDoc = {
$set: {
role: "delivery",
},
};
const result = await userCollection.updateOne(filter, updatedDoc);
res.send(result);
});
// make an user an admin
app.patch("/user/admin/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const updatedDoc = {
$set: {
role: "admin",
},
};
const result = await userCollection.updateOne(filter, updatedDoc);
res.send(result);
});
app.get('/user', async(req,res)=>{
const result = await userCollection.find().toArray()
res.send(result)
})
app.get('/user', async (req, res) => {
try {
const page = parseInt(req.query.page) || 1; // Current page number, default to 1 if not provided
const pageSize = parseInt(req.query.pageSize) || 5; // Number of users per page, default to 5 if not provided
const skip = (page - 1) * pageSize;
const users = await userCollection.find().skip(skip).limit(pageSize).toArray();
const totalUsers = await userCollection.countDocuments();
res.status(200).json({ users, totalUsers });
} catch (error) {
console.error('Error fetching users:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
//get users with role delivery ?
app.get('/user/delivery', async (req, res) => {
const result = await userCollection.find({ role: 'delivery' }).toArray();
res.send(result);
});
// get a user info by email
app.get('/user/:email', async (req, res) => {
const email = req.params.email;
try {
const result = await userCollection.findOne({ email });
if (result) {
res.send({ role: result.role }); // Assuming role is stored in result
} else {
res.status(404).send({ error: "User not found" });
}
} catch (error) {
console.error("Error fetching user:", error);
res.status(500).send({ error: "Internal server error" });
}
});
// user related api ----------------------------------
// booking a parcel
app.post('/parcelBookingData', async (req, res) => {
try {
const parcelBookingData = req.body;
parcelBookingData.requested_Delivery_Date = new Date(parcelBookingData.requested_Delivery_Date);
parcelBookingData.booking_Date = new Date(parcelBookingData.booking_Date);
const result = await bookedParcelCollection.insertOne(parcelBookingData);
res.status(201).json({ insertedId: result.insertedId });
} catch (error) {
console.error('Error occurred while booking parcel:', error);
res.status(500).json({ error: 'Failed to book your parcel. Please try again later.' });
}
});
//get parcels based on specific user email
app.get('/parcelBookingData', async (req, res) => {
const email = req.query.email;
const query = {
user_Email: email
};
const result = await bookedParcelCollection.find(query).toArray();
res.send(result);
});
app.get('/allBookedParcel', async(req,res) =>{
const data = await bookedParcelCollection.find().toArray()
res.send(data)
})
//get delivery riders orders based on id
app.get('/myParcels/:deliveryRiderId', async (req, res) => {
const deliveryRiderId = req.params.deliveryRiderId;
try {
const parcels = await bookedParcelCollection.find({ delivery_Rider_Id: deliveryRiderId }).toArray();
res.status(200).json(parcels);
} catch (error) {
console.error('Error fetching parcels:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// get specific parcel id
app.get('/parcelBookingData/:id', async(req,res) =>{
const id = req.params.id
const query = {
_id : new ObjectId(id)
}
const result = await bookedParcelCollection.findOne(query)
res.send(result)
})
//update and assign riders
app.get('/allParcels' , async(req,res) => {
const data = await bookedParcelCollection.find().toArray()
res.send(data)
})
app.patch('/allParcels/:id', async (req, res) => {
const id = req.params.id;
const data = req.body;
console.log(req.body, id);
try {
const filter = { _id: new ObjectId(id) };
const updatedDoc = {
$set: {
approximate_Delivery_Date: new Date(data.approximate_Delivery_Date), // Ensure this is a Date object
delivery_Rider_Id: data.delivery_Rider_Id,
status: data.status
}
};
const result = await bookedParcelCollection.updateOne(filter, updatedDoc);
res.send(result);
} catch (error) {
console.error('Error updating parcel:', error);
res.status(500).send('Internal Server Error');
}
});
// show parcel data based on date
app.get('/allParcelData', async (req, res) => {
try {
const { from, to } = req.query;
console.log(req.query)
let filter = {};
if (from && to) {
filter.requested_Delivery_Date = {
$gte: new Date(from),
$lte: new Date(to)
};
}
const parcels = await bookedParcelCollection.find(filter).toArray();
res.status(200).json(parcels);
} catch (error) {
console.error('Error fetching parcels:', error);
res.status(500).json({ error: error.message });
}
});
app.patch('/parcelBookingData/:id', async (req, res) => {
const id = req.params.id;
const data = req.body;
console.log("Data to be updated:", data);
const filter = { _id: new ObjectId(id) };
const updatedDoc = {
$set: {
user_Name: data.user_Name,
user_Email: data.user_Email,
user_Photo: data.user_Photo,
user_Phone_Number: data.user_Phone_Number,
requested_Delivery_Date: new Date(data.requested_Delivery_Date), // Ensure this is a Date object
price: data.price,
receiver_Name: data.receiver_Name,
receivers_Phone: data.receivers_Phone,
package_Type: data.package_Type,
location_Latitude: data.location_Latitude,
location_Longitude: data.location_Longitude,
delivery_Address: data.delivery_Address,
weight: data.weight,
status: data.status
}
};
console.log("Filter:", filter);
console.log("Updated Document:", updatedDoc);
try {
const result = await bookedParcelCollection.updateOne(filter, updatedDoc);
console.log("Update Result:", result);
res.send(result);
} catch (error) {
console.error("Error updating document:", error);
res.status(500).send({ message: "Error updating document", error });
}
});
app.patch('/parcelBookingData/:id/status', async (req, res) => {
const id = req.params.id;
const { status } = req.body;
console.log(req.body)
console.log(id)
try {
const filter = { _id: new ObjectId(id) };
const updateDoc = {
$set: {
status: status
}
};
const result = await bookedParcelCollection.updateOne(filter, updateDoc);
if (result.modifiedCount === 1) {
res.status(200).json({ message: 'Order status updated successfully' });
} else {
res.status(404).json({ message: 'Order not found' });
}
} catch (error) {
console.error('Error updating order status:', error);
res.status(500).json({ message: 'Internal server error' });
}
});
// give review
// Give review
app.post('/reviews', async (req, res) => {
const data = req.body;
const result = await reviewCollection.insertOne(data);
res.send(result);
});
//get all the reviews
app.get('/reviews' ,async(req,res) =>{
const data = await reviewCollection.find().toArray()
res.send(data)
})
// Get reviews based on specific delivery rider's id
app.get('/reviews/:deliveryRiderId', async (req, res) => {
const deliveryRiderId = req.params.deliveryRiderId;
try {
const parcels = await reviewCollection.find({ delivery_Rider_Id: deliveryRiderId }).toArray();
res.status(200).json(parcels);
} catch (error) {
console.error('Error fetching parcels:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.get('/parcels/delivered/:deliveryManId', async (req, res) => {
const deliveryManId = req.params.deliveryManId;
try {
const parcels = await bookedParcelCollection.find({ delivery_Rider_Id: deliveryManId, status: 'delivered' }).toArray();
res.status(200).json(parcels);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Fetch reviews for a specific delivery man
app.get('/reviews/:deliveryManId', async (req, res) => {
const deliveryManId = req.params.deliveryManId;
try {
const reviews = await reviewCollection.find({ delivery_Rider_Id: deliveryManId }).toArray();
res.status(200).json(reviews);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});
// await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
// await client.close();
}
}
run().catch(console.dir);
app.get('/',(req,res) => {
res.send('Server is running')
})
app.listen(port, () =>{
console.log('server is running on port' , port)
})