user.js 841 B

12345678910111213141516171819202122232425262728293031323334
  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 updateName = async (id, name) => {
  19. return await User.findByIdAndUpdate({ _id: id }, { name }, { new: true });
  20. };
  21. const updateAvatar = async (id, avatarUrl) => {
  22. return await User.updateOne({ _id: id }, { avatarUrl });
  23. };
  24. module.exports = {
  25. findByNumber,
  26. createUser,
  27. updateCode,
  28. updateToken,
  29. updateName,
  30. updateAvatar,
  31. findById,
  32. };