text-dataset-tiny-code-script-py-format
/
ysnrfd_messenger
/BACKUP4
/backend
/src
/controllers
/userController.js
| const User = require('../models/User'); | |
| exports.searchUsers = async (req, res) => { | |
| try { | |
| const { username, email } = req.query; | |
| if (!username && !email) { | |
| return res.status(400).json({ error: 'Username or email is required for search' }); | |
| } | |
| const searchQuery = {}; | |
| if (username) { | |
| searchQuery.username = { $regex: username, $options: 'i' }; | |
| } | |
| if (email) { | |
| searchQuery.email = { $regex: email, $options: 'i' }; | |
| } | |
| // Exclude current user from results | |
| searchQuery._id = { $ne: req.user.userId }; | |
| const users = await User.find(searchQuery) | |
| .select('username displayName avatar status lastSeen bio') | |
| .limit(20); | |
| res.json({ users }); | |
| } catch (error) { | |
| console.error('❌ User search error:', error); | |
| res.status(500).json({ error: 'Server error during user search' }); | |
| } | |
| }; | |
| exports.getUserProfile = async (req, res) => { | |
| try { | |
| const { userId } = req.params; | |
| const user = await User.findById(userId) | |
| .select('username displayName avatar status lastSeen bio createdAt'); | |
| if (!user) { | |
| return res.status(404).json({ error: 'User not found' }); | |
| } | |
| res.json({ user }); | |
| } catch (error) { | |
| console.error('❌ Get user profile error:', error); | |
| res.status(500).json({ error: 'Server error' }); | |
| } | |
| }; | |
| exports.updateSettings = async (req, res) => { | |
| try { | |
| const { theme, notifications } = req.body; | |
| const updateData = {}; | |
| if (theme) updateData['settings.theme'] = theme; | |
| if (notifications) { | |
| if (typeof notifications.enabled !== 'undefined') { | |
| updateData['settings.notifications.enabled'] = notifications.enabled; | |
| } | |
| if (typeof notifications.sound !== 'undefined') { | |
| updateData['settings.notifications.sound'] = notifications.sound; | |
| } | |
| if (typeof notifications.preview !== 'undefined') { | |
| updateData['settings.notifications.preview'] = notifications.preview; | |
| } | |
| } | |
| const user = await User.findByIdAndUpdate( | |
| req.user.userId, | |
| { $set: updateData }, | |
| { new: true, runValidators: true } | |
| ); | |
| if (!user) { | |
| return res.status(404).json({ error: 'User not found' }); | |
| } | |
| res.json({ | |
| message: 'Settings updated successfully', | |
| settings: user.settings | |
| }); | |
| } catch (error) { | |
| console.error('❌ Update settings error:', error); | |
| res.status(500).json({ error: 'Server error during settings update' }); | |
| } | |
| }; | |
| exports.deleteAccount = async (req, res) => { | |
| try { | |
| const { confirmation } = req.body; | |
| if (confirmation !== 'DELETE MY ACCOUNT') { | |
| return res.status(400).json({ | |
| error: 'Please type "DELETE MY ACCOUNT" to confirm account deletion' | |
| }); | |
| } | |
| await User.findByIdAndDelete(req.user.userId); | |
| res.json({ message: 'Account deleted successfully' }); | |
| } catch (error) { | |
| console.error('❌ Delete account error:', error); | |
| res.status(500).json({ error: 'Server error during account deletion' }); | |
| } | |
| }; |