Module: WolfCore::Integrations::WebhooksOperations

Defined in:
lib/wolf_core/application/integrations/webhooks_operations.rb

Instance Method Summary collapse

Instance Method Details

#build_body_from_params(params:, id_key:, include_changes: false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 55

def build_body_from_params(params:, id_key:, include_changes: false)
  body = {
    event_name: get_event_name(params: params),
    id_key => get_object_id(params: params)
  }
  if include_changes
    payload = get_payload(params: params)
    body[:changes] = payload&.dig("changes")
  end
  body
end

#get_event_name(params:) ⇒ Object



21
22
23
24
25
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 21

def get_event_name(params:)
  message = params["Message"] || {}
  message = JSON.parse(message) if message.is_a?(String)
  message["event_name"]
end

#get_event_type(params:) ⇒ Object



17
18
19
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 17

def get_event_type(params:)
  params["Type"]
end

#get_object_id(params:) ⇒ Object



67
68
69
70
71
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 67

def get_object_id(params:)
  message = params["Message"] || {}
  message = JSON.parse(message) if message.is_a?(String)
  message["object_id"]
end

#get_payload(params:, event_type: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 4

def get_payload(params:, event_type: nil)
  event_type ||= get_event_type(params: params)
  return if event_type == "SubscriptionConfirmation"

  message = params["Message"] || {}
  message = JSON.parse(message) if message.is_a?(String)

  payload = message["payload"] || {}
  payload = JSON.parse(payload) if payload.is_a?(String)

  payload
end

#group_custom_values_by_custom_requirement_group(payload:, jobseeker_file_custom_requirement_ids:, reference_form_custom_requirement_ids:, disclosure_form_custom_requirement_ids:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 73

def group_custom_values_by_custom_requirement_group(
  payload:,
  jobseeker_file_custom_requirement_ids:,
  reference_form_custom_requirement_ids:,
  disclosure_form_custom_requirement_ids:
)
  custom_values = payload.dig("changes", "custom_values")
  custom_values = [payload["attributes"]] if custom_values.blank?

  custom_values.group_by do |custom_value|
    custom_requirement_group = if jobseeker_file_custom_requirement_ids.include?(custom_value["custom_requirement_id"].to_s)
                                 "jobseeker_file"
                               elsif reference_form_custom_requirement_ids.include?(custom_value["custom_requirement_id"].to_s)
                                 "reference_form"
                               elsif disclosure_form_custom_requirement_ids.include?(custom_value["custom_requirement_id"].to_s)
                                 "disclosure_form"
                               elsif custom_value["user_category"] == "Freelancer"
                                 "jobseeker"
                               elsif custom_value["user_category"] == "Campaign"
                                 "order"
                               else
                                 "unknown_group"
                               end

    custom_requirement_group
  end
end

#process_webhook(params:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 44

def process_webhook(params:)
  event_type = get_event_type(params: params)
  if event_type == "SubscriptionConfirmation"
    url = params["SubscribeURL"]
    subscription_confirmation_request(url: url)
  elsif event_type == "Notification"
    event_name = get_event_name(params: params)
    yield(event_name)
  end
end

#subscription_confirmation_request(url:) ⇒ Object



37
38
39
40
41
42
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 37

def subscription_confirmation_request(url:)
  safe_http_get(
    url: url,
    error_message: "Can not confirm subscription"
  )
end

#validate_user_is_not_admin(params:) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 27

def validate_user_is_not_admin(params:)
  event_type = get_event_type(params: params)
  return if event_type == "SubscriptionConfirmation"

  user_id = params.dig("invoker", "user_id").to_s
  return unless user_id == ENV["WOLF_ADMIN_ID"].to_s

  raise_service_error("Ignoring event from user id #{user_id}")
end