lib/definitions/aggregate.js
function (aggregateModel, cmd, callback) {
    var cmdName = dotty.get(cmd, this.definitions.command.name);
    if (!cmdName) {
      var err = new Error('command has no command name in ' + this.definitions.command.name);
      debug(err);
      return callback(err);
    }

    var version = 0;
    if (!!this.definitions.command.version) {
      version = dotty.get(cmd, this.definitions.command.version);
    }

    var command = this.getCommand(cmdName, version);
    if (!command) {
      var err = new Error('Command "' + cmdName + '" not found!');
      debug(err);
      return callback(err);
    }

    var self = this;

    aggregateModel.set = function () {
      throw new Error('You are not allowed to set a value in this step!');
    };

    var previousModel = new AggregateModel(aggregateModel.id, aggregateModel);

    this.checkPreConditions(cmd, aggregateModel, function (err) {
      if (err) {
        return callback(err);
      }

      // attach apply function
      aggregateModel.apply = applyHelper(self, aggregateModel, cmd);

      debug('handle command');
      command.handle(cmd, aggregateModel);

      // remove apply function
      delete aggregateModel.apply;

      var uncommittedEvents = aggregateModel.getUncommittedEvents();

      async.each(uncommittedEvents, function (evt, callback) {
        var isEvtIdDefined = !!dotty.get(evt, self.definitions.event.id);
        if (isEvtIdDefined) {
          debug('event id already defined');
          return callback(null);
        }

        // generate new id for event
        debug('generate new id for event');
        self.getNewId(function (err, id) {
          if (err) {
            return callback(err);
          }

          dotty.put(evt, self.definitions.event.id, id);
          callback(null);
        });
      }, function (err) {
        if (err) {
          return callback(err);
        }

        // check business rules
        debug('check business rules');
        self.checkBusinessRules(aggregateModel, previousModel, uncommittedEvents, cmd, function (err) {
          if (!err) {
            return callback(null);
          }

          // clean up...
          aggregateModel.reset(previousModel);
          aggregateModel.clearUncommittedEvents();
          callback(err);
        });
      });
    });
  }
function (aggregateModel, cmd, callback) {
    var cmdName = dotty.get(cmd, this.definitions.command.name);
    if (!cmdName) {
      var err = new Error('command has no command name in ' + this.definitions.command.name);
      debug(err);
      return callback(err);
    }

    var version = 0;
    if (!!this.definitions.command.version) {
      version = dotty.get(cmd, this.definitions.command.version);
    }

    var command = this.getCommand(cmdName, version);
    if (!command) {
      var err = new Error('Command "' + cmdName + '" not found!');
      debug(err);
      return callback(err);
    }

    var self = this;

    aggregateModel.set = function () {
      throw new Error('You are not allowed to set a value in this step!');
    };

    var previousModel = new AggregateModel(aggregateModel.id, aggregateModel);

    this.checkPreConditions(cmd, aggregateModel, function (err) {
      if (err) {
        return callback(err);
      }

      // attach apply function
      aggregateModel.apply = applyHelper(self, aggregateModel, cmd);

      debug('handle command');
      command.handle(cmd, aggregateModel);

      // remove apply function
      delete aggregateModel.apply;

      var uncommittedEvents = aggregateModel.getUncommittedEvents();

      async.each(uncommittedEvents, function (evt, callback) {
        var isEvtIdDefined = !!dotty.get(evt, self.definitions.event.id);
        if (isEvtIdDefined) {
          debug('event id already defined');
          return callback(null);
        }

        // generate new id for event
        debug('generate new id for event');
        self.getNewId(function (err, id) {
          if (err) {
            return callback(err);
          }

          dotty.put(evt, self.definitions.event.id, id);
          callback(null);
        });
      }, function (err) {
        if (err) {
          return callback(err);
        }

        // check business rules
        debug('check business rules');
        self.checkBusinessRules(aggregateModel, previousModel, uncommittedEvents, cmd, function (err) {
          if (!err) {
            return callback(null);
          }

          // clean up...
          aggregateModel.reset(previousModel);
          aggregateModel.clearUncommittedEvents();
          callback(err);
        });
      });
    });
  }
function ( aggregateModel , cmd , callback ) { var cmdName = dotty . get ( cmd , this . definitions . command . name ) ; if ( ! cmdName ) { var err = new Error ( 'command has no command name in ' + this . definitions . command . name ) ; debug ( err ) ; return callback ( err ) ; } var version = 0 ; if ( ! ! this . definitions . command . version ) { version = dotty . get ( cmd , this . definitions . command . version ) ; } var command = this . getCommand ( cmdName , version ) ; if ( ! command ) { var err = new Error ( 'Command "' + cmdName + '" not found!' ) ; debug ( err ) ; return callback ( err ) ; } var self = this ; aggregateModel . set = function ( ) { throw new Error ( 'You are not allowed to set a value in this step!' ) ; } ; var previousModel = new AggregateModel ( aggregateModel . id , aggregateModel ) ; this . checkPreConditions ( cmd , aggregateModel , function ( err ) { if ( err ) { return callback ( err ) ; } // attach apply function aggregateModel . apply = applyHelper ( self , aggregateModel , cmd ) ; debug ( 'handle command' ) ; command . handle ( cmd , aggregateModel ) ; // remove apply function delete aggregateModel . apply ; var uncommittedEvents = aggregateModel . getUncommittedEvents ( ) ; async . each ( uncommittedEvents , function ( evt , callback ) { var isEvtIdDefined = ! ! dotty . get ( evt , self . definitions . event . id ) ; if ( isEvtIdDefined ) { debug ( 'event id already defined' ) ; return callback ( null ) ; } // generate new id for event debug ( 'generate new id for event' ) ; self . getNewId ( function ( err , id ) { if ( err ) { return callback ( err ) ; } dotty . put ( evt , self . definitions . event . id , id ) ; callback ( null ) ; } ) ; } , function ( err ) { if ( err ) { return callback ( err ) ; } // check business rules debug ( 'check business rules' ) ; self . checkBusinessRules ( aggregateModel , previousModel , uncommittedEvents , cmd , function ( err ) { if ( ! err ) { return callback ( null ) ; } // clean up... aggregateModel . reset ( previousModel ) ; aggregateModel . clearUncommittedEvents ( ) ; callback ( err ) ; } ) ; } ) ; } ) ; }
Handles the passed command and checks the business rules.
@param {AggregateModel}  aggregateModel The aggregateModel that should be used.
@param {Object}          cmd            The command that was handled.
@param {Function}        callback       The function, that will be called when this action is completed.
`function(err){}`
Handles the passed command and checks the business rules .