mixassio/js-cinema

View on GitHub
server/models/user.js

Summary

Maintainability
A
0 mins
Test Coverage
import { encrypt } from '../../lib/secure';

export default (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    firstName: {
      type: DataTypes.STRING,
      validate: {
        notEmpty: {
          msg: 'First name can not be empty',
        },
      },
    },
    lastName: {
      type: DataTypes.STRING,
      validate: {
        notEmpty: {
          msg: 'Last name can not be empty',
        },
      },
    },
    email: {
      type: DataTypes.STRING,
      unique: {
        msg: 'Email already exist',
      },
      validate: {
        isEmail: {
          msg: 'Email is wrong',
        },
      },
    },
    passwordDigest: {
      type: DataTypes.STRING,
      validate: {
        notEmpty: true,
      },
    },
    password: {
      type: DataTypes.VIRTUAL,
      set: function set(value) {
        this.setDataValue('passwordDigest', encrypt(value));
        this.setDataValue('password', value);
        return value;
      },
      validate: {
        len: [1, +Infinity],
      },
    },
  }, {
    getterMethods: {
      fullName() {
        return `${this.firstName} ${this.lastName} / ${this.email}`;
      },
    },
  });
  return User;
};