Class: UserService::Delete

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer, ElasticAPM::SpanHelpers
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.



14
15
16
17
# File 'app/services/user_service/delete.rb', line 14

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.



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

def reason
  @reason
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#delete_accountObject



36
37
38
39
40
41
# File 'app/services/user_service/delete.rb', line 36

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

#delete_account!Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/user_service/delete.rb', line 55

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



27
28
29
30
31
32
33
# File 'app/services/user_service/delete.rb', line 27

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

#suspend!Object

Raises:

  • (StandardError)


44
45
46
47
48
49
50
51
52
# File 'app/services/user_service/delete.rb', line 44

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

  change_password
  remove_all_token
  reset_all_social_media_uids
  
  notify_support
end

#valid?Boolean

Returns:

  • (Boolean)


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

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

  true
end