utilities/handler-helper.js
function filterDeletedEmbeds(result, parent, parentkey, depth, Log) {
  if (_.isArray(result)) {
    result = result.filter(function(obj) {
      let keep = filterDeletedEmbeds(obj, result, parentkey, depth + 1, Log)
      // Log.log("KEEP:", keep);
      return keep
    })
    // Log.log("UPDATED:", parentkey);
    // Log.note("AFTER:", result);
    parent[parentkey] = result
  } else {
    for (let key in result) {
      // Log.debug("KEY:", key);
      // Log.debug("VALUE:", result[key]);
      if (_.isArray(result[key])) {
        // Log.log("JUMPING IN ARRAY");
        filterDeletedEmbeds(result[key], result, key, depth + 1, Log)
      } else if (_.isObject(result[key]) && result[key]._id) {
        // Log.log("JUMPING IN OBJECT");
        let keep = filterDeletedEmbeds(result[key], result, key, depth + 1, Log)
        if (!keep) {
          return false
        }
      } else if (key === 'isDeleted' && result[key] === true && depth > 0) {
        // Log.log("DELETED", depth);
        return false
      }
    }
    // Log.log("JUMPING OUT");
    return true
  }
}
function filterDeletedEmbeds(result, parent, parentkey, depth, Log) {
  if (_.isArray(result)) {
    result = result.filter(function(obj) {
      let keep = filterDeletedEmbeds(obj, result, parentkey, depth + 1, Log)
      // Log.log("KEEP:", keep);
      return keep
    })
    // Log.log("UPDATED:", parentkey);
    // Log.note("AFTER:", result);
    parent[parentkey] = result
  } else {
    for (let key in result) {
      // Log.debug("KEY:", key);
      // Log.debug("VALUE:", result[key]);
      if (_.isArray(result[key])) {
        // Log.log("JUMPING IN ARRAY");
        filterDeletedEmbeds(result[key], result, key, depth + 1, Log)
      } else if (_.isObject(result[key]) && result[key]._id) {
        // Log.log("JUMPING IN OBJECT");
        let keep = filterDeletedEmbeds(result[key], result, key, depth + 1, Log)
        if (!keep) {
          return false
        }
      } else if (key === 'isDeleted' && result[key] === true && depth > 0) {
        // Log.log("DELETED", depth);
        return false
      }
    }
    // Log.log("JUMPING OUT");
    return true
  }
}
function filterDeletedEmbeds ( result , parent , parentkey , depth , Log ) { if ( _ . isArray ( result ) ) { result = result . filter ( function ( obj ) { let keep = filterDeletedEmbeds ( obj , result , parentkey , depth + 1 , Log ) // Log.log("KEEP:", keep); return keep } ) // Log.log("UPDATED:", parentkey); // Log.note("AFTER:", result); parent [ parentkey ] = result } else { for ( let key in result ) { // Log.debug("KEY:", key); // Log.debug("VALUE:", result[key]); if ( _ . isArray ( result [ key ] ) ) { // Log.log("JUMPING IN ARRAY"); filterDeletedEmbeds ( result [ key ] , result , key , depth + 1 , Log ) } else if ( _ . isObject ( result [ key ] ) && result [ key ] . _id ) { // Log.log("JUMPING IN OBJECT"); let keep = filterDeletedEmbeds ( result [ key ] , result , key , depth + 1 , Log ) if ( ! keep ) { return false } } else if ( key === 'isDeleted' && result [ key ] === true && depth > 0 ) { // Log.log("DELETED", depth); return false } } // Log.log("JUMPING OUT"); return true } }
This function is called after embedded associations have been populated so that any associations
that have been soft deleted are removed.
@param result: the object that is being inspected
@param parent: the parent of the result object
@param parentkey: the parents key for the result object
@param depth: the current recursion depth
@param Log: a logging object
@returns {boolean}: returns false if the result object should be removed from the parent
@private
This function is called after embedded associations have been populated so that any associations that have been soft deleted are removed .