def unfold_references(connec_entity, references, organization)
      references = format_references(references)
      unfolded_connec_entity = connec_entity.deep_dup.with_indifferent_access
      not_nil = true

      # Id
      id_hash = unfolded_connec_entity['id'].find { |id| id['provider'] == organization.oauth_provider && id['realm'] == organization.oauth_uid }
      connec_id = unfolded_connec_entity['id'].find { |id| id['provider'] == 'connec' }['id']
      unfolded_connec_entity['id'] = id_hash ? id_hash['id'] : nil

      # Other references
      # Record references are references to other records (organization_id, item_id, ...)
      references[:record_references].each do |reference|
        not_nil &= unfold_references_helper(unfolded_connec_entity, reference.split('/'), organization)
      end
      # Id references are references to sub entities ids (invoice lines id, ...)
      # We do not return nil if we're missing an id reference
      references[:id_references].each do |reference|
        unfold_references_helper(unfolded_connec_entity, reference.split('/'), organization)
      end
      unfolded_connec_entity = not_nil ? unfolded_connec_entity : nil

      # Filter the connec entity to keep only the id_references fields (in order to save some memory)
      # Give an empty hash if there's nothing left
      id_refs_only_connec_entity = filter_connec_entity_for_id_refs(connec_entity, references[:id_references])

      {entity: unfolded_connec_entity, connec_id: connec_id, id_refs_only_connec_entity: id_refs_only_connec_entity}
    end
def unfold_references(connec_entity, references, organization)
      references = format_references(references)
      unfolded_connec_entity = connec_entity.deep_dup.with_indifferent_access
      not_nil = true

      # Id
      id_hash = unfolded_connec_entity['id'].find { |id| id['provider'] == organization.oauth_provider && id['realm'] == organization.oauth_uid }
      connec_id = unfolded_connec_entity['id'].find { |id| id['provider'] == 'connec' }['id']
      unfolded_connec_entity['id'] = id_hash ? id_hash['id'] : nil

      # Other references
      # Record references are references to other records (organization_id, item_id, ...)
      references[:record_references].each do |reference|
        not_nil &= unfold_references_helper(unfolded_connec_entity, reference.split('/'), organization)
      end
      # Id references are references to sub entities ids (invoice lines id, ...)
      # We do not return nil if we're missing an id reference
      references[:id_references].each do |reference|
        unfold_references_helper(unfolded_connec_entity, reference.split('/'), organization)
      end
      unfolded_connec_entity = not_nil ? unfolded_connec_entity : nil

      # Filter the connec entity to keep only the id_references fields (in order to save some memory)
      # Give an empty hash if there's nothing left
      id_refs_only_connec_entity = filter_connec_entity_for_id_refs(connec_entity, references[:id_references])

      {entity: unfolded_connec_entity, connec_id: connec_id, id_refs_only_connec_entity: id_refs_only_connec_entity}
    end
Replaces the arrays of id received from Connec! by the id of the external application
 Returns a hash {entity: {}, connec_id: '', id_refs_only_connec_entity: {}}
 If an array has no id for this oauth_provider and oauth_uid but has one for connec, it returns a nil entity (skip the record)