Skip to content

Commit

Permalink
call notify_updated event when record is changed (#2781)
Browse files Browse the repository at this point in the history
  • Loading branch information
nks2109 authored Sep 27, 2024
1 parent 5cd0183 commit 87aca51
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
10 changes: 9 additions & 1 deletion app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class Person
validate :consumer_fields_validations

after_create :notify_created
after_update :notify_updated, if: :changed?
after_update :notify_updated, if: :attributes_changed?

def active_general_agency_staff_roles
general_agency_staff_roles.select(&:active?)
Expand Down Expand Up @@ -287,6 +287,7 @@ def notify_created
end

def notify_updated
Rails.logger.info "person update event is getting triggered"
notify(PERSON_UPDATED_EVENT_NAME, {:individual_id => hbx_id })
end

Expand Down Expand Up @@ -899,6 +900,13 @@ def set_ridp_for_paper_application(session_var)

private

def attributes_changed?
changes = changed_attributes.stringify_keys
meaningful_changes = changes.except('updated_at', 'updated_by', 'updated_by_id')
Rails.logger.info "Meaningful changes: #{meaningful_changes}"
meaningful_changes.present?
end

def update_census_dependent_relationship(existing_relationship)
return unless existing_relationship.valid?

Expand Down
28 changes: 22 additions & 6 deletions spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -735,27 +735,43 @@ def create_encrypted_ssn_uniqueness_index
context "notify change event" do
let(:person) { FactoryBot.build(:person) }

it "when new record" do
it "calls notify_created when new record is saved" do
expect(person).to receive(:notify_created)

person.save
end

it "when change record" do
first_name = person.first_name
it "calls notify_updated when existing record is changed" do
person.save
allow(person).to receive(:notify_updated)

person.first_name = "Test"
person.save

expect(person).to have_received(:notify_updated)
end

it "calls notify_updated when record is changed" do
it "calls notify_updated when meaningful changes are made" do
person = FactoryBot.create(:person)
allow(person).to receive(:notify_updated).and_call_original
allow(person).to receive(:notify_updated)

person.first_name = "NewName"
person.save

expect(person).to have_received(:notify_updated)
end

it "does not call notify_updated when record is not changed" do
it "does not call notify_updated when only non-meaningful attributes are changed" do
person = FactoryBot.create(:person)
allow(person).to receive(:notify_updated)

person.updated_at = Time.now
person.save

expect(person).not_to have_received(:notify_updated)
end

it "does not call notify_updated when no attributes are changed" do
person = FactoryBot.create(:person)
allow(person).to receive(:notify_updated)

Expand Down

0 comments on commit 87aca51

Please sign in to comment.