Module: GlobalRegistry::Bindings::Entity::PushRelationshipMethods

Extended by:
ActiveSupport::Concern
Included in:
Workers::PushRelationshipWorker
Defined in:
lib/global_registry_bindings/entity/push_relationship_methods.rb

Instance Method Summary collapse

Instance Method Details

#create_relationship_in_global_registryObject



38
39
40
41
42
43
44
45
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 38

def create_relationship_in_global_registry
  entity = put_relationship_to_global_registry
  relationship.id_value = global_registry_relationship_entity_id_from_entity entity
  model.update_column( # rubocop:disable Rails/SkipsModelValidations
    relationship.id_column,
    relationship.id_value
  )
end

#delete_relationship_from_global_registry(and_retry = true) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 116

def delete_relationship_from_global_registry(and_retry = true)
  GlobalRegistry::Bindings::Workers::DeleteEntityWorker.new.perform(relationship.id_value)
  model.update_column( # rubocop:disable Rails/SkipsModelValidations
    relationship.id_column, nil
  )
  return unless and_retry
  raise GlobalRegistry::Bindings::RelatedEntityExistsWithCID,
    "#{model.class.name}(#{model.id}) #{relationship.related_name}" \
    ":relationship already exists with client_integration_id(" \
    "#{relationship.client_integration_id}). Will delete and retry."
end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 60

def ensure_related_entities_have_global_registry_ids!
  return if relationship.primary_id_value && relationship.related_id_value
  # Enqueue push_entity worker for related entities missing global_registry_id and retry relationship push
  names = []
  unless relationship.primary_id_value
    names << push_primary_to_global_registry
  end
  unless relationship.related_id_value
    names << push_related_to_global_registry
  end
  raise GlobalRegistry::Bindings::RelatedEntityMissingGlobalRegistryId,
    "#{model.class.name}(#{model.id}) has related entities [#{names.compact.join ", "}] missing " \
    "global_registry_id; will retry."
end

#global_registry_relationship_entity_id_from_entity(entity) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 47

def global_registry_relationship_entity_id_from_entity(entity)
  relationships = Array.wrap entity.dig(
    "entity",
    relationship.primary_type.to_s,
    "#{relationship.related_name}:relationship"
  )
  relationships.detect do |rel|
    cid = rel["client_integration_id"]
    cid = cid["value"] if cid.is_a?(Hash)
    cid == relationship.client_integration_id.to_s
  end&.dig("relationship_entity_id")
end

#push_primary_to_global_registryObject



75
76
77
78
79
80
81
82
83
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 75

def push_primary_to_global_registry
  model = relationship.primary
  if relationship.primary_binding == :entity
    model.push_entity_to_global_registry_async
  else
    model.push_relationships_to_global_registry_async(relationship.primary_binding)
  end
  "#{model.class.name}(#{model.id})"
end


85
86
87
88
89
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 85

def push_related_to_global_registry
  model = relationship.related
  model.push_entity_to_global_registry_async
  "#{model.class.name}(#{model.id})"
end

#push_relationship_to_global_registryObject



16
17
18
19
20
21
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 16

def push_relationship_to_global_registry
  return unless valid_update?
  ensure_related_entities_have_global_registry_ids!
  push_global_registry_relationship_type
  create_relationship_in_global_registry
end

#put_relationship_to_global_registryObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 100

def put_relationship_to_global_registry
  GlobalRegistry::Entity.put(
    relationship.primary_id_value,
    relationship_entity,
    params: {
      full_response: true,
      fields: "#{relationship.related_name}:relationship"
    }
  )
rescue RestClient::BadRequest => e
  response = JSON.parse(e.response.body)
  raise unless /^Validation failed:.*already exists$/i.match?(response["error"])
  # Delete relationship entity and retry on 400 Bad Request (client_integration_id already exists)
  delete_relationship_from_global_registry
end

#relationshipObject



12
13
14
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 12

def relationship
  global_registry_relationship(type)
end

#relationship_entityObject



91
92
93
94
95
96
97
98
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 91

def relationship_entity
  {entity: {relationship.primary_type => {
    "#{relationship.related_name}:relationship" =>
      model.relationship_attributes_to_push(type)
        .merge(relationship.related_type =>
                 relationship.related_id_value)
  }, :client_integration_id => relationship.primary.id}}
end

#valid_update?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/global_registry_bindings/entity/push_relationship_methods.rb', line 23

def valid_update?
  # We can't update relationship if related model is missing, but we may need to delete
  if relationship.related.nil?
    if relationship.related_id_value.nil? && relationship.id_value
      # Delete relationship if it exists and the related id_value is missing
      delete_relationship_from_global_registry(false)
      return false
    end
    # Do nothing if related model is missing and related_binding is :entity, :remote binding allows
    # empty related model
    return false if relationship.related_binding == :entity
  end
  true
end