Module: GlobalRegistry::Bindings::Entity::PushEntityMethods

Extended by:
ActiveSupport::Concern
Included in:
Workers::PushEntityWorker
Defined in:
lib/global_registry_bindings/entity/push_entity_methods.rb

Instance Method Summary collapse

Instance Method Details

#create_dependent_entity_in_global_registryObject

Create or Update a child entity (ex: :email_address is a child of :person)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 48

def create_dependent_entity_in_global_registry # rubocop:disable Metrics/AbcSize
  return if global_registry_entity.parent.blank?
  ensure_parent_entity_has_global_registry_id!
  entity_attributes = {
    global_registry_entity.parent_type => {
      :client_integration_id => global_registry_entity.parent.id,
      global_registry_entity.type => model.entity_attributes_to_push
    }
  }
  entity = GlobalRegistry::Entity.put(global_registry_entity.parent_id_value, entity: entity_attributes)
  global_registry_entity.id_value = dig_global_registry_id_from_entity(entity["entity"],
    global_registry_entity.type,
    global_registry_entity.parent_type)
  model.update_column(global_registry_entity.id_column, # rubocop:disable Rails/SkipsModelValidations
    global_registry_entity.id_value)
  update_fingerprint
end

#create_entity_in_global_registryObject



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

def create_entity_in_global_registry
  ensure_parent_entity_has_global_registry_id! if global_registry_entity.parent.present?
  entity_attributes = {global_registry_entity.type => model.entity_attributes_to_push}
  entity = GlobalRegistry::Entity.post(entity: entity_attributes)
  global_registry_entity.id_value = dig_global_registry_id_from_entity(entity["entity"],
    global_registry_entity.type)
  model.update_column(global_registry_entity.id_column, # rubocop:disable Rails/SkipsModelValidations
    global_registry_entity.id_value)
  update_fingerprint
end

#dig_global_registry_id_from_entity(entity, type, parent_type = nil) ⇒ Object



66
67
68
69
70
71
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 66

def dig_global_registry_id_from_entity(entity, type, parent_type = nil)
  return entity&.dig(type.to_s, "id") unless parent_type
  Array.wrap(entity&.dig(parent_type.to_s, type.to_s)).detect do |item|
    item["client_integration_id"] == model.id.to_s
  end&.dig("id")
end

#ensure_parent_entity_has_global_registry_id!Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 73

def ensure_parent_entity_has_global_registry_id!
  return unless (global_registry_entity.parent_is_self? && global_registry_entity.parent_id_value.blank?) ||
    global_registry_entity.parent_id_value.blank?
  # Push parent entity if it exists and is missing global_registry_id
  global_registry_entity.parent.push_entity_to_global_registry_async
  raise GlobalRegistry::Bindings::ParentEntityMissingGlobalRegistryId,
    "#{model.class.name}(#{model.id}) has parent entity " \
    "#{global_registry_entity.parent.class.name}(#{global_registry_entity.parent.id}) missing " \
    "global_registry_id; will retry."
end

#entity_fingerprintObject



84
85
86
87
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 84

def entity_fingerprint
  @entity_fingerprint ||=
    Digest::MD5.hexdigest(Marshal.dump(model.entity_attributes_to_push&.except(:client_updated_at)))
end

#fingerprints_match?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 95

def fingerprints_match?
  # fingerprint never matches if id_value is missing (never been pushed to Global Registry)
  return false unless global_registry_entity.id_value?
  # fingerprint never matches if previous fingerprint is missing.
  old_fingerprint = model.send(global_registry_entity.fingerprint_column)
  return false if old_fingerprint.blank?
  return true if old_fingerprint == entity_fingerprint
  false
end

#push_entity_to_global_registryObject

rubocop:disable Metrics/PerceivedComplexity



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 12

def push_entity_to_global_registry # rubocop:disable Metrics/PerceivedComplexity
  # Don't push entity if fingerprint is defined and matches (nothing changed)
  return if global_registry_entity.fingerprint_column.present? && fingerprints_match?
  return if global_registry_entity.parent_required? && global_registry_entity.parent.blank?
  push_entity_type_to_global_registry

  if global_registry_entity.parent_type.present? && !global_registry_entity.parent_is_self?
    create_dependent_entity_in_global_registry
  elsif global_registry_entity.id_value?
    update_entity_in_global_registry
  else
    create_entity_in_global_registry
  end
end

#update_entity_in_global_registryObject



27
28
29
30
31
32
33
34
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 27

def update_entity_in_global_registry
  entity_attributes = {global_registry_entity.type => model.entity_attributes_to_push}
  GlobalRegistry::Entity.put(global_registry_entity.id_value, entity: entity_attributes)
  update_fingerprint
rescue RestClient::ResourceNotFound
  global_registry_entity.id_value = nil
  create_entity_in_global_registry
end

#update_fingerprintObject



89
90
91
92
93
# File 'lib/global_registry_bindings/entity/push_entity_methods.rb', line 89

def update_fingerprint
  return if global_registry_entity.fingerprint_column.blank?
  model.update_column(global_registry_entity.fingerprint_column, # rubocop:disable Rails/SkipsModelValidations
    entity_fingerprint)
end