def unfold_references_helper(entity, array_of_refs, organization)
      ref = array_of_refs.shift
      field = entity[ref]

      # Unfold the id
      if array_of_refs.empty? && field
        return entity.delete(ref) if field.is_a?(String) # ~retro-compatibility to ease transition aroud Connec! idmaps rework. Should be removed eventually.

        id_hash = field.find { |id| id[:provider] == organization.oauth_provider && id[:realm] == organization.oauth_uid }
        if id_hash
          entity[ref] = id_hash['id']
        elsif field.find { |id| id[:provider] == 'connec' } # Should always be true as ids will always contain a connec id
          # We may enqueue a fetch on the endpoint of the missing association, followed by a re-fetch on this one.
          # However it's expected to be an edge case, so for now we rely on the fact that the webhooks should be relativly in order.
          # Worst case it'll be done on following sync
          entity.delete(ref)
          return nil
        end
        true

      # Follow embedment path
      else
        return true if field.blank?

        case field
        when Array
          bool = true
          field.each do |f|
            bool &= unfold_references_helper(f, array_of_refs.dup, organization)
          end
          bool
        when Hash
          unfold_references_helper(entity[ref], array_of_refs, organization)
        end
      end
    end
def unfold_references_helper(entity, array_of_refs, organization)
      ref = array_of_refs.shift
      field = entity[ref]

      # Unfold the id
      if array_of_refs.empty? && field
        return entity.delete(ref) if field.is_a?(String) # ~retro-compatibility to ease transition aroud Connec! idmaps rework. Should be removed eventually.

        id_hash = field.find { |id| id[:provider] == organization.oauth_provider && id[:realm] == organization.oauth_uid }
        if id_hash
          entity[ref] = id_hash['id']
        elsif field.find { |id| id[:provider] == 'connec' } # Should always be true as ids will always contain a connec id
          # We may enqueue a fetch on the endpoint of the missing association, followed by a re-fetch on this one.
          # However it's expected to be an edge case, so for now we rely on the fact that the webhooks should be relativly in order.
          # Worst case it'll be done on following sync
          entity.delete(ref)
          return nil
        end
        true

      # Follow embedment path
      else
        return true if field.blank?

        case field
        when Array
          bool = true
          field.each do |f|
            bool &= unfold_references_helper(f, array_of_refs.dup, organization)
          end
          bool
        when Hash
          unfold_references_helper(entity[ref], array_of_refs, organization)
        end
      end
    end
Recursive method for unfolding references