lib/impl/updateForm.js
function findMatchingDocuments(type, documentIDs, modelToSearch, cb) {

    var errorTextDataType = type === models.MODELNAMES.DATA_SOURCE ? "Data Sources" : "Data Targets";

    //If the form contains no data sources, then no need to verify
    if (documentIDs.length === 0) {
      return cb(undefined, []);
    }

    var query = {_id: {
      "$in": documentIDs
    }};

    modelToSearch.find(query).exec(function(err, foundModels) {
      if (err) {
        return cb(buildErrorResponse({
          error: err,
          userDetail: "Unexpected Error Finding " + errorTextDataType
        }));
      }

      //There should be the same number of data source documents
      if (documentIDs.length !== foundModels.length) {

        var missingDocumentId = _.find(documentIDs, function(documentId) {
          return !_.findWhere(foundModels, {_id: documentId});
        });

        //If there is no missing data source, then something is wrong..
        if (!missingDocumentId) {
          return cb(buildErrorResponse({
            error: new Error("Unexpected Error When Finding " + errorTextDataType),
            systemDetail: "Expected A Missing " + errorTextDataType + " But Could Not Find It"
          }));
        }

        return cb(buildErrorResponse({
          userDetail: "A " + errorTextDataType + " Contained In The Form Could Not Be Found",
          systemDetail: "Expected " + errorTextDataType + " With ID " + missingDocumentId + " To Be Found",
          code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
        }));
      }

      //All documents found -- checking for cache if needed
      if (options.expectDataSourceCache && type === models.MODELNAMES.DATA_SOURCE) {
        var missingCache = _.find(foundModels, function(dataSource) {
          return dataSource.cache.length === 0;
        });

        if (missingCache) {
          return cb(buildErrorResponse({
            error: new Error("Expected " + errorTextDataType + " Cached Data To Be Set"),
            systemDetail: "Expected Cache For " + errorTextDataType + " ID " + missingCache._id + " To Be Set",
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          }));
        }
      }

      //Checking that only one real time data target is assigned.
      if (type === models.MODELNAMES.DATA_TARGET) {
        var realTimeDataTargets = _.filter(foundModels, function(foundModel) {
          return foundModel.type === models.CONSTANTS.DATA_TARGET_TYPE_REAL_TIME;
        });

        if (realTimeDataTargets.length > 1) {
          return cb(buildErrorResponse({
            error: new Error("Only One Real Time Data Target Can Be Assigned To A Form"),
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          }
                                      ));
        }
      }

      return cb(undefined, foundModels);
    });
  }
function findMatchingDocuments(type, documentIDs, modelToSearch, cb) {

    var errorTextDataType = type === models.MODELNAMES.DATA_SOURCE ? "Data Sources" : "Data Targets";

    //If the form contains no data sources, then no need to verify
    if (documentIDs.length === 0) {
      return cb(undefined, []);
    }

    var query = {_id: {
      "$in": documentIDs
    }};

    modelToSearch.find(query).exec(function(err, foundModels) {
      if (err) {
        return cb(buildErrorResponse({
          error: err,
          userDetail: "Unexpected Error Finding " + errorTextDataType
        }));
      }

      //There should be the same number of data source documents
      if (documentIDs.length !== foundModels.length) {

        var missingDocumentId = _.find(documentIDs, function(documentId) {
          return !_.findWhere(foundModels, {_id: documentId});
        });

        //If there is no missing data source, then something is wrong..
        if (!missingDocumentId) {
          return cb(buildErrorResponse({
            error: new Error("Unexpected Error When Finding " + errorTextDataType),
            systemDetail: "Expected A Missing " + errorTextDataType + " But Could Not Find It"
          }));
        }

        return cb(buildErrorResponse({
          userDetail: "A " + errorTextDataType + " Contained In The Form Could Not Be Found",
          systemDetail: "Expected " + errorTextDataType + " With ID " + missingDocumentId + " To Be Found",
          code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
        }));
      }

      //All documents found -- checking for cache if needed
      if (options.expectDataSourceCache && type === models.MODELNAMES.DATA_SOURCE) {
        var missingCache = _.find(foundModels, function(dataSource) {
          return dataSource.cache.length === 0;
        });

        if (missingCache) {
          return cb(buildErrorResponse({
            error: new Error("Expected " + errorTextDataType + " Cached Data To Be Set"),
            systemDetail: "Expected Cache For " + errorTextDataType + " ID " + missingCache._id + " To Be Set",
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          }));
        }
      }

      //Checking that only one real time data target is assigned.
      if (type === models.MODELNAMES.DATA_TARGET) {
        var realTimeDataTargets = _.filter(foundModels, function(foundModel) {
          return foundModel.type === models.CONSTANTS.DATA_TARGET_TYPE_REAL_TIME;
        });

        if (realTimeDataTargets.length > 1) {
          return cb(buildErrorResponse({
            error: new Error("Only One Real Time Data Target Can Be Assigned To A Form"),
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          }
                                      ));
        }
      }

      return cb(undefined, foundModels);
    });
  }
