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



2
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
# File 'app/controllers/maestrano/connec_controller.rb', line 2

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|
        begin
          organization = find_valid_organization(entity[:group_id], params[:tenant], entity_class_hash)
          next if organization.blank?

          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}\", trace=\"#{e.backtrace}\"")
        end
      end
    end
  rescue => e
    Maestrano::Connector::Rails::ConnectorLogger.log('warn', nil, "error processing notification #{e.message} - #{e.backtrace}")
  end

  head 200, content_type: 'application/json'
end