Spaces:
Sleeping
Sleeping
Mohamed Abu Basith
commited on
Commit
·
b00ce4a
1
Parent(s):
5bdae11
Please work
Browse files- routes/profileUpdate.js +69 -23
routes/profileUpdate.js
CHANGED
@@ -93,40 +93,86 @@ const asyncHandler = fn => (req, res, next) =>
|
|
93 |
// };
|
94 |
|
95 |
// Create address (add to user's address array)
|
96 |
-
|
97 |
const userId = parseInt(req.params.userId);
|
98 |
-
|
|
|
99 |
if (isNaN(userId)) {
|
100 |
-
return res.status(400).json(
|
|
|
|
|
|
|
|
|
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 |
-
res.status(201).json(successResponse("Address created successfully", newAddress));
|
129 |
-
}));
|
130 |
|
131 |
// Get addresses by user
|
132 |
router.get("/:userId/addresses", asyncHandler(async (req, res) => {
|
|
|
93 |
// };
|
94 |
|
95 |
// Create address (add to user's address array)
|
96 |
+
const updateUserAddress = async (req, res) => {
|
97 |
const userId = parseInt(req.params.userId);
|
98 |
+
|
99 |
+
// Validate userId
|
100 |
if (isNaN(userId)) {
|
101 |
+
return res.status(400).json({
|
102 |
+
status: 400,
|
103 |
+
message: "Invalid user ID",
|
104 |
+
data: null
|
105 |
+
});
|
106 |
}
|
107 |
|
108 |
+
// Validate request body
|
109 |
+
const { name, mobileNumber, pinCode, address, area } = req.body;
|
110 |
+
if (!name || !mobileNumber || !pinCode || !address || !area) {
|
111 |
+
return res.status(400).json({
|
112 |
+
status: 400,
|
113 |
+
message: "Missing required fields in the address",
|
114 |
+
data: null
|
115 |
+
});
|
116 |
}
|
117 |
|
118 |
+
try {
|
119 |
+
// Find the user by unique_id and update the address array
|
120 |
+
const updatedUser = await User.findOneAndUpdate(
|
121 |
+
{ unique_id: userId }, // Filter by unique_id
|
122 |
+
{ $push: { address: req.body } }, // Push new address into the array
|
123 |
+
{ new: true, runValidators: true } // Return updated document and run schema validators
|
124 |
+
);
|
125 |
+
|
126 |
+
// If user not found, return 404
|
127 |
+
if (!updatedUser) {
|
128 |
+
return res.status(404).json({
|
129 |
+
status: 404,
|
130 |
+
message: "User not found",
|
131 |
+
data: null
|
132 |
+
});
|
133 |
+
}
|
134 |
|
135 |
+
// Extract the newly added address
|
136 |
+
const addressArray = updatedUser.address || [];
|
137 |
+
const newAddress = addressArray[addressArray.length - 1];
|
138 |
+
|
139 |
+
// If newAddress is missing, return 500
|
140 |
+
if (!newAddress) {
|
141 |
+
return res.status(500).json({
|
142 |
+
status: 500,
|
143 |
+
message: "Failed to retrieve new address",
|
144 |
+
data: null
|
145 |
+
});
|
146 |
+
}
|
147 |
|
148 |
+
// Return success response with the new address
|
149 |
+
res.status(201).json({
|
150 |
+
status: 201,
|
151 |
+
message: "Address created successfully",
|
152 |
+
data: newAddress
|
153 |
+
});
|
154 |
+
} catch (error) {
|
155 |
+
console.error("Error updating user address:", error);
|
156 |
+
|
157 |
+
// Handle specific MongoDB errors
|
158 |
+
if (error.name === "ValidationError") {
|
159 |
+
return res.status(400).json({
|
160 |
+
status: 400,
|
161 |
+
message: "Validation error: " + error.message,
|
162 |
+
data: null
|
163 |
+
});
|
164 |
+
}
|
165 |
|
166 |
+
// Generic server error
|
167 |
+
res.status(500).json({
|
168 |
+
status: 500,
|
169 |
+
message: "Internal server error",
|
170 |
+
data: null
|
171 |
+
});
|
172 |
}
|
173 |
+
};
|
174 |
+
|
175 |
|
|
|
|
|
176 |
|
177 |
// Get addresses by user
|
178 |
router.get("/:userId/addresses", asyncHandler(async (req, res) => {
|