Skip to content

Commit

Permalink
Update according to join pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
martha committed Feb 5, 2025
1 parent ddcfd11 commit 0ea4dd6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
28 changes: 10 additions & 18 deletions drivers/hmis/app/graphql/mutations/split_household.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,49 @@ def resolve(splitting_enrollment_inputs:)
where(id: splitting_enrollment_ids).
viewable_by(current_user).
includes(:household, :project)
access_denied! unless splitting_enrollments.count == splitting_enrollment_inputs.count
access_denied! unless splitting_enrollments.size == splitting_enrollment_inputs.size

project = splitting_enrollments.map(&:project).uniq.sole
access_denied! unless current_permission?(permission: :can_split_households, entity: project)

donor_household = splitting_enrollments.map(&:household).uniq.sole
remaining_enrollments = Hmis::Hud::Enrollment.
viewable_by(current_user).
where(household_id: donor_household.household_id).
where.not(id: splitting_enrollment_ids)
remaining_hoh = remaining_enrollments.any? { |enrollment| enrollment.relationship_to_hoh == 1 }
remaining_enrollments = donor_household.enrollments.where.not(id: splitting_enrollment_ids)

raise 'Splitting all clients to a new household is invalid' if remaining_enrollments.empty?
raise 'This operation would leave behind a household with no HoH, which is not allowed' unless remaining_hoh
raise 'This operation would leave behind a household with no HoH, which is not allowed' unless remaining_enrollments.exists?(relationship_to_hoh: 1)

donor_before_state = Hmis::Hud::Enrollment.snapshot_enrollments([*splitting_enrollments, *remaining_enrollments])
donor_before_state = donor_household.snapshot_household_state
new_household_id = Hmis::Hud::Base.generate_uuid

Hmis::Hud::Enrollment.transaction do
splitting_enrollments.each do |enrollment|
enrollment.update!(
household_id: new_household_id,
relationship_to_hoh: map_enrollment_id_to_relationship[enrollment.id.to_s],
)
enrollment.household_id = new_household_id
enrollment.relationship_to_hoh = map_enrollment_id_to_relationship[enrollment.id.to_s]

enrollment.active_unit_occupancy&.assign_attributes(occupancy_period_attributes: { end_date: Date.current })

enrollment.save!
end

donor_household.reload
donor_after_state = donor_household.snapshot_household_state

event = Hmis::HouseholdEvent.new
event.user = current_user
event.household = donor_household
event.event_type = Hmis::HouseholdEvent::SPLIT
event.event_details = {
'receivingHouseholdId': new_household_id,
'receiving_household_id': new_household_id,
'before': donor_before_state,
'after': Hmis::Hud::Enrollment.snapshot_enrollments(remaining_enrollments),
'after': donor_after_state,
}
event.save!

remaining_enrollments.invalidate_processing!
end

enrollment = splitting_enrollments.first
enrollment.reload

{
new_household: enrollment.household,
new_household: splitting_enrollments.first.reload.household,
remaining_household: donor_household,
}
end
Expand Down
6 changes: 3 additions & 3 deletions drivers/hmis/spec/requests/hmis/split_household_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def perform_mutation(
split_event = remaining.household.events.sole
expect(split_event.event_type).to eq('split')
dets = split_event.event_details
expect(dets['receivingHouseholdId']).to eq(new_hoh.household_id)
expect(dets['before'].map { |enrollment_snap| enrollment_snap['enrollmentId'] }).to contain_exactly(remaining.id, new_hoh.id, child.id)
expect(dets['after'].map { |enrollment_snap| enrollment_snap['enrollmentId'] }).to contain_exactly(remaining.id)
expect(dets['receiving_household_id']).to eq(new_hoh.household_id)
expect(dets['before'].map { |enrollment_snap| enrollment_snap['enrollment_id'] }).to contain_exactly(remaining.id, new_hoh.id, child.id)
expect(dets['after'].map { |enrollment_snap| enrollment_snap['enrollment_id'] }).to contain_exactly(remaining.id)
end

it 'fails when the user does not have can_split_households permission' do
Expand Down

0 comments on commit 0ea4dd6

Please sign in to comment.