diff --git a/app/controllers/api/v2/children_controller.rb b/app/controllers/api/v2/children_controller.rb index c869ef4ff9..1bb13700f3 100644 --- a/app/controllers/api/v2/children_controller.rb +++ b/app/controllers/api/v2/children_controller.rb @@ -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 diff --git a/app/models/child.rb b/app/models/child.rb index 41f1833528..9b93bc15b0 100644 --- a/app/models/child.rb +++ b/app/models/child.rb @@ -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 @@ -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 diff --git a/app/models/concerns/family_linkable.rb b/app/models/concerns/family_linkable.rb index 7e1f07ae18..2c93a800f4 100644 --- a/app/models/concerns/family_linkable.rb +++ b/app/models/concerns/family_linkable.rb @@ -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 @@ -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 @@ -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 diff --git a/app/services/record_data_service.rb b/app/services/record_data_service.rb index ed9b0f7740..a5fa8f3ea3 100644 --- a/app/services/record_data_service.rb +++ b/app/services/record_data_service.rb @@ -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 diff --git a/spec/requests/api/v2/children_controller_spec.rb b/spec/requests/api/v2/children_controller_spec.rb index 8d7c29536e..bb2603e49d 100644 --- a/spec/requests/api/v2/children_controller_spec.rb +++ b/spec/requests/api/v2/children_controller_spec.rb @@ -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', @@ -973,6 +975,8 @@ params = { data: { family_number: '002', + family_size: 5, + family_notes: 'Family002Notes', family_details_section: [ { unique_id: member2_unique_id, @@ -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( [ { @@ -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( [ { @@ -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]) @@ -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) diff --git a/spec/support/fake_devise_login.rb b/spec/support/fake_devise_login.rb index 31eff2fd98..568ad7da5a 100644 --- a/spec/support/fake_devise_login.rb +++ b/spec/support/fake_devise_login.rb @@ -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