Class: Maestrano::ConnecController

Inherits:
Rails::WebHookController
  • Object
show all
Defined in:
app/controllers/maestrano/connec_controller.rb

Instance Method Summary collapse

Instance Method Details

#notificationsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/maestrano/connec_controller.rb', line 3

def notifications
  Rails.logger.debug "Received notification from Connec!: #{params}"

  begin
    params.except(:tenant, :controller, :action).each do |entity_name, entities|
      if entity_instance_hash = find_entity_instance(entity_name)
        entity_instance = entity_instance_hash[:instance]

        entities.each do |entity|
          if (organization = Maestrano::Connector::Rails::Organization.find_by(uid: entity[:group_id], tenant: params[:tenant])) && organization.oauth_uid
            Maestrano::Connector::Rails::ConnectorLogger.log('info', organization, "Received entity from Connec! webhook: Entity=#{entity_name}, Data=#{entity}")
            if organization.sync_enabled && organization.synchronized_entities[entity_instance_hash[:name].to_sym]
              external_client = Maestrano::Connector::Rails::External.get_client(organization)

              # Build expected input for consolidate_and_map_data
              if entity_instance_hash[:is_complex]
                mapped_entity = entity_instance.consolidate_and_map_data(Hash[ *entity_instance.class.connec_entities_names.collect{|name| name.parameterize('_').pluralize == entity_name ? [name, [entity]] : [ name, []]}.flatten(1) ], Hash[ *entity_instance.class.external_entities_names.collect{|name| [ name, []]}.flatten(1) ], organization, {})
              else
                mapped_entity = entity_instance.consolidate_and_map_data([entity], [], organization, {})
              end

              entity_instance.push_entities_to_external(external_client, mapped_entity[:connec_entities], organization)
            end

          else
            Rails.logger.warn "Received notification from Connec! for unknown group or group without oauth: #{entity['group_id']} (tenant: #{params[:tenant]})"
          end
        end
      else
        Rails.logger.info "Received notification from Connec! for unknow entity: #{entity_name}"
      end
    end
  rescue => e
    Rails.logger.warn("error processing notification #{e.message} - #{e.backtrace.join("\n")}")
  end

  head 200, content_type: "application/json"
end