Skip to content

Commit

Permalink
Merged in r2-2591-field-mapping-split-name (pull request #6490)
Browse files Browse the repository at this point in the history
R2-2591 - Update name field mapping for creating case from family details / family member subforms
  • Loading branch information
dhernandez-quoin authored and pnabutovsky committed Sep 11, 2023
2 parents 0d7d650 + 0412fb5 commit 09298c2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
30 changes: 27 additions & 3 deletions app/services/family_linkage_service.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -140,3 +163,4 @@ def family_details_section_local_data(family_details_section)
end
end
end
# rubocop:enable Metrics/ClassLength
4 changes: 2 additions & 2 deletions spec/requests/api/v2/families_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
66 changes: 61 additions & 5 deletions spec/services/family_linkage_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 09298c2

Please sign in to comment.