diff --git a/app/models/child.rb b/app/models/child.rb index 2bc2b378d7..1ac8b132dd 100644 --- a/app/models/child.rb +++ b/app/models/child.rb @@ -246,29 +246,6 @@ def save_incidents end end - def update_family(case_data) - family.family_number = case_data['family_number'] if case_data.key?('family_number') - - update_family_members(case_data.delete('family_details_section') || []) - end - - def update_family_members(family_details_section_data) - return unless family_details_section_data.present? - - @family_members = FamilyLinkageService.build_or_update_family_members( - family_details_section_data, - family.family_members || [] - ) - self.family_details_section = FamilyLinkageService.family_details_section_local_data(family_details_section_data) - end - - def save_family - return unless family.present? - - family.family_members = @family_members if @family_members.present? - family.save! if family.has_changes_to_save? - end - def to_s name.present? ? "#{name} (#{unique_identifier})" : unique_identifier end diff --git a/app/models/concerns/family_linkable.rb b/app/models/concerns/family_linkable.rb index ed0e4e1374..4e6aa29ae1 100644 --- a/app/models/concerns/family_linkable.rb +++ b/app/models/concerns/family_linkable.rb @@ -12,6 +12,29 @@ module FamilyLinkable after_save :save_family end + def update_family(record_data) + family.family_number = record_data['family_number'] if record_data.key?('family_number') + + update_family_members(record_data.delete('family_details_section') || []) + end + + def update_family_members(family_details_section_data) + return unless family_details_section_data.present? + + @family_members = FamilyLinkageService.build_or_update_family_members( + family_details_section_data, + family.family_members || [] + ) + self.family_details_section = FamilyLinkageService.family_details_section_local_data(family_details_section_data) + end + + def save_family + return unless family.present? + + family.family_members = @family_members if @family_members.present? + family.save! if family.has_changes_to_save? + end + def stamp_family_fields return unless changes_to_save.key?('family_id') diff --git a/app/services/family_linkage_service.rb b/app/services/family_linkage_service.rb index 1916664240..d0991d403a 100644 --- a/app/services/family_linkage_service.rb +++ b/app/services/family_linkage_service.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true # Generates a case from the family +# rubocop:disable Metrics/ClassLength class FamilyLinkageService LOCAL_FAMILY_MEMBER_FIELDS = %w[ - family_relationship family_relationship_notes family_relationship_notes_additional + family_relationship family_relation_is_caregiver family_relationship_notes family_relationship_notes_additional ].freeze LOCAL_FAMILY_DETAIL_FIELDS = %w[ relation @@ -18,6 +19,10 @@ class FamilyLinkageService ].freeze LOCAL_FAMILY_FIELDS = %w[family_notes_additional].freeze GLOBAL_FAMILY_FIELDS = %w[family_number family_size family_notes].freeze + FAMILY_DETAIL_MAPPING = [ + { source: 'relation', target: 'family_relationship' }, + { source: 'relation_is_caregiver', target: 'family_relation_is_caregiver' } + ].map(&:with_indifferent_access).freeze DEFAULT_MAPPING = [ { source: 'relation_name', target: %w[name_first name_middle name_last] }, { source: 'relation_nickname', target: 'name_nickname' }, @@ -46,9 +51,9 @@ def new_family_linked_child(user, source_case, family_detail_id) end def link_child_to_new_family(user, child) - family = Family.new_with_user(user, FamilyLinkageService.child_to_family(child)) + family = Family.new_with_user(user, child_to_family(child)) family.module_id = child.module_id - family.family_members = FamilyLinkageService.build_or_update_family_members(child.family_details_section, []) + family.family_members = build_or_update_family_members(child.family_details_section, []) family_member = child_to_family_member(child) family.family_members << family_member @@ -132,7 +137,7 @@ def build_family_members(family_details_section, family_members) family_details_section.each_with_object([]) do |detail, memo| next unless family_member_ids.exclude?(detail['unique_id']) - memo << global_family_detail_data(detail) + memo << family_detail_to_family_member(detail) end end @@ -145,8 +150,17 @@ def update_family_members(family_details_section, family_members) end end + def family_detail_to_family_member(family_detail) + FAMILY_DETAIL_MAPPING.each_with_object(global_family_detail_data(family_detail)) do |elem, memo| + next unless family_detail[elem['source']].present? + + memo[elem['target']] = family_detail[elem['source']] + end + end + def family_details_section_local_data(family_details_section) family_details_section.map { |family_detail| local_family_detail_data(family_detail) } end end end +# rubocop:enable Metrics/ClassLength diff --git a/spec/requests/api/v2/children_controller_spec.rb b/spec/requests/api/v2/children_controller_spec.rb index 22df9466b7..2bf8a321d1 100644 --- a/spec/requests/api/v2/children_controller_spec.rb +++ b/spec/requests/api/v2/children_controller_spec.rb @@ -931,7 +931,13 @@ data: { family_number: '002', family_details_section: [ - { unique_id: member2_unique_id, relation_age: 2, relation: 'relation3', relation_name: 'Member 3' } + { + unique_id: member2_unique_id, + relation_age: 2, + relation: 'relation3', + relation_name: 'Member 3', + relation_is_caregiver: true + } ] } } @@ -955,7 +961,8 @@ 'unique_id' => member2_unique_id, 'relation' => 'relation3', 'relation_name' => 'Member 3', - 'relation_age' => 2 + 'relation_age' => 2, + 'relation_is_caregiver' => true } ] ) @@ -964,7 +971,8 @@ [ { 'unique_id' => member2_unique_id, - 'relation' => 'relation3' + 'relation' => 'relation3', + 'relation_is_caregiver' => true } ] ) @@ -988,7 +996,9 @@ { 'unique_id' => member2_unique_id, 'relation_name' => 'Member 3', - 'relation_age' => 2 + 'relation_age' => 2, + 'family_relationship' => 'relation3', + 'family_relation_is_caregiver' => true } ] ) diff --git a/spec/support/fake_devise_login.rb b/spec/support/fake_devise_login.rb index 4ed54ad396..31eff2fd98 100644 --- a/spec/support/fake_devise_login.rb +++ b/spec/support/fake_devise_login.rb @@ -19,7 +19,8 @@ module FakeDeviseLogin Field.new(name: 'relation_name', type: Field::TEXT_FIELD), Field.new(name: 'relation', type: Field::SELECT_BOX), Field.new(name: 'relation_type', type: Field::SELECT_BOX), - Field.new(name: 'relation_age', type: Field::NUMERIC_FIELD) + Field.new(name: 'relation_age', type: Field::NUMERIC_FIELD), + Field.new(name: 'relation_is_caregiver', type: Field::TICK_BOX) ] ) ),