lib/impl/dataSources/updateCache.js
function saveAuditLogEntryAndUpdateDataSource(params, callback) {
  var cacheElement = params.cacheElement;
  var dataSourceDocument = params.dataSourceDocument;
  var dataSourceData = params.dataSourceData;

  async.waterfall([
    function saveAuditLog(cb) {
      //An Audit Log Entry Is Based On The Cache Update Entry.
      var auditLogEntry = cacheElement.toJSON();
      //Adding Service Details To The Audit Log Entry
      _.extend(auditLogEntry, _.pick(dataSourceDocument, "serviceGuid", "endpoint"));
      //Adding the audit log
      var AuditLog = models.get(params.connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE_AUDIT_LOG);
      auditLogEntry.dataSource = dataSourceDocument._id;

      var auditLogToSave = new AuditLog(auditLogEntry);

      auditLogToSave.save(function(err, savedAuditLog) {
        if (err) {
          return cb(err);
        }
        dataSourceDocument.auditLogs.push(savedAuditLog);

        return cb();
      });
    },
    function saveDataSource(cb) {

      //Having Attempted To Save, the updated data source has to be validated again to clear old validation errors before trying to save again.
      dataSourceDocument.validate(function() {
        //Data Source Data Is Now valid, can save it - whether it is in an error state or not.
        dataSourceDocument.save(function(err) {
          if (err) {
            dataSourceData.error = buildErrorResponse({
              error: err,
              userDetail: "Unexpected Error When Saving Data Source Data",
              code: ERROR_CODES.FH_FORMS_UNEXPECTED_ERROR
            });

            logger.error("Error Updating Data Source ", {error: dataSourceData.error, dataSourceDocument: dataSourceDocument, cache: dataSourceDocument.cache[0]});

            //Not interested in the document if the save is invalid
            dataSourceData = _.omit(dataSourceData, 'document');

            return cb(undefined, dataSourceData);
          }

          logger.debug("updateDataSourceEntry: Finished Updating Data Source", dataSourceDocument);

          //Save Was Successful, return updated document
          dataSourceData.document = dataSourceDocument;

          return cb(undefined, dataSourceData);
        });
      });

    }
  ], callback);
}
function saveAuditLogEntryAndUpdateDataSource(params, callback) {
  var cacheElement = params.cacheElement;
  var dataSourceDocument = params.dataSourceDocument;
  var dataSourceData = params.dataSourceData;

  async.waterfall([
    function saveAuditLog(cb) {
      //An Audit Log Entry Is Based On The Cache Update Entry.
      var auditLogEntry = cacheElement.toJSON();
      //Adding Service Details To The Audit Log Entry
      _.extend(auditLogEntry, _.pick(dataSourceDocument, "serviceGuid", "endpoint"));
      //Adding the audit log
      var AuditLog = models.get(params.connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE_AUDIT_LOG);
      auditLogEntry.dataSource = dataSourceDocument._id;

      var auditLogToSave = new AuditLog(auditLogEntry);

      auditLogToSave.save(function(err, savedAuditLog) {
        if (err) {
          return cb(err);
        }
        dataSourceDocument.auditLogs.push(savedAuditLog);

        return cb();
      });
    },
    function saveDataSource(cb) {

      //Having Attempted To Save, the updated data source has to be validated again to clear old validation errors before trying to save again.
      dataSourceDocument.validate(function() {
        //Data Source Data Is Now valid, can save it - whether it is in an error state or not.
        dataSourceDocument.save(function(err) {
          if (err) {
            dataSourceData.error = buildErrorResponse({
              error: err,
              userDetail: "Unexpected Error When Saving Data Source Data",
              code: ERROR_CODES.FH_FORMS_UNEXPECTED_ERROR
            });

            logger.error("Error Updating Data Source ", {error: dataSourceData.error, dataSourceDocument: dataSourceDocument, cache: dataSourceDocument.cache[0]});

            //Not interested in the document if the save is invalid
            dataSourceData = _.omit(dataSourceData, 'document');

            return cb(undefined, dataSourceData);
          }

          logger.debug("updateDataSourceEntry: Finished Updating Data Source", dataSourceDocument);

          //Save Was Successful, return updated document
          dataSourceData.document = dataSourceDocument;

          return cb(undefined, dataSourceData);
        });
      });

    }
  ], callback);
}
function saveAuditLogEntryAndUpdateDataSource ( params , callback ) { var cacheElement = params . cacheElement ; var dataSourceDocument = params . dataSourceDocument ; var dataSourceData = params . dataSourceData ; async . waterfall ( [ function saveAuditLog ( cb ) { //An Audit Log Entry Is Based On The Cache Update Entry. var auditLogEntry = cacheElement . toJSON ( ) ; //Adding Service Details To The Audit Log Entry _ . extend ( auditLogEntry , _ . pick ( dataSourceDocument , "serviceGuid" , "endpoint" ) ) ; //Adding the audit log var AuditLog = models . get ( params . connections . mongooseConnection , models . MODELNAMES . DATA_SOURCE_AUDIT_LOG ) ; auditLogEntry . dataSource = dataSourceDocument . _id ; var auditLogToSave = new AuditLog ( auditLogEntry ) ; auditLogToSave . save ( function ( err , savedAuditLog ) { if ( err ) { return cb ( err ) ; } dataSourceDocument . auditLogs . push ( savedAuditLog ) ; return cb ( ) ; } ) ; } , function saveDataSource ( cb ) { //Having Attempted To Save, the updated data source has to be validated again to clear old validation errors before trying to save again. dataSourceDocument . validate ( function ( ) { //Data Source Data Is Now valid, can save it - whether it is in an error state or not. dataSourceDocument . save ( function ( err ) { if ( err ) { dataSourceData . error = buildErrorResponse ( { error : err , userDetail : "Unexpected Error When Saving Data Source Data" , code : ERROR_CODES . FH_FORMS_UNEXPECTED_ERROR } ) ; logger . error ( "Error Updating Data Source " , { error : dataSourceData . error , dataSourceDocument : dataSourceDocument , cache : dataSourceDocument . cache [ 0 ] } ) ; //Not interested in the document if the save is invalid dataSourceData = _ . omit ( dataSourceData , 'document' ) ; return cb ( undefined , dataSourceData ) ; } logger . debug ( "updateDataSourceEntry: Finished Updating Data Source" , dataSourceDocument ) ; //Save Was Successful, return updated document dataSourceData . document = dataSourceDocument ; return cb ( undefined , dataSourceData ) ; } ) ; } ) ; } ] , callback ) ; }
Saving The Updated Data Source and Updating The Audit Log.
@param params
@param params.cacheElement - Cache Element Being Updated
@param params.dataSourceDocument - The Mongoose Data Source Document
@param params.dataSourceData - The Data Source Data To Be Updated
@param params.connections.mongooseConnection - The Mongoose Connection
@param callback
Saving The Updated Data Source and Updating The Audit Log .