Mohamed Abu Basith commited on
Commit
b5c0ed7
·
1 Parent(s): b377aba

CHG profile chnages

Browse files
Files changed (1) hide show
  1. routes/profileUpdate.js +27 -30
routes/profileUpdate.js CHANGED
@@ -54,55 +54,52 @@ const upload = multer({
54
  //Update profile and watch
55
  router.post('/', upload.single('file'), async (req, res) => {
56
  try {
57
- console.log("File", req.body);
58
- const formData = await req.body;
59
- var imageJSON = await JSON.parse(formData.file);
60
-
61
- console.log("userJSON", imageJSON);
62
-
63
-
64
  const user = await User.findOne({ unique_id: req.body.userId });
65
 
66
  if (!user) {
67
- return resres.status(404).send(failedResponse('User not found!'));
68
  }
69
 
70
- const { avatar, imageName, filePath } = imageJSON
71
- if (avatar && !avatar.includes("https://storage.googleapis.com")) {
72
- // Upload the file to Firebase Storage
73
- //await firebase.uploadFile(filePath, "ProfilePictures/" + imageName);
74
 
75
- // Generate a signed URL for the uploaded file
76
- const imageUrl = await firebase.uploadAvatar(filePath, req.body.userId);
 
 
 
 
 
 
 
77
 
78
  // Update profilePic only if imageUrl is not empty
79
  if (imageUrl) {
80
  user.profilePic = imageUrl;
81
  } else {
82
- user.profilePic = req.file.path;
83
  }
84
  }
85
 
86
- if (req.body.username) {
87
- user.username = req.body.username;
88
- }
89
- if (req.body.dateOfBirth) {
90
- user.dateOfBirth = req.body.dateOfBirth;
91
- }
92
-
93
- // Update email or mobile number based on the request body
94
- if (req.body.mobileNumber) {
95
- user.mobileNumber = req.body.mobileNumber;
96
- } else if (req.body.email) {
97
- user.email = req.body.email;
98
- }
99
 
 
100
  const savedData = await user.save();
101
  console.log('Profile updated successfully', savedData);
102
- res.status(200).send(successResponse('Profile updated!', savedData));
103
  } catch (error) {
104
  console.error('Error updating profile:', error);
105
- res.status(400).send(failedResponse(error));
106
  }
107
  });
108
 
 
54
  //Update profile and watch
55
  router.post('/', upload.single('file'), async (req, res) => {
56
  try {
57
+ console.log("File:", req.file); // Check the uploaded file
58
+ console.log("Body:", req.body); // Check other form data
59
+ let imageUrl = '';
60
+ // Find the user by userId
 
 
 
61
  const user = await User.findOne({ unique_id: req.body.userId });
62
 
63
  if (!user) {
64
+ return res.status(404).send({ message: 'User not found!' });
65
  }
66
 
67
+ // Handle file upload to Firebase Storage
68
+ if (req.file) {
69
+ const filePath = req.file.path; // Path to the uploaded file
70
+ const imageName = req.file.filename; // Name of the uploaded file
71
 
72
+ // Upload the file to Firebase Storage
73
+ await firebase.uploadFile(imagePath, "profile/" + imageName);
74
+ await firebase.generateSignedUrl("profile/" + imageName)
75
+ .then(url => {
76
+ imageUrl = url;
77
+ })
78
+ .catch(e => {
79
+ console.log(e);
80
+ });
81
 
82
  // Update profilePic only if imageUrl is not empty
83
  if (imageUrl) {
84
  user.profilePic = imageUrl;
85
  } else {
86
+ user.profilePic = req.file.path; // Fallback to local file path
87
  }
88
  }
89
 
90
+ // Update other user details
91
+ if (req.body.username) user.username = req.body.username;
92
+ if (req.body.dateOfBirth) user.dateOfBirth = req.body.dateOfBirth;
93
+ if (req.body.mobileNumber) user.mobileNumber = req.body.mobileNumber;
94
+ if (req.body.email) user.email = req.body.email;
 
 
 
 
 
 
 
 
95
 
96
+ // Save the updated user data
97
  const savedData = await user.save();
98
  console.log('Profile updated successfully', savedData);
99
+ res.status(200).send({ message: 'Profile updated!', data: savedData });
100
  } catch (error) {
101
  console.error('Error updating profile:', error);
102
+ res.status(400).send({ message: 'Error updating profile', error });
103
  }
104
  });
105