diff --git a/app/services/family_linkage_service.rb b/app/services/family_linkage_service.rb index 2cf16275bc..48a39ba652 100644 --- a/app/services/family_linkage_service.rb +++ b/app/services/family_linkage_service.rb @@ -1,6 +1,7 @@ # 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 @@ -58,8 +59,9 @@ def link_child_to_new_family(user, child) def family_member_to_child(user, family_member) child_data = DEFAULT_MAPPING.each_with_object({}) do |elem, memo| - if elem['target'].is_a?(Array) - elem['target'].each { |target| memo[target] = family_member[elem['source']] } + if elem['source'] == 'relation_name' + child_names = relation_name_to_child_names(family_member) + elem['target'].each { |target| memo[target] = child_names[target] } else memo[elem['target']] = family_member[elem['source']] end @@ -70,11 +72,32 @@ def family_member_to_child(user, family_member) def child_to_family_member(child) DEFAULT_MAPPING.each_with_object({ 'unique_id' => SecureRandom.uuid }) do |elem, memo| - target = elem['target'].is_a?(Array) ? elem['target'].first : elem['target'] + target = elem['target'] + if elem['source'] == 'relation_name' + memo[elem['source']] = generate_relation_name(child, target) + next + end + memo[elem['source']] = child.data[target] end end + def generate_relation_name(child, field_names) + child.data['name'].presence || field_names.map { |field_name| child.data[field_name] }.compact.join(' ') + end + + def relation_name_to_child_names(family_member) + relation_name = family_member['relation_name'] + return {} unless relation_name.present? + + names = relation_name.split + { + 'name_first' => names.first, + 'name_middle' => names.slice(1..-2).join(' ').presence, + 'name_last' => names.size > 1 ? names.last : nil + } + end + def child_to_family(child) GLOBAL_FAMILY_FIELDS.each_with_object({}) { |field, memo| memo[field] = child.data[field] } end @@ -140,3 +163,4 @@ def family_details_section_local_data(family_details_section) end end end +# rubocop:enable Metrics/ClassLength diff --git a/spec/requests/api/v2/families_controller_spec.rb b/spec/requests/api/v2/families_controller_spec.rb index 12bedfa413..c7e3c86cbe 100644 --- a/spec/requests/api/v2/families_controller_spec.rb +++ b/spec/requests/api/v2/families_controller_spec.rb @@ -19,7 +19,7 @@ family_members: [ { unique_id: '001', - relation_name: 'Member 1', + relation_name: 'Member1', relation_sex: 'male', relation_age: 10, relation_date_of_birth: Date.today - 10.years @@ -220,7 +220,7 @@ expect(json['data']['id']).to eq(family1.id) child = Child.find_by(id: json['data']['record']['id']) - expect(child.name_first).to eq('Member 1') + expect(child.name_first).to eq('Member1') expect(child.sex).to eq('male') expect(child.age).to eq(10) expect(child.date_of_birth).to eq(Date.today - 10.years) diff --git a/spec/services/family_linkage_service_spec.rb b/spec/services/family_linkage_service_spec.rb index b4c4673007..e13b938e36 100644 --- a/spec/services/family_linkage_service_spec.rb +++ b/spec/services/family_linkage_service_spec.rb @@ -23,7 +23,7 @@ family_relationship: 'relationship1', family_relationship_notes: 'Notes about the relationship', family_relationship_notes_additional: 'Additional notes about the relationship', - relation_name: 'Member 1', + relation_name: 'MemberFirst MemberLast', relation_nickname: 'Member 1 Nickname', relation_sex: 'male', relation_age: 10, @@ -98,9 +98,9 @@ expect(Child.find(child.id).data.select { |key, _value| target_fields.include?(key) }).to eq( { - 'name_first' => 'Member 1', - 'name_middle' => 'Member 1', - 'name_last' => 'Member 1', + 'name_first' => 'MemberFirst', + 'name_middle' => nil, + 'name_last' => 'MemberLast', 'name_nickname' => 'Member 1 Nickname', 'sex' => 'male', 'age' => 10, @@ -123,6 +123,62 @@ end end + describe 'family_member_to_child' do + it 'splits the relation name of the family member' do + child = FamilyLinkageService.family_member_to_child( + user, { 'unique_id' => '001', 'relation_name' => 'MemberFirst MemberLast' } + ) + + expect(child.name_first).to eq('MemberFirst') + expect(child.name_middle).to be_nil + expect(child.name_last).to eq('MemberLast') + end + + it 'splits the relation name of the family member with middle name' do + child = FamilyLinkageService.family_member_to_child( + user, { 'unique_id' => '001', 'relation_name' => 'MemberFirst MemberMiddle MemberLast' } + ) + + expect(child.name_first).to eq('MemberFirst') + expect(child.name_middle).to eq('MemberMiddle') + expect(child.name_last).to eq('MemberLast') + end + + it 'splits the relation name of the family member with a long middle name' do + child = FamilyLinkageService.family_member_to_child( + user, { 'unique_id' => '001', 'relation_name' => 'MemberFirst MemberMiddle1 MemberMiddle2 MemberMiddle3 MemberLast' } + ) + + expect(child.name_first).to eq('MemberFirst') + expect(child.name_middle).to eq('MemberMiddle1 MemberMiddle2 MemberMiddle3') + expect(child.name_last).to eq('MemberLast') + end + + it 'splits the relation name of the family member but only sets the first name' do + child = FamilyLinkageService.family_member_to_child( + user, { 'unique_id' => '001', 'relation_name' => 'MemberFirst' } + ) + + expect(child.name_first).to eq('MemberFirst') + expect(child.name_middle).to be_nil + expect(child.name_last).to be_nil + end + end + + describe 'child_to_family_member' do + it 'sets the relation_name from the name fields' do + child = Child.create!(data: { name_first: 'FirstName', name_last: 'LastName', age: 10, sex: 'male' }) + family_member = FamilyLinkageService.child_to_family_member(child) + expect(family_member['relation_name']).to eq('FirstName LastName') + end + + it 'sets the relation_name from the name field' do + child = Child.create!(data: { name: 'FirstName LastName', age: 10, sex: 'male' }) + family_member = FamilyLinkageService.child_to_family_member(child) + expect(family_member['relation_name']).to eq('FirstName LastName') + end + end + describe 'family_to_child' do it 'returns the family details for a child' do family_details = FamilyLinkageService.family_to_child(family1) @@ -141,7 +197,7 @@ [ { 'unique_id' => '001', - 'relation_name' => 'Member 1', + 'relation_name' => 'MemberFirst MemberLast', 'relation_nickname' => 'Member 1 Nickname', 'relation_sex' => 'male', 'relation_age' => 10,