Skip to content

Commit

Permalink
Merged in r2-2627-unlink-family-from-case (pull request #6542)
Browse files Browse the repository at this point in the history
R2-2627  - Unlink family from case
  • Loading branch information
dhernandez-quoin authored and pnabutovsky committed Oct 18, 2023
2 parents 3969e84 + b2d9508 commit c0a0cbf
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 29 deletions.
3 changes: 1 addition & 2 deletions app/models/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ def validate_date_of_birth
alias super_update_properties update_properties
def update_properties(user, data)
build_or_update_incidents(user, (data.delete('incident_details') || []))
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)
update_family_fields(data)
super_update_properties(user, data)
end

Expand Down
27 changes: 22 additions & 5 deletions app/models/concerns/family_linkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ module FamilyLinkable
before_save :update_family_members
after_save :associate_family_member
after_save :save_family
after_save :disassociate_family_member
end

def stamp_family_fields
return unless changes_to_save.key?('family_id')

self.family_id_display = family&.family_id_display
self.family_member_id = nil if family_id.nil?
end

def associate_to_family
Expand All @@ -38,11 +40,26 @@ def associate_family_member
end
end

def update_family_data(child_data)
def disassociate_family_member
return unless @family_to_disassociate.present?

@family_to_disassociate.family_members = @family_to_disassociate.family_members.map do |member|
next(member) unless member['case_id'] == id

member.merge('case_id' => nil, 'case_id_display' => nil)
end
@family_to_disassociate.save!
end

def update_family_fields(properties)
if properties.key?('family_id')
@family_to_disassociate = family if properties['family_id'].nil?
self.family_id = properties.delete('family_id')
end
return unless family.present?

changed_family_fields = FamilyLinkageService::GLOBAL_FAMILY_FIELDS & child_data.keys
changed_family_fields.each { |field| family.data[field] = child_data.delete(field) }
changed_family_fields = FamilyLinkageService::GLOBAL_FAMILY_FIELDS & properties.keys
changed_family_fields.each { |field| family.data[field] = properties.delete(field) }
end

def save_family
Expand Down Expand Up @@ -89,14 +106,14 @@ def family_members
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 = []
return field_names unless family.present?

field_names << 'family_details_section' if family.family_members_changed?
field_names += FamilyLinkageService::GLOBAL_FAMILY_FIELDS & family.saved_changes_to_record.keys
field_names
Expand Down
2 changes: 1 addition & 1 deletion app/models/exporters/base_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def export(records)

def embed_associated_data(record)
# If we need to embed other associated data we can add methods from the RecordDataService in this class.
record.data = record_data_service.embed_family_info(record.data, record, field_names)
record.data = record_data_service.embed_family_info(record.data, record, field_names || [])
end

def intialize_services
Expand Down
6 changes: 0 additions & 6 deletions app/services/family_linkage_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ def child_to_family(child)
GLOBAL_FAMILY_FIELDS.each_with_object({}) { |field, memo| memo[field] = child.data[field] }
end

def family_to_child(family)
return {} unless family.present?

GLOBAL_FAMILY_FIELDS.each_with_object({}) { |field, memo| memo[field] = family.data[field] }
end

def global_family_member_data(family_member)
family_member.except(*LOCAL_FAMILY_MEMBER_FIELDS)
end
Expand Down
11 changes: 6 additions & 5 deletions app/services/record_data_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def embed_registry_record_info(data, record, selected_field_names)
end

def embed_family_info(data, record, selected_field_names, user = nil)
return data unless record.is_a?(Child) && record.family_id.present?
return data unless record.is_a?(Child)

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')
Expand All @@ -105,17 +105,18 @@ def embed_family_info(data, record, selected_field_names, user = nil)
end

def embed_family_details(data, record, selected_field_names)
family_details = FamilyLinkageService.family_to_child(record.family)
field_names = selected_field_names & FamilyLinkageService::GLOBAL_FAMILY_FIELDS
field_names.each { |field_name| data[field_name] = family_details[field_name] }
field_names.each do |field_name|
data[field_name] = record.family.present? ? record.family.data[field_name] : data[field_name]
end
data
end

def embed_family_details_section(data, record, selected_field_names, user)
return data unless selected_field_names.include?('family_details_section')

data['family_details_section'] = calculate_family_member_record_user_access(
record.family_members_details, record.family.cases_grouped_by_id, user
record.family_members_details, record.family&.cases_grouped_by_id, user
)
data
end
Expand Down Expand Up @@ -174,7 +175,7 @@ def calculate_family_member_record_user_access(family_members, cases_grouped_by_
return family_members if family_members.blank? || user.blank?

family_members.each do |family_member|
family_member_record = cases_grouped_by_id[family_member['case_id']]&.first
family_member_record = cases_grouped_by_id&.dig(family_member['case_id'])&.first
next if family_member_record.blank?

family_member['can_read_record'] = user.can_read_record?(
Expand Down
14 changes: 14 additions & 0 deletions spec/models/concerns/family_linkable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@
)
end
end

describe 'disassociate_from_family' do
it 'disassociates a case from a family record' do
child.update_properties(user, { 'family_id' => nil })
child.save!

family2.reload

expect(child.family).to be_nil
expect(child.family_member_id).to be_nil
expect(family2.cases).to be_empty
expect(family2.family_members.find { |member| member['case_id'] == child.id }).to be_nil
end
end
end
21 changes: 21 additions & 0 deletions spec/requests/api/v2/children_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,27 @@
expect(@family5.family_members[1]['case_id']).to eq(@case10.id)
expect(@family5.family_members[1]['case_id_display']).to eq(@case10.case_id_display)
end

it 'unlinks a family record from a record' do
login_for_test(
permissions: [
Permission.new(resource: Permission::CASE, actions: [Permission::WRITE, Permission::LINK_FAMILY_RECORD])
]
)
params = { data: { family_id: nil } }

patch "/api/v2/cases/#{@case8.id}", params:, as: :json

@family2.reload

expect(response).to have_http_status(200)
expect(json['data']['id']).not_to be_empty
expect(json['data']['family_id']).to be_nil
expect(json['data']['family_member_id']).to be_nil
expect(json['data']['family_details_section']).to be_empty
expect(@family2.cases).to be_empty
expect(@family2.family_members.find { |member| member['case_id'] == @case8.id }).to be_nil
end
end

describe 'DELETE /api/v2/cases/:id' do
Expand Down
10 changes: 0 additions & 10 deletions spec/services/family_linkage_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@
end
end

describe 'family_to_child' do
it 'returns the family details for a child' do
family_details = FamilyLinkageService.family_to_child(family1)

expect(family_details['family_number']).to eq('40bf9109')
expect(family_details['family_size']).to eq(1)
expect(family_details['family_notes']).to eq('Notes about the family')
end
end

describe 'new_family_linked_child' do
context 'when the child is not linked to a family' do
it 'returns a case linked to a family with the cases as members' do
Expand Down

0 comments on commit c0a0cbf

Please sign in to comment.