Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
1.1 kB
2
Indexable
Never
const { User } = require('../models');
const { jsonToken } = require('../utils/tokenValidator');

const userService = {
  async checkUser(email) {
    return User.findOne({ where: { email } });
  },

  async createUser(userInfo) {
    return User.create({
      displayName: userInfo.displayName,
      email: userInfo.email,
      password: userInfo.password,
      image: userInfo.image,
    });
  },
  async insertUser(userInfo) {
    if (await this.checkUser(userInfo.email)) {
      return {
        error: 'User already registered',
      }; 
    }
    const insertedUser = await this.createUser(userInfo);
    const token = jsonToken(insertedUser.id);
    return { token };
  },
  async findAllUsers() {
    const findUsers = await User.findAll({
      attributes: { exclude: ['password'] },
    });
    return findUsers;
  },
  async findUserById(id) {
    const findUser = await User.findByPk(id, {
      attributes: ['id', 'displayName', 'email', 'image'],
    });
    if (!findUser) return { error: 'User does not exist' };
    return findUser;
  },
};

module.exports = userService;
Leave a Comment