<commit_before>class Maestrano::ConnecController < Maestrano::Rails::WebHookController
  def notifications
    begin
      params.except(:tenant, :controller, :action, :connec).each do |entity_name, entities|
        entity_class_hash = find_entity_class(entity_name)
        next Maestrano::Connector::Rails::ConnectorLogger.log('info', nil, "Received notification from Connec! for unknow entity: #{entity_name}") unless entity_class_hash

        entities.each do |entity|
          organization = Maestrano::Connector::Rails::Organization.find_by(uid: entity[:group_id], tenant: params[:tenant])

          begin
            next unless valid_organization?(organization, entity_class_hash)
            Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Processing entity from Connec! webhook, entity_name=\"#{entity_name}\", data=\"#{entity}\"")

            connec_client = Maestrano::Connector::Rails::ConnecHelper.get_client(organization)
            external_client = Maestrano::Connector::Rails::External.get_client(organization)
            last_synchronization_date = organization.last_synchronization_date

            entity_instance = entity_class_hash[:class].new(organization, connec_client, external_client, {})
            entity_instance.before_sync(last_synchronization_date)

            # Build expected input for consolidate_and_map_data
            mapped_entity = map_entity(entity_class_hash, entity_instance, entity_name, entity)

            entity_instance.push_entities_to_external(mapped_entity[:connec_entities])
            entity_instance.after_sync(last_synchronization_date)
          rescue => e
            Maestrano::Connector::Rails::ConnectorLogger.log('warn', organization, "error processing notification entity_name=\"#{entity_name}\", message=\"#{e.message}\", #{e.backtrace.join("\n")}")
          end
        end
      end
    rescue => e
      Maestrano::Connector::Rails::ConnectorLogger.log('warn', nil, "error processing notification #{e.message} - #{e.backtrace.join("\n")}")
    end

    head 200, content_type: 'application/json'
  end

  private

    def valid_organization?(organization, entity_class_hash)
      if organization.nil?
        Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Received notification from Connec! for an unknown organization, organization_uid=\"#{entity[:group_id]}\", tenant=\"#{params[:tenant]}\"")
        return false
      end

      if organization.oauth_uid.blank?
        Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Received notification from Connec! for an organization not linked, organization_uid=\"#{entity[:group_id]}\", tenant=\"#{params[:tenant]}\"")
        return false
      end

      unless organization.sync_enabled && organization.synchronized_entities[entity_class_hash[:name].to_sym]
        Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Skipping notification from Connec! webhook, entity_name=\"#{entity_name}\"")
        return false
      end

      true
    end

    def find_entity_class(entity_name)
      Maestrano::Connector::Rails::External.entities_list.each do |entity_name_from_list|
        clazz = "Entities::#{entity_name_from_list.singularize.titleize.split.join}".constantize
        if clazz.methods.include?('connec_entities_names'.to_sym)
          formatted_entities_names = clazz.connec_entities_names.map { |n| n.parameterize('_').pluralize }
          return {class: clazz, is_complex: true, name: entity_name_from_list} if formatted_entities_names.include?(entity_name)
        elsif clazz.methods.include?('connec_entity_name'.to_sym) && clazz.normalized_connec_entity_name == entity_name
          return {class: clazz, is_complex: false, name: entity_name_from_list}
        end
      end
      nil
    end

    def map_entity(entity_class_hash, entity_instance, entity_name, entity)
      # Build expected input for consolidate_and_map_data
      if entity_class_hash[:is_complex]
        connec_hash_of_entities = Maestrano::Connector::Rails::ComplexEntity.build_hash_with_entities(entity_instance.class.connec_entities_names, entity_name, ->(name) { name.parameterize('_').pluralize }, [entity])
        filtered_entities = entity_instance.filter_connec_entities(connec_hash_of_entities)

        empty_external_hash = Maestrano::Connector::Rails::ComplexEntity.build_empty_hash(entity_instance.class.external_entities_names)
        mapped_entity = entity_instance.consolidate_and_map_data(filtered_entities, empty_external_hash)
      else
        filtered_entities = entity_instance.filter_connec_entities([entity])
        mapped_entity = entity_instance.consolidate_and_map_data(filtered_entities, [])
      end

      mapped_entity
    end
