Skip to content

Commit

Permalink
Merged in r2-2637-global-fields-percolate (pull request #6535)
Browse files Browse the repository at this point in the history
R2-2637 - Changes to family_size and family_notes not percolating up to family record.
  • Loading branch information
dhernandez-quoin authored and pnabutovsky committed Oct 10, 2023
2 parents 58228c7 + d881e4a commit a8e073a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v2/children_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def traces
alias select_updated_fields_super select_updated_fields
def select_updated_fields
changes = @record.saved_changes_to_record.keys
@updated_field_names = select_updated_fields_super + @record.current_care_arrangements_changes(changes)
@updated_field_names << 'family_details_section' if @record.family&.family_members_changed?
@updated_field_names = select_updated_fields_super + @record.current_care_arrangements_changes(changes) +
@record.family_changes(changes)
end

def create_family
Expand Down
7 changes: 7 additions & 0 deletions app/models/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def update_properties(user, data)
self.family_id = data.delete('family_id') if data.key?('family_id')
self.registry_record_id = data.delete('registry_record_id') if data.key?('registry_record_id')
self.mark_for_reopen = @incidents_to_save.present?
update_family_data(data)
super_update_properties(user, data)
end

Expand Down Expand Up @@ -284,6 +285,12 @@ def display_id
case_id_display
end

def family_number
return super unless family.present?

family.family_number
end

def day_of_birth
return nil unless date_of_birth.is_a? Date

Expand Down
27 changes: 22 additions & 5 deletions app/models/concerns/family_linkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ module FamilyLinkable
before_save :associate_to_family
before_save :update_family_members
after_save :associate_family_member
after_save :update_family
after_save :save_family
end

def stamp_family_fields
return unless changes_to_save.key?('family_id')

self.family_id_display = family&.family_id_display
self.family_number = family&.family_number
end

def associate_to_family
Expand All @@ -39,11 +38,15 @@ def associate_family_member
end
end

def update_family
def update_family_data(child_data)
return unless family.present?

family.family_number = family_number if saved_changes_to_record.keys.include?('family_number')
return unless family.has_changes_to_save?
changed_family_fields = FamilyLinkageService::GLOBAL_FAMILY_FIELDS & child_data.keys
changed_family_fields.each { |field| family.data[field] = child_data.delete(field) }
end

def save_family
return unless family.present? && family.has_changes_to_save?

family.save!
end
Expand Down Expand Up @@ -84,4 +87,18 @@ def find_family_detail(family_detail_id)
def family_members
(family&.family_members || []).reject { |member| member['unique_id'] == family_member_id }
end

def family_changes(changes)
return [] unless family.present?

changes ||= saved_changes_to_record.keys
if changes.include?('family_id_display')
return FamilyLinkageService::GLOBAL_FAMILY_FIELDS + ['family_details_section']
end

field_names = []
field_names << 'family_details_section' if family.family_members_changed?
field_names += FamilyLinkageService::GLOBAL_FAMILY_FIELDS & family.saved_changes_to_record.keys
field_names
end
end
1 change: 0 additions & 1 deletion app/services/record_data_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def embed_family_info(data, record, selected_field_names)

data['family_id'] = record.family_id if selected_field_names.include?('family_id')
data['family_member_id'] = record.family_member_id if selected_field_names.include?('family_member_id')
data['family_number'] = record.family_number if selected_field_names.include?('family_number')
data = embed_family_details(data, record, selected_field_names)
embed_family_details_section(data, record, selected_field_names)
end
Expand Down
15 changes: 13 additions & 2 deletions spec/requests/api/v2/children_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@
}
)
@family3 = Family.create!(data: { family_number: '003', family_name: 'FamilyTest' })
@family4 = Family.create!(data: { family_number: '004', family_name: 'FamilyTest2' })
@family4 = Family.create!(
data: { family_number: '004', family_size: 2, family_notes: 'NotesFamilyTest2', family_name: 'FamilyTest2' }
)
@family5 = Family.create!(
data: {
family_number: '005',
Expand Down Expand Up @@ -973,6 +975,8 @@
params = {
data: {
family_number: '002',
family_size: 5,
family_notes: 'Family002Notes',
family_details_section: [
{
unique_id: member2_unique_id,
Expand All @@ -992,6 +996,8 @@

expect(response).to have_http_status(200)
expect(json['data']['family_number']).to eq('002')
expect(json['data']['family_size']).to eq(5)
expect(json['data']['family_notes']).to eq('Family002Notes')
expect(json['data']['family_details_section']).to eq(
[
{
Expand Down Expand Up @@ -1020,6 +1026,8 @@
]
)
expect(family.family_number).to eq('002')
expect(family.family_size).to eq(5)
expect(family.family_notes).to eq('Family002Notes')
expect(family.family_members).to eq(
[
{
Expand Down Expand Up @@ -1092,7 +1100,7 @@
end
end

it 'links an existing record to a family' do
it 'links an existing record to a family and returns global fields' do
login_for_test(
permissions: [
Permission.new(resource: Permission::CASE, actions: [Permission::WRITE, Permission::LINK_FAMILY_RECORD])
Expand All @@ -1107,6 +1115,9 @@
expect(response).to have_http_status(200)
expect(json['data']['id']).not_to be_empty
expect(json['data']['family_id']).to eq(@family4.id)
expect(json['data']['family_number']).to eq(@family4.family_number)
expect(json['data']['family_size']).to eq(@family4.family_size)
expect(json['data']['family_notes']).to eq(@family4.family_notes)
expect(json['data']['family_member_id']).to eq(@family4.family_members[0]['unique_id'])
expect(@family4.family_members[0]['unique_id']).not_to be_empty
expect(@family4.family_members[0]['relation_name']).to eq(@case9.name)
Expand Down
6 changes: 4 additions & 2 deletions spec/support/fake_devise_login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ module FakeDeviseLogin
]
)
),
Field.new(name: 'registry_type', type: 'text_field', display_name_en: 'Registry Type'),
Field.new(name: 'family_number', type: 'text_field', display_name_en: 'Family Number')
Field.new(name: 'registry_type', type: Field::TEXT_FIELD, display_name_en: 'Registry Type'),
Field.new(name: 'family_number', type: Field::TEXT_FIELD, display_name_en: 'Family Number'),
Field.new(name: 'family_notes', type: Field::TEXT_FIELD, display_name_en: 'Family Notes'),
Field.new(name: 'family_size', type: Field::NUMERIC_FIELD, display_name_en: 'Family Size')
].freeze

def permission_case
Expand Down

0 comments on commit a8e073a

Please sign in to comment.