Skip to content

Commit

Permalink
avoid multiple callbacks and persist only once
Browse files Browse the repository at this point in the history
  • Loading branch information
vkghub committed Jan 23, 2025
1 parent 98866bf commit 09e2bcc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app/domain/operations/families/update_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def build_relationship(person, family, relationship_kind)

existing_relationship = primary_person.person_relationships.where(kind: relationship_kind, relative_id: BSON::ObjectId(person.id.to_s)).first
return Success() if existing_relationship
primary_person.ensure_relationship_with(person, relationship_kind)

relationship = primary_person.build_or_assign_relationship_with(person, relationship_kind, {skip_relationship_updated_event_callback: true})
relationship&.save!

Success()
rescue StandardError => e
Failure("Relationship creation failed: #{e}")
Expand Down
11 changes: 7 additions & 4 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ def publish_updated_event
end

def person_create_or_update_handler
return if skip_person_updated_event_callback
::Operations::FinancialAssistance::PersonCreateOrUpdateHandler.new.call({person: self, event: :person_updated}) if ::EnrollRegistry.feature_enabled?(:financial_assistance)
rescue StandardError => e
Rails.logger.error {"FAA Engine: Unable to do action Operations::FinancialAssistance::PersonCreateOrUpdateHandler for person with object_id: #{self.id} due to #{e.message}"}
Expand Down Expand Up @@ -712,17 +713,19 @@ def ensure_relationship_with(person, relationship)
end
end

def build_or_update_relationship_with(person, relationship)
def build_or_assign_relationship_with(person, relationship, opts = {})
return if person.blank?
existing_relationship = self.person_relationships.detect do |rel|
rel.relative_id.to_s == person.id.to_s
end
if existing_relationship
existing_relationship.update_attributes(:kind => relationship)
existing_relationship.assign_attributes(:kind => relationship, :skip_relationship_updated_event_callback => opts[:skip_relationship_updated_event_callback])
existing_relationship
elsif id != person.id
self.person_relationships << PersonRelationship.new({
self.person_relationships.build({
:kind => relationship,
:relative_id => person.id
:relative_id => person.id,
:skip_relationship_updated_event_callback => opts[:skip_relationship_updated_event_callback]
})
end
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/person_relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class PersonRelationship
after_save :notify_updated
after_update :relationship_updated, if: :kind_changed?

attr_accessor :skip_relationship_updated_event_callback

def notify_updated
person.notify_updated
end
Expand All @@ -130,6 +132,7 @@ def notify_updated
# This is the reason why we have to call person_create_or_update_handler on relative
# to be able to notify FAA engine with the relationship change.
def relationship_updated
return if skip_relationship_updated_event_callback
relative&.person_create_or_update_handler
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,27 @@ def build_relationship(predecessor, successor, relationship_kind, destroy_relati

direct_relationship.assign_attributes(kind: relationship_kind)
elsif predecessor.id != successor.id
update_or_build_relationship(predecessor, successor, relationship_kind) # Direct Relationship
assign_or_build_relationship(predecessor, successor, relationship_kind) # Direct Relationship
end
end

def assign_or_build_relationship(applicant, relative, relation_kind)
return if applicant.blank? || relative.blank? || relation_kind.blank?
return if applicant == relative

relationship = relationships.where(applicant_id: applicant.id, relative_id: relative.id).first

if relationship.present?
# Update relationship object only if the existing RelationshipKind is different from the incoming RelationshipKind.
relationship.assign_attributes(kind: relation_kind) if relationship.kind != relation_kind
else
self.relationships.build(
{
kind: relation_kind,
applicant_id: applicant.id,
relative_id: relative.id
}
)
end
end

Expand Down

0 comments on commit 09e2bcc

Please sign in to comment.