end
<commit_msg>lower case entities names in connec webhook<commit_after>class Maestrano::ConnecController < Maestrano::Rails::WebHookController
  def notifications
    begin
      params.except(:tenant, :controller, :action, :connec).each do |entity_name, entities|
        entity_class_hash = find_entity_class(entity_name)
        next Maestrano::Connector::Rails::ConnectorLogger.log('info', nil, "Received notification from Connec! for unknow entity: #{entity_name}") unless entity_class_hash

        entities.each do |entity|
          organization = Maestrano::Connector::Rails::Organization.find_by(uid: entity[:group_id], tenant: params[:tenant])

          begin
            next unless valid_organization?(organization, entity_class_hash)
            Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Processing entity from Connec! webhook, entity_name=\"#{entity_name}\", data=\"#{entity}\"")

            connec_client = Maestrano::Connector::Rails::ConnecHelper.get_client(organization)
            external_client = Maestrano::Connector::Rails::External.get_client(organization)
            last_synchronization_date = organization.last_synchronization_date

            entity_instance = entity_class_hash[:class].new(organization, connec_client, external_client, {})
            entity_instance.before_sync(last_synchronization_date)

            # Build expected input for consolidate_and_map_data
            mapped_entity = map_entity(entity_class_hash, entity_instance, entity_name, entity)

            entity_instance.push_entities_to_external(mapped_entity[:connec_entities])
            entity_instance.after_sync(last_synchronization_date)
          rescue => e
            Maestrano::Connector::Rails::ConnectorLogger.log('warn', organization, "error processing notification entity_name=\"#{entity_name}\", message=\"#{e.message}\", #{e.backtrace.join("\n")}")
          end
        end
      end
    rescue => e
      Maestrano::Connector::Rails::ConnectorLogger.log('warn', nil, "error processing notification #{e.message} - #{e.backtrace.join("\n")}")
    end

    head 200, content_type: 'application/json'
  end

  private

    def valid_organization?(organization, entity_class_hash)
      if organization.nil?
        Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Received notification from Connec! for an unknown organization, organization_uid=\"#{entity[:group_id]}\", tenant=\"#{params[:tenant]}\"")
        return false
      end

      if organization.oauth_uid.blank?
        Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Received notification from Connec! for an organization not linked, organization_uid=\"#{entity[:group_id]}\", tenant=\"#{params[:tenant]}\"")
        return false
      end

      unless organization.sync_enabled && organization.synchronized_entities[entity_class_hash[:name].to_sym]
        Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Skipping notification from Connec! webhook, entity_name=\"#{entity_name}\"")
        return false
      end

      true
    end

    def find_entity_class(entity_name)
      parametrised_entity_name = entity_name.parameterize('_')
      Maestrano::Connector::Rails::External.entities_list.each do |entity_name_from_list|
        clazz = "Entities::#{entity_name_from_list.singularize.titleize.split.join}".constantize
        if clazz.methods.include?('connec_entities_names'.to_sym)
          formatted_entities_names = clazz.connec_entities_names.map { |n| n.parameterize('_').pluralize }
          return {class: clazz, is_complex: true, name: entity_name_from_list} if formatted_entities_names.include?(parametrised_entity_name)
        elsif clazz.methods.include?('connec_entity_name'.to_sym) && clazz.normalized_connec_entity_name == parametrised_entity_name
          return {class: clazz, is_complex: false, name: entity_name_from_list}
        end
      end
      nil
    end

    def map_entity(entity_class_hash, entity_instance, entity_name, entity)
      # Build expected input for consolidate_and_map_data
      if entity_class_hash[:is_complex]
        connec_hash_of_entities = Maestrano::Connector::Rails::ComplexEntity.build_hash_with_entities(entity_instance.class.connec_entities_names, entity_name, ->(name) { name.parameterize('_').pluralize }, [entity])
        filtered_entities = entity_instance.filter_connec_entities(connec_hash_of_entities)

        empty_external_hash = Maestrano::Connector::Rails::ComplexEntity.build_empty_hash(entity_instance.class.external_entities_names)
        mapped_entity = entity_instance.consolidate_and_map_data(filtered_entities, empty_external_hash)
      else
        filtered_entities = entity_instance.filter_connec_entities([entity])
        mapped_entity = entity_instance.consolidate_and_map_data(filtered_entities, [])
      end

      mapped_entity
    end
end

{'fraction_non_alphanumeric': 0.10684508492022646, 'fraction_numerical': 0.0006176016469377251, 'mean_word_length': 4.437045327364298, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7050500', 'n_tokens_mistral': 2748, 'n_tokens_neox': 2815, 'n_words': 462}