Skip to content

Commit

Permalink
Update PR from feedback on mutation (still wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
martha committed Feb 3, 2025
1 parent f767ad6 commit a36a1a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
28 changes: 10 additions & 18 deletions drivers/hmis/app/graphql/mutations/join_household.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,26 @@ class JoinHousehold < BaseMutation
field :donor_enrollment, Types::HmisSchema::Enrollment, null: true # Will be null if there are no remaining members

def resolve(receiving_household_id:, joining_enrollment_inputs:)
receiving_enrollments = Hmis::Hud::Enrollment.
where(household_id: receiving_household_id).
viewable_by(current_user).
includes(:project, :household)
access_denied! if receiving_enrollments.empty?

receiving_household = receiving_enrollments.first.household

receiving_project = receiving_enrollments.map(&:project).uniq.sole
# note: viewable_by takes care of filtering by data source via project with_access
receiving_household = Hmis::Hud::Household.viewable_by(current_user).find_by(household_id: receiving_household_id)
access_denied! unless receiving_household
receiving_enrollments = receiving_household.enrollments
receiving_project = receiving_household.project
access_denied! unless current_permission?(permission: :can_split_households, entity: receiving_project)

# The mutation input is a list, so convert it to a hashmap for ease of access
map_enrollment_id_to_relationship = joining_enrollment_inputs.map { |e| [e.enrollment_id, e.relationship_to_hoh] }.to_h

joining_enrollment_ids = map_enrollment_id_to_relationship.keys
joining_enrollments = Hmis::Hud::Enrollment.
viewable_by(current_user).
where(id: joining_enrollment_ids).
includes(:household)
where(project: receiving_project). # can only join from household in same project
where(id: joining_enrollment_ids)
access_denied! unless joining_enrollments.count == joining_enrollment_inputs.count
raise 'Cannot join from multiple households' unless joining_enrollments.map(&:household_id).uniq.size == 1

raise 'Cannot merge enrollments from another project' unless joining_enrollments.pluck(:project_pk).uniq.sole == receiving_project.id
donor_household = joining_enrollments.first.household

donor_household = joining_enrollments.map(&:household).uniq.sole
remaining_enrollments = Hmis::Hud::Enrollment.
viewable_by(current_user). # Restrict to this data source
where(household_id: donor_household.household_id).
where.not(id: joining_enrollment_ids)
remaining_enrollments = donor_household.enrollments - joining_enrollments
remaining_hoh = remaining_enrollments.find { |enrollment| enrollment.relationship_to_hoh == 1 }
raise 'This operation would leave behind a household with no HoH, which is not allowed' unless remaining_enrollments.empty? || !!remaining_hoh

Expand Down
2 changes: 1 addition & 1 deletion drivers/hmis/spec/requests/hmis/join_household_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def perform_mutation(
},
],
}
expect_gql_error post_graphql(input: input) { mutation }, message: /Cannot merge enrollments from another project/
expect_access_denied post_graphql(input: input) { mutation }
end

it 'fails when the join would leave behind a headless household' do
Expand Down

0 comments on commit a36a1a8

Please sign in to comment.