Class: UserService::Delete

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/services/user_service/delete.rb

Overview

This class is used to delete user account when user delete their account, we will not remove their account but we will mark them as deleted, and anonymize their data we will remove their data from user table, and move it to archive DB we will also remove their reviews, and change their name, email, and phone

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(user_id, reason) ⇒ Delete

Returns a new instance of Delete.



12
13
14
15
# File 'app/services/user_service/delete.rb', line 12

def initialize(user_id, reason)
  @user = User.find_by(id: user_id)
  @reason = reason
end

Instance Attribute Details

#reasonObject

Returns the value of attribute reason.



10
11
12
# File 'app/services/user_service/delete.rb', line 10

def reason
  @reason
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'app/services/user_service/delete.rb', line 10

def user
  @user
end

Instance Method Details

#delete_accountObject



32
33
34
35
36
37
# File 'app/services/user_service/delete.rb', line 32

def 
  delete_account!
rescue StandardError => e
  APMErrorHandler.report(e)
  false
end

#delete_account!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/user_service/delete.rb', line 48

def delete_account!
  status = false
  ActiveRecord::Base.transaction do
    begin
      archive_user
      change_password
      remove_reviews
      
    rescue StandardError => e
      APMErrorHandler.report e
      raise ActiveRecord::Rollback
    end

    status = true
  end

  status
end

#suspendObject



24
25
26
27
28
29
30
# File 'app/services/user_service/delete.rb', line 24

def suspend
  suspend!
rescue StandardError => e
  errors.add(:base, e)
  APMErrorHandler.report(e)
  false
end

#suspend!Object

Raises:

  • (StandardError)


39
40
41
42
43
44
45
46
# File 'app/services/user_service/delete.rb', line 39

def suspend!
  raise StandardError, I18n.t('views.user.invalid_deletion_reason') if reason.blank?

  change_password
  remove_all_token
  
  notify_support
end

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'app/services/user_service/delete.rb', line 17

def valid?
  return false if user.blank?
  return false if errors.present?

  true
end