lib/impl/dataSources/updateCache.js
function findDataSources(connections, dataSources, cb) {
  //Just Want The Data Source IDs.
  logger.debug("findDataSources", {dataSources: dataSources});
  var dataSourceIDsToUpdate = _.map(dataSources, function(dataSourceUpdateData) {
    return dataSourceUpdateData.error ? null : dataSourceUpdateData._id;
  });

  dataSourceIDsToUpdate = _.compact(dataSourceIDsToUpdate);

  //No Valid Data Sources To Update. No Need To Search For Data Sources
  if (dataSourceIDsToUpdate.length === 0) {
    return cb(undefined, dataSourceIDsToUpdate);
  }

  var query = {

  };

  //Searching By ID. Just one mongo call to get them all. More Efficient
  query[CONSTANTS.DATA_SOURCE_ID] = {
    "$in": dataSourceIDsToUpdate
  };

  lookUpDataSources(connections, {
    query: query
  }, function(err, foundDataSources) {
    if (err) {
      logger.error("Error Finding Data Sources ", {error: err});
      return cb(buildErrorResponse({
        error: err,
        userDetail: "Unexpected Error When Searching For A Data Source",
        code: ERROR_CODES.FH_FORMS_UNEXPECTED_ERROR
      }));
    }

    //Map Found Data Source Documents
    var dsWithDocuments = _.map(dataSources, function(validDataSource) {
      var matchingDocument = _.find(foundDataSources, function(dataSourceDocument) {
        return _.isEqual(dataSourceDocument._id.toString(), validDataSource._id.toString());
      });

      //If the document is found, assign it to the object, if not, set an error
      if (matchingDocument) {
        validDataSource.document = matchingDocument;
      } else {
        validDataSource.error = buildErrorResponse({
          error: new Error("Data Source Not Found"),
          userDetail: "Data Source Not Found",
          code: ERROR_CODES.FH_FORMS_NOT_FOUND
        });
      }

      return validDataSource;
    });

    var validInvalidDataSources = updateValidAndInvalidDataSources(dsWithDocuments, []);

    logger.debug("findDataSources", {validInvalidDataSources: validInvalidDataSources});

    cb(undefined, validInvalidDataSources.valid, validInvalidDataSources.invalid);
  });
}
function findDataSources(connections, dataSources, cb) {
  //Just Want The Data Source IDs.
  logger.debug("findDataSources", {dataSources: dataSources});
  var dataSourceIDsToUpdate = _.map(dataSources, function(dataSourceUpdateData) {
    return dataSourceUpdateData.error ? null : dataSourceUpdateData._id;
  });

  dataSourceIDsToUpdate = _.compact(dataSourceIDsToUpdate);

  //No Valid Data Sources To Update. No Need To Search For Data Sources
  if (dataSourceIDsToUpdate.length === 0) {
    return cb(undefined, dataSourceIDsToUpdate);
  }

  var query = {

  };

  //Searching By ID. Just one mongo call to get them all. More Efficient
  query[CONSTANTS.DATA_SOURCE_ID] = {
    "$in": dataSourceIDsToUpdate
  };

  lookUpDataSources(connections, {
    query: query
  }, function(err, foundDataSources) {
    if (err) {
      logger.error("Error Finding Data Sources ", {error: err});
      return cb(buildErrorResponse({
        error: err,
        userDetail: "Unexpected Error When Searching For A Data Source",
        code: ERROR_CODES.FH_FORMS_UNEXPECTED_ERROR
      }));
    }

    //Map Found Data Source Documents
    var dsWithDocuments = _.map(dataSources, function(validDataSource) {
      var matchingDocument = _.find(foundDataSources, function(dataSourceDocument) {
        return _.isEqual(dataSourceDocument._id.toString(), validDataSource._id.toString());
      });

      //If the document is found, assign it to the object, if not, set an error
      if (matchingDocument) {
        validDataSource.document = matchingDocument;
      } else {
        validDataSource.error = buildErrorResponse({
          error: new Error("Data Source Not Found"),
          userDetail: "Data Source Not Found",
          code: ERROR_CODES.FH_FORMS_NOT_FOUND
        });
      }

      return validDataSource;
    });

    var validInvalidDataSources = updateValidAndInvalidDataSources(dsWithDocuments, []);

    logger.debug("findDataSources", {validInvalidDataSources: validInvalidDataSources});

    cb(undefined, validInvalidDataSources.valid, validInvalidDataSources.invalid);
  });
}
function findDataSources ( connections , dataSources , cb ) { //Just Want The Data Source IDs. logger . debug ( "findDataSources" , { dataSources : dataSources } ) ; var dataSourceIDsToUpdate = _ . map ( dataSources , function ( dataSourceUpdateData ) { return dataSourceUpdateData . error ? null : dataSourceUpdateData . _id ; } ) ; dataSourceIDsToUpdate = _ . compact ( dataSourceIDsToUpdate ) ; //No Valid Data Sources To Update. No Need To Search For Data Sources if ( dataSourceIDsToUpdate . length === 0 ) { return cb ( undefined , dataSourceIDsToUpdate ) ; } var query = { } ; //Searching By ID. Just one mongo call to get them all. More Efficient query [ CONSTANTS . DATA_SOURCE_ID ] = { "$in" : dataSourceIDsToUpdate } ; lookUpDataSources ( connections , { query : query } , function ( err , foundDataSources ) { if ( err ) { logger . error ( "Error Finding Data Sources " , { error : err } ) ; return cb ( buildErrorResponse ( { error : err , userDetail : "Unexpected Error When Searching For A Data Source" , code : ERROR_CODES . FH_FORMS_UNEXPECTED_ERROR } ) ) ; } //Map Found Data Source Documents var dsWithDocuments = _ . map ( dataSources , function ( validDataSource ) { var matchingDocument = _ . find ( foundDataSources , function ( dataSourceDocument ) { return _ . isEqual ( dataSourceDocument . _id . toString ( ) , validDataSource . _id . toString ( ) ) ; } ) ; //If the document is found, assign it to the object, if not, set an error if ( matchingDocument ) { validDataSource . document = matchingDocument ; } else { validDataSource . error = buildErrorResponse ( { error : new Error ( "Data Source Not Found" ) , userDetail : "Data Source Not Found" , code : ERROR_CODES . FH_FORMS_NOT_FOUND } ) ; } return validDataSource ; } ) ; var validInvalidDataSources = updateValidAndInvalidDataSources ( dsWithDocuments , [ ] ) ; logger . debug ( "findDataSources" , { validInvalidDataSources : validInvalidDataSources } ) ; cb ( undefined , validInvalidDataSources . valid , validInvalidDataSources . invalid ) ; } ) ; }
Finding All Data Sources That Need To Be Updated
@param dataSources
@param connections
@param cb
@returns {*}
Finding All Data Sources That Need To Be Updated