Class: CommandTower::Services::Me::DeleteAccount

Inherits:
ApplicationService show all
Defined in:
app/services/command_tower/services/me/delete_account.rb

Constant Summary collapse

FIELD_KEYS =
{
  password: :password
}.freeze

Constants inherited from CommandTower::ServiceBase

CommandTower::ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from ApplicationService

call, inherited

Methods included from Transactional

#fail_transaction!, #transaction

Methods inherited from CommandTower::ServiceBase

#command_tower_lifecycle, inherited, #internal_validate, #service_lifecycle_error_codes, #service_lifecycle_log_level, #validate!

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from ArgumentValidation

included

Methods included from CommandTower::ServiceLogging

included

Instance Method Details

#callObject



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
# File 'app/services/command_tower/services/me/delete_account.rb', line 14

def call
  if user.deleted?
    context.fail!(
      application_error: CommandTower::Errors::Auth::AccountDeletedError.new
    )
    return
  end

  reject!(password: "Incorrect password") unless user.authenticate(password)

  infrastructure_failure = false

  ActiveRecord::Base.transaction do
    unless invoke_host_finalizer!
      infrastructure_failure = true
      raise ActiveRecord::Rollback
    end

    purge_result = CommandTower::Services::Auth::PurgeUserAssociatedData.call(user:)
    unless purge_result.success?
      infrastructure_failure = true
      raise ActiveRecord::Rollback
    end

    tombstone_result = CommandTower::Services::Auth::TombstoneUser.call(user:)
    unless tombstone_result.success?
      infrastructure_failure = true
      raise ActiveRecord::Rollback
    end

    user.reload
    audit(
      :account_deleted,
      subject: user,
      affected_user: user,
      changes: {},
      metadata: { mechanism: "self_service" }
    )
  end

  if infrastructure_failure
    context.fail!(application_error: CommandTower::Errors::InternalError.new)
    return
  end

  user.reload
  unless user.deleted?
    log_error("Account deletion transaction did not persist for user [#{user.id}]")
    context.fail!(application_error: CommandTower::Errors::InternalError.new)
  end

  log_info("Account deleted for user [#{user.id}]")
  context.message = "Your account has been deleted"
end