Skip to content

Commit 5aacd22

Browse files
authored
Merge pull request #300 from espoo-dev/feat-users-destroyself
Adds endpoint to delete users account
2 parents 978dbcd + ea6c3b1 commit 5aacd22

24 files changed

+205
-26
lines changed

app/controllers/api/v1/users_controller.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ def index
1111
render json: users, status: :ok
1212
end
1313

14+
def destroy_self
15+
authorize(current_user)
16+
17+
result = Users::DestroySelf.result(user: current_user, password: confirmation_password)
18+
19+
if result.success?
20+
render json: { message: "Account deleted successfully" }, status: :ok
21+
else
22+
render json: { error: "Unable to delete account. Error: #{result.error}" }, status: :unprocessable_entity
23+
end
24+
end
25+
1426
private
1527

1628
def page
@@ -20,6 +32,10 @@ def page
2032
def per_page
2133
params[:per_page]
2234
end
35+
36+
def confirmation_password
37+
params[:password]
38+
end
2339
end
2440
end
2541
end

app/models/health_insurance.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class HealthInsurance < ApplicationRecord
4+
acts_as_paranoid
5+
46
belongs_to :user, optional: true
57

68
has_many :event_procedures, dependent: :destroy

app/models/hospital.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class Hospital < ApplicationRecord
4+
acts_as_paranoid
5+
46
has_many :event_procedures, dependent: :destroy
57

68
validates :name, presence: true

app/models/medical_shift.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class MedicalShift < ApplicationRecord
4+
acts_as_paranoid
5+
46
has_enumeration_for :workload, with: MedicalShifts::Workloads, create_helpers: true
57

68
monetize :amount

app/models/patient.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# frozen_string_literal: true
22

33
class Patient < ApplicationRecord
4-
has_many :event_procedures, dependent: :restrict_with_exception
4+
acts_as_paranoid
5+
6+
has_many :event_procedures, dependent: :destroy
57
belongs_to :user
68

79
validates :name, presence: true

app/models/procedure.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class Procedure < ApplicationRecord
4+
acts_as_paranoid
5+
46
monetize :amount
57

68
belongs_to :user, optional: true

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class User < ApplicationRecord
4+
acts_as_paranoid
5+
46
# Include default devise modules. Others available are:
57
# :lockable, :timeoutable, :trackable and :omniauthable
68
devise :database_authenticatable, :registerable,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module Users
4+
class DestroySelf < Actor
5+
input :user, type: User
6+
input :password, type: String
7+
8+
def call
9+
fail!(error: "Wrong password") unless valid_password?
10+
11+
ActiveRecord::Base.transaction { user.destroy_fully! }
12+
end
13+
14+
private
15+
16+
def valid_password?
17+
user.valid_password?(password)
18+
end
19+
end
20+
end

app/policies/user_policy.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class UserPolicy < ApplicationPolicy
44
def index?
55
user.admin?
66
end
7+
8+
def destroy_self?
9+
user == record
10+
end
711
end

config/routes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
end
3030
resources :patients, only: %i[index create update destroy]
3131
resources :procedures, only: %i[index create update destroy]
32-
resources :users, only: [:index]
32+
resources :users, only: [:index] do
33+
collection do
34+
delete :destroy_self
35+
end
36+
end
3337

3438
get "/event_procedures_dashboard/amount_by_day", to: "event_procedures_dashboard#amount_by_day"
3539
get "/pdf_reports/generate", to: "pdf_reports#generate", defaults: { format: :pdf }

0 commit comments

Comments
 (0)