user.js 969 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const User = require('./schemas/user');
  2. const findByNumber = async (number) => {
  3. return await User.findOne({ number });
  4. };
  5. const findById = async (id) => {
  6. return await User.findById(id);
  7. };
  8. const createUser = async (body) => {
  9. const user = new User({ ...body });
  10. return await user.save();
  11. };
  12. const updateCode = async (id, code) => {
  13. return await User.updateOne({ _id: id }, { code });
  14. };
  15. const updateToken = async (id, token) => {
  16. return await User.updateOne({ _id: id }, { token });
  17. };
  18. const updateUser = async (id, obj) => {
  19. return await User.updateOne({ _id: id }, { ...obj });
  20. };
  21. const updateCredentials = async (id, body) => {
  22. return await User.findByIdAndUpdate({ _id: id }, { ...body }, { new: true });
  23. };
  24. const updateAvatar = async (id, avatarUrl) => {
  25. return await User.updateOne({ _id: id }, { avatarUrl });
  26. };
  27. module.exports = {
  28. findByNumber,
  29. createUser,
  30. updateCode,
  31. updateToken,
  32. updateUser,
  33. updateCredentials,
  34. updateAvatar,
  35. findById,
  36. };