function findMatchingDocuments ( type , documentIDs , modelToSearch , cb ) { var errorTextDataType = type === models . MODELNAMES . DATA_SOURCE ? "Data Sources" : "Data Targets" ; //If the form contains no data sources, then no need to verify if ( documentIDs . length === 0 ) { return cb ( undefined , [ ] ) ; } var query = { _id : { "$in" : documentIDs } } ; modelToSearch . find ( query ) . exec ( function ( err , foundModels ) { if ( err ) { return cb ( buildErrorResponse ( { error : err , userDetail : "Unexpected Error Finding " + errorTextDataType } ) ) ; } //There should be the same number of data source documents if ( documentIDs . length !== foundModels . length ) { var missingDocumentId = _ . find ( documentIDs , function ( documentId ) { return ! _ . findWhere ( foundModels , { _id : documentId } ) ; } ) ; //If there is no missing data source, then something is wrong.. if ( ! missingDocumentId ) { return cb ( buildErrorResponse ( { error : new Error ( "Unexpected Error When Finding " + errorTextDataType ) , systemDetail : "Expected A Missing " + errorTextDataType + " But Could Not Find It" } ) ) ; } return cb ( buildErrorResponse ( { userDetail : "A " + errorTextDataType + " Contained In The Form Could Not Be Found" , systemDetail : "Expected " + errorTextDataType + " With ID " + missingDocumentId + " To Be Found" , code : ERROR_CODES . FH_FORMS_INVALID_PARAMETERS } ) ) ; } //All documents found -- checking for cache if needed if ( options . expectDataSourceCache && type === models . MODELNAMES . DATA_SOURCE ) { var missingCache = _ . find ( foundModels , function ( dataSource ) { return dataSource . cache . length === 0 ; } ) ; if ( missingCache ) { return cb ( buildErrorResponse ( { error : new Error ( "Expected " + errorTextDataType + " Cached Data To Be Set" ) , systemDetail : "Expected Cache For " + errorTextDataType + " ID " + missingCache . _id + " To Be Set" , code : ERROR_CODES . FH_FORMS_INVALID_PARAMETERS } ) ) ; } } //Checking that only one real time data target is assigned. if ( type === models . MODELNAMES . DATA_TARGET ) { var realTimeDataTargets = _ . filter ( foundModels , function ( foundModel ) { return foundModel . type === models . CONSTANTS . DATA_TARGET_TYPE_REAL_TIME ; } ) ; if ( realTimeDataTargets . length > 1 ) { return cb ( buildErrorResponse ( { error : new Error ( "Only One Real Time Data Target Can Be Assigned To A Form" ) , code : ERROR_CODES . FH_FORMS_INVALID_PARAMETERS } ) ) ; } } return cb ( undefined , foundModels ) ; } ) ; }
Finding Matching Documents
@param type - Data Source or Data Target
@param documentIDs - Array Of Document IDs
@param modelToSearch - Model to search on
@param cb
Finding Matching Documents