All files / src/models user.model.js

87.5% Statements 7/8
100% Branches 0/0
66.66% Functions 2/3
87.5% Lines 7/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 774x   4x   4x                             63x 63x                                                                                                         4x   4x  
const { Schema, model, models } = require('mongoose');
 
const emailRegex = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/;
 
const userSchema = new Schema(
  {
    fullName: {
      type: String,
      required: true,
      minlength: 6,
      maxlength: 40,
    },
    email: {
      type: String,
      required: true,
      match: [emailRegex, 'Invalid email'],
      validate: [
        {
          validator(value) {
            return models.User.findOne({ email: value })
              .then((user) => !user)
              .catch(() => error);
          },
          message: 'There is already a user with that email',
        },
      ],
    },
    password: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      maxlength: 150,
      default: '',
    },
    phone: {
      type: String,
      maxlength: 20,
      default: '',
    },
    occupation: {
      type: String,
      maxlength: 40,
      default: '',
    },
    state: {
      type: String,
      default: 'disable',
    },
    image: {
      type: String,
      default:
        'https://ceslava.s3-accelerate.amazonaws.com/2016/04/mistery-man-gravatar-wordpress-avatar-persona-misteriosa.png',
    },
    channels: {
      type: [{ type: Schema.Types.ObjectId, ref: 'Channel' }],
      required: false,
    },
    workSpaceId: {
      type: [{ type: Schema.Types.ObjectId, ref: 'WorkSpace' }],
      required: [true, 'WorkSpace is required'],
    },
    premium: {
      type: Boolean,
      default: false,
    }
  },
  {
    timestamps: true,
  }
);
 
const User = model('User', userSchema);
 
module.exports = User;