class Monban::UseCase::Account::Change::Email

Public Instance Methods

change(params) click to toggle source
# File lib/monban/use_case/account/change/email.rb, line 24
def change(params)
  Getto::Params.new.validate(params) do |v|
    v.hash(
      account_id: v.integer            {|val| param_error!(account_id: val) },
      email:      v.combine([v.string]){|val| param_error!(email: val) },
    )
  end or param_error!(params: params)

  repository.transaction do
    unless repository.account_exists?(account_id: params[:account_id])
      error.not_found! "account_id: #{params[:account_id]}"
    end

    email_account = repository.reset_password_email_account(email: params[:email])

    if email_account && email_account != params[:account_id]
      error.conflict! "email: #{params[:email]}"
    end

    unless email_account
      repository.update_reset_password_email(
        account_id: params[:account_id],
        email:      params[:email],
        now:        time.now,
      )
    end

    {
      email: repository.reset_password_email(account_id: params[:account_id]),
    }
  end
end