You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const User = require('../../../models/user');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. async function handler(req, res) {
  4. const { method } = req;
  5. await dbConnect();
  6. switch (method) {
  7. case 'PATCH': {
  8. console.log(req.body);
  9. const updates = Object.keys(req.body.userData);
  10. const allowedUpdates = [
  11. 'fullName',
  12. 'email',
  13. 'address',
  14. 'address2',
  15. 'city',
  16. 'country',
  17. 'postcode',
  18. ];
  19. const isValidOperation = updates.every((update) =>
  20. allowedUpdates.includes(update)
  21. );
  22. if (!isValidOperation) {
  23. return res.status(400).send({ error: 'Invalid updates!' });
  24. }
  25. try {
  26. const user = await User.findOne({ _id: req.body._id });
  27. updates.forEach((update) => (user[update] = req.body.userData[update]));
  28. await user.save();
  29. res.send({
  30. user,
  31. message: 'User profile updated successfully.',
  32. });
  33. } catch (error) {
  34. res.status(400).json({ message: error.message });
  35. }
  36. break;
  37. }
  38. }
  39. }
  40. export default handler;