var db = require( '../config/db' );
var helpers = require( '../config/helpers' );
var Sequelize = require( 'sequelize' );
var User = require( '../users/users' );
var Session = require( '../sessions/sessions' );

var Session_User = db.define( 'sessions_users', {
	user_id: {
    type: Sequelize.INTEGER,
    unique: 'session_user_idx'
  },
	session_id: {
    type: Sequelize.INTEGER,
    unique: 'session_user_idx'
  }
} );

Session_User.sync().then( function(){
  console.log("sessions_users table created");
} )
.catch( function( err ){
  console.error(err);
} );

Session_User.belongsTo( User, {foreignKey: 'user_id'} );
Session_User.belongsTo( Session, {foreignKey: 'session_id'} );

Session_User.getSessionUserBySessionIdAndUserId = function( sessionID, userID ) {
  return Session_User.findOne({where: {session_id: sessionID, user_id: userID} })
    .catch( function( err ) {
      helpers.errorLogger( err );
    });
};

Session_User.countUsersInOneSession = function( sessionID ) {
  /* STUB FOR TESTING, REMOVE WHEN THIS FUNCTION IS IMPLEMENTED */
  return {
    then: function( resolve ) {
      if( sessionID == 1 ) {
        resolve( 2 );
      } else {
        resolve( 0 );
      }
    }
  }
  /* END STUB */
};

module.exports = Session_User; 

@@ -988,238 +988,131 @@
 %7B%0A  
-/* STUB FOR TESTING, REMOVE WHEN THIS FUNCTION IS IMPLEMENTED */%0A  return %7B%0A    then: function( resolve ) %7B%0A      if( sessionID == 1 ) %7B%0A        resolve( 2 );%0A      %7D else %7B%0A        resolve( 0 );%0A      %7D%0A    %7D%0A  %7D%0A  /* END STUB */
+return Session_User.count( %7B where: %7B id: sessionId %7D %7D )%0A  .catch( function( err ) %7B%0A    helpers.errorLogger( err );%0A  %7D);
 %0A%7D;%0A

Refactor stub into real query