lib/middleware/app.js
function findOrCreateMbaasApp(req, res, next){
  log.logger.debug("findOrCreateMbaasApp ", req.params);

  var models = mbaas.getModels();

  //Checking For Required Params
  var missingParams = _.filter(["appGuid", "apiKey", "coreHost"], function(requiredKey){
    return validation.validateParamPresent(requiredKey, req);
  });

  if(missingParams.length > 0){
    log.logger.debug("findOrCreateMbaasApp Missing Param ", missingParams[0]);
    return validation.requireParam(missingParams[0], req, res);
  }

  findMbaasApp(req, res, function(err){
    if(err){
      log.logger.debug("Error Finding Mbaas App ", err);
      return next(err);
    }
    var appMbaas = req.appMbaasModel;

    //App Found, moving on.
    if(_.isObject(appMbaas)){
      log.logger.debug("Mbaas App Already Exists ");
      return next();
    }

    //App mbaas does not exist, create it.
    log.logger.debug("Mbaas App Does Not Exist, Creating New App Model ", {params: req.params, body: req.body});
    models.AppMbaas.createModel({
      name: req.params.appname,
      domain: req.params.domain,
      environment: req.params.environment,
      guid: req.body.appGuid,
      apiKey: req.body.apiKey,
      coreHost: req.body.coreHost,
      type: req.body.type,
      mbaasUrl: req.body.mbaasUrl,
      isServiceApp: req.body.isServiceApp,
      url: req.body.url
    }, function(err, createdMbaasApp){
      if(err){
        log.logger.debug("Error Creating New Mbaas App Model ", err);
        return next(err);
      }

      req.appMbaasModel = createdMbaasApp;
      next();
    });
  });
}
function findOrCreateMbaasApp(req, res, next){
  log.logger.debug("findOrCreateMbaasApp ", req.params);

  var models = mbaas.getModels();

  //Checking For Required Params
  var missingParams = _.filter(["appGuid", "apiKey", "coreHost"], function(requiredKey){
    return validation.validateParamPresent(requiredKey, req);
  });

  if(missingParams.length > 0){
    log.logger.debug("findOrCreateMbaasApp Missing Param ", missingParams[0]);
    return validation.requireParam(missingParams[0], req, res);
  }

  findMbaasApp(req, res, function(err){
    if(err){
      log.logger.debug("Error Finding Mbaas App ", err);
      return next(err);
    }
    var appMbaas = req.appMbaasModel;

    //App Found, moving on.
    if(_.isObject(appMbaas)){
      log.logger.debug("Mbaas App Already Exists ");
      return next();
    }

    //App mbaas does not exist, create it.
    log.logger.debug("Mbaas App Does Not Exist, Creating New App Model ", {params: req.params, body: req.body});
    models.AppMbaas.createModel({
      name: req.params.appname,
      domain: req.params.domain,
      environment: req.params.environment,
      guid: req.body.appGuid,
      apiKey: req.body.apiKey,
      coreHost: req.body.coreHost,
      type: req.body.type,
      mbaasUrl: req.body.mbaasUrl,
      isServiceApp: req.body.isServiceApp,
      url: req.body.url
    }, function(err, createdMbaasApp){
      if(err){
        log.logger.debug("Error Creating New Mbaas App Model ", err);
        return next(err);
      }

      req.appMbaasModel = createdMbaasApp;
      next();
    });
  });
}
function findOrCreateMbaasApp ( req , res , next ) { log . logger . debug ( "findOrCreateMbaasApp " , req . params ) ; var models = mbaas . getModels ( ) ; //Checking For Required Params var missingParams = _ . filter ( [ "appGuid" , "apiKey" , "coreHost" ] , function ( requiredKey ) { return validation . validateParamPresent ( requiredKey , req ) ; } ) ; if ( missingParams . length > 0 ) { log . logger . debug ( "findOrCreateMbaasApp Missing Param " , missingParams [ 0 ] ) ; return validation . requireParam ( missingParams [ 0 ] , req , res ) ; } findMbaasApp ( req , res , function ( err ) { if ( err ) { log . logger . debug ( "Error Finding Mbaas App " , err ) ; return next ( err ) ; } var appMbaas = req . appMbaasModel ; //App Found, moving on. if ( _ . isObject ( appMbaas ) ) { log . logger . debug ( "Mbaas App Already Exists " ) ; return next ( ) ; } //App mbaas does not exist, create it. log . logger . debug ( "Mbaas App Does Not Exist, Creating New App Model " , { params : req . params , body : req . body } ) ; models . AppMbaas . createModel ( { name : req . params . appname , domain : req . params . domain , environment : req . params . environment , guid : req . body . appGuid , apiKey : req . body . apiKey , coreHost : req . body . coreHost , type : req . body . type , mbaasUrl : req . body . mbaasUrl , isServiceApp : req . body . isServiceApp , url : req . body . url } , function ( err , createdMbaasApp ) { if ( err ) { log . logger . debug ( "Error Creating New Mbaas App Model " , err ) ; return next ( err ) ; } req . appMbaasModel = createdMbaasApp ; next ( ) ; } ) ; } ) ; }
Middleware To Find Or Create An Mbaas App
@param req
@param res
@param next
Middleware To Find Or Create An Mbaas App