{"size": 989, "ext": "js", "max_stars_repo_path": "app/models/user.model.js", "max_stars_repo_name": "ypcloud/console-server", "max_stars_repo_stars_event_min_datetime": "2018-03-09T11:37:02.000Z", "max_stars_repo_stars_event_max_datetime": "2019-06-05T06:22:49.000Z", "max_issues_repo_path": "app/models/user.model.js", "max_issues_repo_name": "ypcloud/console-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/models/user.model.js", "max_forks_repo_name": "ypcloud/console-server", "max_forks_repo_forks_event_min_datetime": "2018-07-05T14:10:46.000Z", "max_forks_repo_forks_event_max_datetime": "2018-07-05T14:10:46.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 14.3333333333, "max_line_length": 52, "alphanum_fraction": 0.5267947422}
const mongoose = require('mongoose');

/**
 * User schema
 */
const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    default: ''
  },
  username: {
    type: String,
    index: true,
    unique: true,
    lowercase: true,
    trim: true,
    required: true
  },
  email: {
    type: String,
    lowercase: true,
    trim: true,
    default: ''
  },
  avatar: {
    type: String,
    default: ''
  },
  type: {
    type: String,
    lowercase: true
  },
  bitbucket: {
    token: String,
    tokenSecret: String,
  },
  isActive: {
    type: Boolean,
    default: false
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

/**
 * Statics
 */
UserSchema.statics = {

  findByUsername (username) {
    return this.findOne({ username })
      .then(user => {
        if (user) {
          return user;
        } else {
          throw new Error('Not found');
        }
      });
  }

};

/**
 * Register
 */
module.exports = mongoose.model('User', UserSchema);
