Example #1
0
UserSchema.pre('save', function(next) {

  //this.markModified('tasks');
  if (_.isNaN(this.preferences.dayStart) || this.preferences.dayStart < 0 || this.preferences.dayStart > 23) {
    this.preferences.dayStart = 0;
  }

  if (!this.profile.name) {
    var fb = this.auth.facebook;
    this.profile.name =
      (this.auth.local && this.auth.local.username) ||
      (fb && (fb.displayName || fb.name || fb.username || (fb.first_name && fb.first_name + ' ' + fb.last_name))) ||
      'Anonymous';
  }

  var petCount = shared.countPets(_.reduce(this.items.pets,function(m,v){
    //HOTFIX - Remove when solution is found, the first argument passed to reduce is a function
    if(_.isFunction(v)) return m;
    return m+(v?1:0)},0), this.items.pets);

  this.achievements.beastMaster = petCount >= 90;

  //our own version incrementer
  this._v++;
  next();
});
Example #2
0
UserSchema.pre('save', function(next) {

  // Populate new users with default content
  if (this.isNew){
    //TODO for some reason this doesn't work here: `_.merge(this, shared.content.userDefaults);`
    var self = this;
    _.each(['habits', 'dailys', 'todos', 'rewards', 'tags'], function(taskType){
      self[taskType] = _.map(shared.content.userDefaults[taskType], function(task){
        var newTask = _.cloneDeep(task);

        // Render task's text and notes in user's language
        if(taskType === 'tags'){
          // tasks automatically get id=helpers.uuid() from TaskSchema id.default, but tags are Schema.Types.Mixed - so we need to manually invoke here
          newTask.id = shared.uuid();
          newTask.name = newTask.name(self.preferences.language);
        }else{
          newTask.text = newTask.text(self.preferences.language);
          newTask.notes = newTask.notes(self.preferences.language);

          if(newTask.checklist){
            newTask.checklist = _.map(newTask.checklist, function(checklistItem){
              checklistItem.text = checklistItem.text(self.preferences.language);
              return checklistItem;
            });
          }
        }

        return newTask;
      });
    });

    this.preferences.language = undefined;
  }

  //this.markModified('tasks');
  if (_.isNaN(this.preferences.dayStart) || this.preferences.dayStart < 0 || this.preferences.dayStart > 23) {
    this.preferences.dayStart = 0;
  }

  if (!this.profile.name) {
    var fb = this.auth.facebook;
    this.profile.name =
      (this.auth.local && this.auth.local.username) ||
      (fb && (fb.displayName || fb.name || fb.username || (fb.first_name && fb.first_name + ' ' + fb.last_name))) ||
      'Anonymous';
  }

  var petCount = shared.countPets(_.reduce(this.items.pets,function(m,v){
    //HOTFIX - Remove when solution is found, the first argument passed to reduce is a function
    if(_.isFunction(v)) return m;
    return m+(v?1:0)},0), this.items.pets);

  this.achievements.beastMaster = petCount >= 90;

  //our own version incrementer
  this._v++;
  next();
});