-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdelete_application.rb
95 lines (83 loc) · 2.44 KB
/
delete_application.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
85
86
87
88
89
90
91
92
93
94
95
class DeleteApplication
include ImpersonationAuditHelper
attr_reader :actor, :application_form, :zendesk_url
APPLICATION_FORM_FIELDS_TO_REDACT = %i[
first_name
last_name
first_nationality
second_nationality
english_main_language
english_language_details
other_language_details
date_of_birth
further_information
phone_number
address_line1
address_line2
address_line3
address_line4
country
postcode
disability_disclosure
work_history_explanation
becoming_a_teacher
interview_preferences
work_history_breaks
volunteering_experience
equality_and_diversity
safeguarding_issues
international_address
right_to_work_or_study
right_to_work_or_study_details
third_nationality
fourth_nationality
fifth_nationality
feedback_satisfaction_level
feedback_suggestions
work_history_status
].freeze
ASSOCIATIONS_TO_DESTROY = %i[
application_work_experiences
application_volunteering_experiences
application_qualifications
application_references
application_work_history_breaks
application_feedback
].freeze
def initialize(actor:, application_form:, zendesk_url:, force: false)
@actor = actor
@application_form = application_form
@zendesk_url = zendesk_url
@force = force
end
def call!
if !@force && !application_form.application_choices.all?(&:unsubmitted?)
raise 'Application has been sent to providers' \
end
audit(actor) do
ActiveRecord::Base.transaction do
ASSOCIATIONS_TO_DESTROY.each { |assoc| application_form.send(assoc)&.destroy_all }
APPLICATION_FORM_FIELDS_TO_REDACT.each { |attr| application_form.send("#{attr}=", nil) }
application_form.save!
reference = application_form.support_reference
application_form.candidate.update!(email_address: "deleted-application-#{reference}@example.com")
application_form.candidate.one_login_auth&.delete
application_form.candidate.sessions&.destroy_all
application_form.own_and_associated_audits.destroy_all
add_audit_event_for_deletion!
end
end
end
private
def add_audit_event_for_deletion!
comment = "Data deletion request: #{zendesk_url}"
application_form.reload.audits << Audited::Audit.new(
action: 'destroy',
user: actor,
version: 1,
audited_changes: {},
comment:,
created_at: Time.zone.now,
)
end
end