Class: CommandTower::Services::Admin::Users::UpdateUsername

Inherits:
CommandTower::Services::ApplicationService show all
Defined in:
app/services/command_tower/services/admin/users.rb

Constant Summary

Constants inherited from CommandTower::ServiceBase

CommandTower::ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from CommandTower::Services::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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/services/command_tower/services/admin/users.rb', line 168

def call
  normalized = username.to_s.strip
  if user.username.to_s == normalized
    context.user = user
    context.changed = false
    return
  end

  result = CommandTower::UserAttributes::Mutate.call(user:, username: normalized)
  unless result.success?
    details = result.invalid_argument_hash.transform_values { _1[:msg].to_s }
    context.fail!(application_error: CommandTower::Errors::ValidationError.new(details:))
    return
  end

  context.user = user.reload
  context.changed = true
rescue ActiveRecord::RecordInvalid => error
  fail_uniqueness_or_raise(error)
rescue ActiveRecord::RecordNotUnique
  context.fail!(application_error: IdentityUniqueness.uniqueness_error("username"))
end