generated from espoo-dev/rails_boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdestroy_self.rb
More file actions
62 lines (48 loc) · 1.28 KB
/
destroy_self.rb
File metadata and controls
62 lines (48 loc) · 1.28 KB
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
# frozen_string_literal: true
module Users
class DestroySelf
Result = Struct.new(:success?, :error)
def initialize(user, password)
@user = user
@password = password
end
def call
return Result.new(false, "Wrong password") unless valid_password?
ActiveRecord::Base.transaction do
destroy_event_procedures!
destroy_medical_shifts!
destroy_patients!
destroy_procedures!
destroy_health_insurances!
@user.destroy
end
Result.new(true, nil)
rescue StandardError => e
Result.new(false, e.message)
end
private
def valid_password?
@user.valid_password?(@password)
end
def destroy_event_procedures!
@user.event_procedures.destroy_all
end
def destroy_medical_shifts!
@user.medical_shifts.destroy_all
end
def destroy_health_insurances!
@user.health_insurances.destroy_all
end
def destroy_procedures!
@user.procedures.destroy_all
end
def destroy_patients!
@user.patients.each do |patient|
if patient.event_procedures.exists?(deleted_at: nil)
raise ActiveRecord::InvalidForeignKey, "Patient ##{patient.id} has associated procedures"
end
patient.destroy
end
end
end
end