-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdelete_client.rb
84 lines (71 loc) · 2.91 KB
/
delete_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
###
# Copyright 2016 - 2025 Green River Data Analysis, LLC
#
# License detail: https://github.com/greenriver/hmis-warehouse/blob/production/LICENSE.md
###
module Mutations
class DeleteClient < BaseMutation
argument :id, ID, required: true
argument :client_lock_version, Integer, required: false
argument :confirmed, Boolean, 'Whether warnings have been confirmed', required: false
field :client, Types::HmisSchema::Client, null: true
def self.problem_enrollments_message(count)
"If this client is deleted, #{count} #{'household'.pluralize(count)} will have no Head of Household."
end
def resolve(id:, client_lock_version: nil, confirmed: false)
client = Hmis::Hud::Client.viewable_by(current_user).find_by(id: id)
client.lock_version = client_lock_version if client_lock_version
# While this is redundant with the viewable_by() scope above, this check caches the authorization result so that
# the client object-level authorization check will succeed even after the client has been deleted
access_denied! unless current_permission?(permission: :can_view_clients, entity: client)
warnings, resolvable_enrollments = check_enrollments(client, ignore_warnings: confirmed)
return { client: nil, errors: warnings } if warnings.any?
client.transaction do
default_delete_record(
record: client,
field_name: :client,
permissions: :can_delete_clients,
after_delete: -> do
resolvable_enrollments.each do |enrollment|
enrollment.update!(relationship_to_ho_h: 1)
end
end,
)
end
end
def check_enrollments(client, ignore_warnings: false)
problem_enrollments = []
warnings = []
resolvable_enrollments = []
client.enrollments.viewable_by(current_user).heads_of_households.each do |enrollment|
members = enrollment.household_members
problem_enrollments << enrollment if members.count > 2
resolvable_enrollments += members.where.not(personal_id: client.personal_id) if members.count == 2
end
if problem_enrollments.present? && !ignore_warnings
warnings = HmisErrors::Errors.new
warnings.add(
:id,
:information,
full_message: self.class.problem_enrollments_message(problem_enrollments.size),
severity: :warning,
data: {
text: "The Head of Household for the following #{'household'.pluralize(problem_enrollments.size)} should be changed before this client is deleted:",
enrollments: problem_enrollments.map do |e|
{
id: e.id.to_s,
name: e.project&.project_name,
entryDate: e.entry_date,
exitDate: e.exit_date,
}
end,
},
)
end
[
warnings,
resolvable_enrollments,
]
end
end
end