Module: WolfCore::FkmOperations

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

Instance Method Summary collapse

Methods included from LoggingUtils

#log_object

Methods included from ExceptionOperations

#raise_service_error

Methods included from HttpOperations

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

Methods included from AsyncUtils

#run_async

Instance Method Details

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 40

def create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)
  raise_service_error({ message: 'tenant is required to create a foreign key', tenant: tenant }) if tenant.blank?
  raise_service_error({ message: 'source is required to create a foreign key', source: source }) if source.blank?
  raise_service_error({ message: 'source_id is required to create a foreign key', source_id: source_id }) if source_id.blank?
  raise_service_error({ message: 'destination is required to create a foreign key', destination: destination }) if destination.blank?
  raise_service_error({ message: 'destination_id is required to create a foreign key', destination_id: destination_id }) if destination_id.blank?
  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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 62

def find_or_create_foreign_key(tenant:, source:, source_id:, destination:, destination_id:)
  found_destination_id = get_foreign_key_destination_id(
    tenant: tenant, source: source, source_id: source_id,
    destination: destination
  )
  log_object "find_or_create_foreign_key found_destination_id is #{found_destination_id}"
  return Result.success(data: { operation: :find }) if found_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_by_destination(tenant:, source:, source_id:, destination:) ⇒ Object



15
16
17
18
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 15

def get_foreign_key_by_destination(tenant:, source:, source_id:, destination:)
  foreign_keys = get_foreign_keys(tenant: tenant, source: source, source_id: source_id)
  foreign_keys.find { |fk| fk['destination'] == destination }
end

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



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

def get_foreign_key_destination_id(tenant:, source:, source_id:, destination:)
  foreign_key = get_foreign_key_by_destination(
    tenant: tenant, source: source, source_id: source_id,
    destination: destination
  )
  foreign_key&.dig('destination_id')
end

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



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

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



30
31
32
33
34
35
36
37
38
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 30

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

#update_foreign_key(tenant:, auth_token:, uuid:, source: nil, source_id: nil, destination: nil, destination_id: nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 95

def update_foreign_key(tenant:, auth_token:, uuid:, source: nil, source_id: nil, destination: nil, destination_id: nil)
  raise_service_error({ message: 'tenant is required to update a foreign key', tenant: tenant }) if tenant.blank?
  raise_service_error({ message: 'auth_token is required to update a foreign key', auth_token: auth_token }) if auth_token.blank?
  raise_service_error({ message: 'uuid is required to update a foreign key', uuid: uuid }) if uuid.blank?

  fields_to_update = {
    source: source,
    source_id: source_id,
    destination: destination,
    destination_id: destination_id,
  }.compact

  raise_service_error({ message: 'At least one field to update is required to update a foreign key', source: source }) if fields_to_update.blank?

  body = {
    tenant: tenant,
    authentication_token: auth_token,
    uuid: uuid,
  }.merge(fields_to_update)
  log_object body, title: 'update_foreign_key body is'

  response = safe_http_put(
    url: "#{ENV['FKM_URL']}/Prod/update",
    body: body,
    title: 'update_foreign_key response is',
    error_message: 'Error updating foreign key',
  )
  response.body
end

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wolf_core/infrastructure/fkm_operations.rb', line 76

def upsert_foreign_key(tenant:, auth_token:, source:, source_id:, destination:, destination_id:)
  foreign_key = get_foreign_key_by_destination(
    tenant: tenant, source: source, source_id: source_id,
    destination: destination
  )
  if foreign_key.present?
    update_foreign_key(
      tenant: tenant, auth_token: auth_token, uuid: foreign_key['uuid'],
      source: source, source_id: source_id,
      destination: destination, destination_id: destination_id,
    )
  else
    create_foreign_key(
      tenant: tenant, source: source, source_id: source_id,
      destination: destination, destination_id: destination_id
    )
  end
end