// This file contains all pre and post triggers for db
// interaction with our models. 

/* * * * * * * * * * * * * * * * * * * * * 
 *                 USER                  *
 * * * * * * * * * * * * * * * * * * * * */


var setCreatedTimestamp = function (next) {
  var now = Date.now();

  if (this.isNew) this.created = now;
  this.updated = now;

  next();
};

// This is a collector function which will trigger
// on all versions of the update trigger.
var setUpdatedTimestamp = function (next) {
  this.update({},{ $set: { updated: Date.now() } });

  next();
};

// All valid triggers are included here for reference.
module.exports.User = function(UserSchema) {
  UserSchema.pre('save', function(next) {
    setCreatedTimestamp.call(this, next);

  });

  UserSchema.pre('remove', function(next) {
    next();
  });

  UserSchema.pre('validate', function(next) {
    next();
  });

  UserSchema.pre('update', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

  UserSchema.pre('findOneAndUpdate', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

  UserSchema.pre('findByIdAndUpdate', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

};


/* * * * * * * * * * * * * * * * * * * * * 
 *               ROADMAP                 *
 * * * * * * * * * * * * * * * * * * * * */

module.exports.Roadmap = function(RoadmapSchema) {
  RoadmapSchema.pre('save', function(next) {
    setCreatedTimestamp.call(this, next);
  });

  RoadmapSchema.pre('remove', function(next) {
    next();
  });

  RoadmapSchema.pre('validate', function(next) {
    next();
  });

  RoadmapSchema.pre('update', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

  RoadmapSchema.pre('findOneAndUpdate', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

  RoadmapSchema.pre('findByIdAndUpdate', function(next) {
    setUpdatedTimestamp.call(this, next);
  });
};


/* * * * * * * * * * * * * * * * * * * * * 
 *                 NODE                  *
 * * * * * * * * * * * * * * * * * * * * */

module.exports.Node = function(NodeSchema) {

  NodeSchema.pre('save', function(next) {
    setCreatedTimestamp.call(this, next);
  });

  NodeSchema.pre('remove', function(next) {
    next();
  });

  NodeSchema.pre('validate', function(next) {
    next();
  });

  NodeSchema.pre('update', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

  NodeSchema.pre('findOneAndUpdate', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

  NodeSchema.pre('findByIdAndUpdate', function(next) {
    setUpdatedTimestamp.call(this, next);
  });

};
@@ -86,141 +86,8 @@
 . %0A%0A
-/* * * * * * * * * * * * * * * * * * * * * %0A *                 USER                  *%0A * * * * * * * * * * * * * * * * * * * * */%0A%0A%0A
 var 
@@ -431,16 +431,149 @@
 ();%0A%7D;%0A%0A
+%0A/* * * * * * * * * * * * * * * * * * * * * %0A *                 USER                  *%0A * * * * * * * * * * * * * * * * * * * * */%0A%0A
 // All v
@@ -634,20 +634,28 @@
 exports.
+set
 User
+Hooks
  = funct
@@ -664,24 +664,30 @@
 n(UserSchema
+, User
 ) %7B%0A  UserSc
@@ -762,17 +762,16 @@
  next);%0A
-%0A
   %7D);%0A%0A 
@@ -1349,23 +1349,31 @@
 exports.
+set
 Roadmap
+Hooks
  = funct
@@ -1385,24 +1385,33 @@
 oadmapSchema
+, Roadmap
 ) %7B%0A  Roadma
@@ -1566,76 +1566,8 @@
 );%0A%0A
-  RoadmapSchema.pre('validate', function(next) %7B%0A    next();%0A  %7D);%0A%0A
   Ro
@@ -2022,20 +2022,28 @@
 exports.
+set
 Node
+Hooks
  = funct
@@ -2060,13 +2060,18 @@
 hema
+, Node
 ) %7B%0A
-%0A
   No
@@ -2193,32 +2193,415 @@
 unction(next) %7B%0A
+    // On deletion of a Node, remove it's ID from the parent Roadmaps nodes array%0A    var Roadmap = require('./roadmaps/roadmapModel.js');%0A    var parentRoadmapID = this.parentRoadmap;%0A    var deletedNodeID = this._id;%0A%0A    var update = %7B $pull:%7B nodes: deletedNodeID %7D %7D;%0A%0A    Roadmap.findByIdAndUpdate(parentRoadmapID, update)%0A      .exec(function(err)%7B if (err) throw err; %7D);  %0A%0A
     next();%0A  %7D)

Add remove hook to Node model