Skip to content

Commit

Permalink
R2-2616 - Copy relationship fields from family details to family memb…
Browse files Browse the repository at this point in the history
…er on family creation
  • Loading branch information
dhernandez-quoin committed Sep 20, 2023
1 parent 80a5dc5 commit 92758c8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
23 changes: 0 additions & 23 deletions app/models/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions app/models/concerns/family_linkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
22 changes: 18 additions & 4 deletions app/services/family_linkage_service.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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' },
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
18 changes: 14 additions & 4 deletions spec/requests/api/v2/children_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
}
Expand All @@ -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
}
]
)
Expand All @@ -964,7 +971,8 @@
[
{
'unique_id' => member2_unique_id,
'relation' => 'relation3'
'relation' => 'relation3',
'relation_is_caregiver' => true
}
]
)
Expand All @@ -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
}
]
)
Expand Down
3 changes: 2 additions & 1 deletion spec/support/fake_devise_login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
)
),
Expand Down

0 comments on commit 92758c8

Please sign in to comment.