Mohamed Abu Basith commited on
Commit
b00ce4a
·
1 Parent(s): 5bdae11

Please work

Browse files
Files changed (1) hide show
  1. 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
- router.post("/:userId/addresses", asyncHandler(async (req, res) => {
97
  const userId = parseInt(req.params.userId);
98
- console.log(" -----> " + userId)
 
99
  if (isNaN(userId)) {
100
- return res.status(400).json(failedResponse("Invalid user ID", 400));
 
 
 
 
101
  }
102
 
103
- const updatedUser = await User.findOneAndUpdate(
104
- { unique_id: userId },
105
- { $push: { address: req.body } },
106
- { new: true, runValidators: true }
107
- );
108
-
109
- if (!updatedUser) {
110
- return res.status(404).json(failedResponse("User not found", 404));
111
  }
112
 
113
- // Ensure the address array exists
114
- if (!updatedUser.address || !Array.isArray(updatedUser.address)) {
115
- return res.status(500).json(failedResponse("User address field is invalid", 500));
116
- }
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
- const addressArray = updatedUser.address || [];
119
- const newAddress = addressArray[addressArray.length - 1];
 
 
 
 
 
 
 
 
 
 
120
 
121
- console.log("Updated User:", updatedUser);
122
- console.log("New Address:", newAddress);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- if (!newAddress) {
125
- return res.status(500).json(failedResponse("Failed to retrieve new address", 500));
 
 
 
 
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) => {