Advertisement
Guest User

Untitled

a guest
Dec 27th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by Dmitriy Krishtal on 02.08.2017.
  3.  */
  4.  
  5. const mongoose = require('mongoose');
  6. const argon2 = require('argon2');
  7. const fake = require('faker');
  8. const valid = require('validator');
  9. const ObjectId = mongoose.Schema.Types.ObjectId;
  10.  
  11.  
  12. let user = new mongoose.Schema({
  13.   password: {
  14.     type: String,
  15.     required: true,
  16.     minlength: 8,
  17.     maxlength: 100
  18.   },
  19.   role: {
  20.     type: String,
  21.     default: 'User',
  22.     enum: ['User', 'Admin']
  23.   },
  24.   status: {
  25.     type: String,
  26.     default: 'new',
  27.     enum: ['banned', 'active', 'new', 'deleted']
  28.   },
  29.   online: {
  30.     type: Boolean,
  31.     default: false
  32.   },
  33.   email: {
  34.     type: String,
  35.     required: true,
  36.     minlength: 6,
  37.     maxlength: 50,
  38.     validate: { validator: valid.isEmail, message: 'Please fill a valid email address' }
  39.   },
  40.   email_confirmed: {
  41.     type: Boolean,
  42.     default: false
  43.   },
  44.   fullname: {
  45.     type: String,
  46.     required: true,
  47.     maxlength: 50,
  48.     validate: { validator: valid.isAscii, message: 'Please fill a valid full name' }
  49.   },
  50.   fullname_confirmed: {
  51.     type: Boolean,
  52.     default: false
  53.   },
  54.   country: {
  55.     type: String,
  56.     require: false,
  57.     validate: { validator: valid.isISO31661Alpha2, message: 'Please fill a valid country ISO31661Alpha2 code' }
  58.   },
  59.   company: {
  60.     type: String,
  61.     require: false,
  62.     validate: { validator: valid.isAscii, message: 'Please fill a valid company  name' }
  63.   },
  64.   website: {
  65.     type: String,
  66.     require: false,
  67.     validate: { validator: valid.isURL, message: 'Please fill a valid website' }
  68.   },
  69.   signup_date: {
  70.     type: Number,
  71.     default: null
  72.   },
  73.   last_visit: { //?? virtual, get last from user_visit_model
  74.     type: Number,
  75.     default: null
  76.   },
  77.   comment: {
  78.     type: String,
  79.     default: null
  80.   },
  81.   refer_id: {
  82.     type: ObjectId,
  83.     default: null
  84.   }
  85. }, { toJSON: { virtuals: true } });
  86.  
  87. user.virtual('refer', {
  88.   ref: 'userModel',
  89.   localField: 'refer_id',
  90.   foreignField: '_id',
  91.   justOne: true
  92. });
  93.  
  94. user.pre('save', function(next) {
  95.   let timestamp = +new Date();
  96.  
  97.   this.signup_date = timestamp;
  98.   this.last_visit = timestamp;
  99.  
  100.   argon2.hash(this.password).then((hash) => {
  101.     this.password = hash;
  102.     next();
  103.   });
  104. });
  105.  
  106. user.pre('update', function(next) {
  107.   if (this.getUpdate().$set.password) {
  108.     argon2.hash(this.getUpdate().$set.password).then((hash) => {
  109.       this.update({password: hash});
  110.       next();
  111.     });
  112.   } else {
  113.     next();
  114.   }
  115. });
  116.  
  117. user.methods.verify = function(email, password) {
  118.   if (email !== this.email) {
  119.     return false;
  120.   }
  121.   return argon2.verify(this.password, password).then((match) => {
  122.     return match;
  123.   });
  124. };
  125.  
  126. user.methods.setRandomPassword = function() {
  127.   this.password = fake.internet.password();
  128. };
  129.  
  130. user.methods.sendEmailWithPassword = function() {
  131.   //TODO ?
  132. };
  133.  
  134. user.statics.random = function(callback) {
  135.   this.count(function(err, count) {
  136.     if (err) {
  137.       return callback(err);
  138.     }
  139.     var rand = Math.floor(Math.random() * count);
  140.     this.findOne().skip(rand).exec(callback);
  141.   }.bind(this));
  142. };
  143.  
  144. module.exports = mongoose.model('userModel', user);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement