Spaces:
Sleeping
Sleeping
Mohamed Abu Basith
commited on
Commit
·
40c5a52
1
Parent(s):
3666337
CHG: Adress refactor
Browse files- Database/models/address.js +29 -38
- Database/models/user.js +4 -28
- routes/address.js +94 -92
Database/models/address.js
CHANGED
@@ -1,42 +1,33 @@
|
|
1 |
-
|
2 |
-
|
3 |
|
4 |
-
addressSchema = new Schema({
|
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 |
-
alterMobileNumber: {
|
33 |
-
type: String
|
34 |
-
}
|
35 |
-
|
36 |
-
}
|
37 |
-
]
|
38 |
-
},
|
39 |
-
{ timestamps: true }
|
40 |
-
);
|
41 |
|
42 |
module.exports = mongoose.model('Address', addressSchema);
|
|
|
1 |
+
const mongoose = require('mongoose');
|
2 |
+
const { Schema } = mongoose;
|
3 |
|
4 |
+
const addressSchema = new Schema({
|
5 |
+
user: {
|
6 |
+
type: Schema.Types.ObjectId,
|
7 |
+
ref: 'User',
|
8 |
+
required: true
|
9 |
},
|
10 |
+
name: {
|
11 |
+
type: String,
|
12 |
+
required: true
|
13 |
+
},
|
14 |
+
mobileNumber: {
|
15 |
+
type: String,
|
16 |
+
required: true
|
17 |
+
},
|
18 |
+
pinCode: {
|
19 |
+
type: String,
|
20 |
+
required: true
|
21 |
+
},
|
22 |
+
address: {
|
23 |
+
type: String,
|
24 |
+
required: true
|
25 |
+
},
|
26 |
+
area: String,
|
27 |
+
landMark: String,
|
28 |
+
alterMobileNumber: String
|
29 |
+
}, {
|
30 |
+
timestamps: true
|
31 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
module.exports = mongoose.model('Address', addressSchema);
|
Database/models/user.js
CHANGED
@@ -65,34 +65,10 @@ const userSchema = new Schema({
|
|
65 |
profilePic: {
|
66 |
type: String
|
67 |
},
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
},
|
73 |
-
userId: {
|
74 |
-
type: String
|
75 |
-
},
|
76 |
-
mobileNumber: {
|
77 |
-
type: String
|
78 |
-
},
|
79 |
-
pinCode: {
|
80 |
-
type: String
|
81 |
-
},
|
82 |
-
address: {
|
83 |
-
type: String
|
84 |
-
},
|
85 |
-
area: {
|
86 |
-
type: String
|
87 |
-
},
|
88 |
-
landMark: {
|
89 |
-
type: String
|
90 |
-
},
|
91 |
-
alterMobileNumber: {
|
92 |
-
type: String
|
93 |
-
}
|
94 |
-
}
|
95 |
-
]
|
96 |
}, {
|
97 |
timestamps: true // This adds `createdAt` and `updatedAt` fields
|
98 |
});
|
|
|
65 |
profilePic: {
|
66 |
type: String
|
67 |
},
|
68 |
+
addresses: [{
|
69 |
+
type: Schema.Types.ObjectId,
|
70 |
+
ref: 'Address'
|
71 |
+
}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
}, {
|
73 |
timestamps: true // This adds `createdAt` and `updatedAt` fields
|
74 |
});
|
routes/address.js
CHANGED
@@ -3,97 +3,99 @@ const router = express.Router();
|
|
3 |
const mongoose = require("mongoose");
|
4 |
const Address = require("../Database/models/address");
|
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 |
-
module.exports = router;
|
|
|
3 |
const mongoose = require("mongoose");
|
4 |
const Address = require("../Database/models/address");
|
5 |
|
6 |
+
// Response helpers
|
7 |
+
const response = {
|
8 |
+
success: (data, message = "Success") => ({
|
9 |
+
status: "success",
|
10 |
+
code: 200,
|
11 |
+
data,
|
12 |
+
message
|
13 |
+
}),
|
14 |
+
error: (message = "Error", code = 400) => ({
|
15 |
+
status: "error",
|
16 |
+
code,
|
17 |
+
message
|
18 |
+
})
|
19 |
+
};
|
20 |
+
|
21 |
+
// Middleware for async error handling
|
22 |
+
const asyncHandler = fn => (req, res, next) =>
|
23 |
+
Promise.resolve(fn(req, res, next)).catch(next);
|
24 |
+
|
25 |
+
// Validation middleware
|
26 |
+
const validateAddressData = (req, res, next) => {
|
27 |
+
const requiredFields = ['user', 'name', 'mobileNumber', 'pinCode', 'address'];
|
28 |
+
const missingFields = requiredFields.filter(field => !req.body[field]);
|
29 |
+
|
30 |
+
if (missingFields.length > 0) {
|
31 |
+
return res.status(400).json(response.error(
|
32 |
+
`Missing required fields: ${missingFields.join(', ')}`
|
33 |
+
));
|
34 |
+
}
|
35 |
+
next();
|
36 |
+
};
|
37 |
+
|
38 |
+
// Create address
|
39 |
+
router.post("/", validateAddressData, asyncHandler(async (req, res) => {
|
40 |
+
const newAddress = new Address(req.body);
|
41 |
+
const savedAddress = await newAddress.save();
|
42 |
+
res.status(201).json(response.success(savedAddress, "Address created successfully"));
|
43 |
+
}));
|
44 |
+
|
45 |
+
// Get addresses by user
|
46 |
+
router.get("/user/:userId", asyncHandler(async (req, res) => {
|
47 |
+
if (!mongoose.Types.ObjectId.isValid(req.params.userId)) {
|
48 |
+
return res.status(400).json(response.error("Invalid user ID"));
|
49 |
+
}
|
50 |
+
|
51 |
+
const addresses = await Address.find({ user: req.params.userId });
|
52 |
+
if (!addresses.length) {
|
53 |
+
return res.status(404).json(response.error("No addresses found", 404));
|
54 |
+
}
|
55 |
+
res.json(response.success(addresses));
|
56 |
+
}));
|
57 |
+
|
58 |
+
// Get all addresses
|
59 |
+
router.get("/", asyncHandler(async (req, res) => {
|
60 |
+
const addresses = await Address.find().populate('user', 'username email');
|
61 |
+
res.json(response.success(addresses));
|
62 |
+
}));
|
63 |
+
|
64 |
+
// Update address
|
65 |
+
router.put("/:id", validateAddressData, asyncHandler(async (req, res) => {
|
66 |
+
if (!mongoose.Types.ObjectId.isValid(req.params.id)) {
|
67 |
+
return res.status(400).json(response.error("Invalid address ID"));
|
68 |
+
}
|
69 |
+
|
70 |
+
const updatedAddress = await Address.findByIdAndUpdate(
|
71 |
+
req.params.id,
|
72 |
+
req.body,
|
73 |
+
{ new: true, runValidators: true }
|
74 |
+
);
|
75 |
+
|
76 |
+
if (!updatedAddress) {
|
77 |
+
return res.status(404).json(response.error("Address not found", 404));
|
78 |
+
}
|
79 |
+
res.json(response.success(updatedAddress, "Address updated successfully"));
|
80 |
+
}));
|
81 |
+
|
82 |
+
// Delete address
|
83 |
+
router.delete("/:id", asyncHandler(async (req, res) => {
|
84 |
+
if (!mongoose.Types.ObjectId.isValid(req.params.id)) {
|
85 |
+
return res.status(400).json(response.error("Invalid address ID"));
|
86 |
+
}
|
87 |
+
|
88 |
+
const deletedAddress = await Address.findByIdAndDelete(req.params.id);
|
89 |
+
if (!deletedAddress) {
|
90 |
+
return res.status(404).json(response.error("Address not found", 404));
|
91 |
+
}
|
92 |
+
res.json(response.success(null, "Address deleted successfully"));
|
93 |
+
}));
|
94 |
+
|
95 |
+
// Error handling middleware
|
96 |
+
router.use((err, req, res, next) => {
|
97 |
+
console.error(err.stack);
|
98 |
+
res.status(500).json(response.error("Internal server error", 500));
|
99 |
});
|
100 |
|
101 |
+
module.exports = router;
|