Module: WolfCore::FkmOperations

Includes:
HttpOperations, LoggingUtils
Defined in:
lib/wolf_core/infrastructure/fkm_operations.rb

Instance Method Summary collapse

Methods included from LoggingUtils

#log_object

Methods included from HttpOperations

#async_http_get, #async_http_post, #async_http_put, #http_get, #http_post, #http_put, #parse_http_response, #safe_http_get, #safe_http_post, #safe_http_put, #validate_http_response

Methods included from AsyncUtils

#run_async

Methods included from ExceptionOperations

#raise_service_error

Instance Method Details

#create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 32

def create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)
  response = http_post(
    url: "#{ENV['FKM_URL']}/Prod/create",
    body: {
      tenant: tenant,
      source: source,
      source_id: source_id,
      destination: destination,
      destination_id: destination_id
    }
  )
  response = parse_http_response(response)
  log_object response, title: 'create_foreign_key response is'
  validate_http_response(response: response, message: 'Error creating foreign key')
  parse_http_response(response).body
end

#find_or_create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 49

def find_or_create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)
  destination_id = get_foreign_key_destination_id(
    tenant: tenant, source: source, source_id: source_id,
    destination: destination
  )
  log_object "find_or_create_foreign_key destination_id is #{destination_id}"
  return Result.success(data: { operation: :find }) if destination_id.present?
  create_foreign_key(
    tenant: tenant, source: source, source_id: source_id,
    destination: destination, destination_id: destination_id
  )
  Result.success(data: { operation: :create })
end

#get_foreign_key_destination_id(tenant:, source:, source_id:, destination:) ⇒ Object



6
7
8
9
10
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 6

def get_foreign_key_destination_id(tenant:, source:, source_id:, destination:)
  foreign_keys = get_foreign_keys(tenant: tenant, source: source, source_id: source_id)
  foreign_key = foreign_keys.find { |fk| fk['destination'] == destination }
  foreign_key&.dig('destination_id')
end

#get_foreign_keys(tenant:, source:, source_id:) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 12

def get_foreign_keys(tenant:, source:, source_id:)
  response = http_get(
    url: "#{ENV['FKM_URL']}/Prod/lookup?tenant=#{tenant}&source=#{source}&source_id=#{source_id}"
  )
  response = parse_http_response(response)
  log_object response, title: 'get_foreign_keys response is'
  validate_http_response(response: response, message: 'Error getting foreign keys')
  response.body
end

#get_foreign_keys_by_destination(tenant:, destination:, destination_id:) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 22

def get_foreign_keys_by_destination(tenant:, destination:, destination_id:)
  response = http_get(
    url: "#{ENV['FKM_URL']}/Prod/lookup?tenant=#{tenant}&destination=#{destination}&destination_id=#{destination_id}"
  )
  response = parse_http_response(response)
  log_object response, title: 'get_foreign_keys_by_destination response is'
  validate_http_response(response: response, message: 'Error getting foreign keys by destination id')
  response.body
end