update code based on feedback
This commit is contained in:
		
							parent
							
								
									8f2adb8084
								
							
						
					
					
						commit
						b33c638483
					
				| 
						 | 
					@ -210,10 +210,8 @@ class Admin::UsersController < Admin::ApplicationController
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def update_user
 | 
					  def update_user(&block)
 | 
				
			||||||
    result = Users::UpdateService.new(user).execute do |user|
 | 
					    result = Users::UpdateService.new(user).execute(&block)
 | 
				
			||||||
      yield(user)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    result[:status] == :success
 | 
					    result[:status] == :success
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,8 +41,10 @@ class Profiles::TwoFactorAuthsController < Profiles::ApplicationController
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def create
 | 
					  def create
 | 
				
			||||||
    if current_user.validate_and_consume_otp!(params[:pin_code])
 | 
					    if current_user.validate_and_consume_otp!(params[:pin_code])
 | 
				
			||||||
 | 
					      codes = nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      Users::UpdateService.new(current_user, otp_required_for_login: true).execute! do |user|
 | 
					      Users::UpdateService.new(current_user, otp_required_for_login: true).execute! do |user|
 | 
				
			||||||
        @codes = user.generate_otp_backup_codes!
 | 
					        codes = user.generate_otp_backup_codes!
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      render 'create'
 | 
					      render 'create'
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,4 @@
 | 
				
			||||||
module Users
 | 
					module Users
 | 
				
			||||||
  # Service for building a new user.
 | 
					 | 
				
			||||||
  class BuildService < BaseService
 | 
					  class BuildService < BaseService
 | 
				
			||||||
    def initialize(current_user, params = {})
 | 
					    def initialize(current_user, params = {})
 | 
				
			||||||
      @current_user = current_user
 | 
					      @current_user = current_user
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,4 @@
 | 
				
			||||||
module Users
 | 
					module Users
 | 
				
			||||||
  # Service for creating a new user.
 | 
					 | 
				
			||||||
  class CreateService < BaseService
 | 
					  class CreateService < BaseService
 | 
				
			||||||
    def initialize(current_user, params = {})
 | 
					    def initialize(current_user, params = {})
 | 
				
			||||||
      @current_user = current_user
 | 
					      @current_user = current_user
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,4 @@
 | 
				
			||||||
module Users
 | 
					module Users
 | 
				
			||||||
  # Service for updating a user.
 | 
					 | 
				
			||||||
  class UpdateService < BaseService
 | 
					  class UpdateService < BaseService
 | 
				
			||||||
    def initialize(user, params = {})
 | 
					    def initialize(user, params = {})
 | 
				
			||||||
      @user = user
 | 
					      @user = user
 | 
				
			||||||
| 
						 | 
					@ -7,6 +6,8 @@ module Users
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def execute(validate: true, &block)
 | 
					    def execute(validate: true, &block)
 | 
				
			||||||
 | 
					      yield(@user) if block_given?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      assign_attributes(&block)
 | 
					      assign_attributes(&block)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if @user.save(validate: validate)
 | 
					      if @user.save(validate: validate)
 | 
				
			||||||
| 
						 | 
					@ -27,8 +28,6 @@ module Users
 | 
				
			||||||
    private
 | 
					    private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def assign_attributes(&block)
 | 
					    def assign_attributes(&block)
 | 
				
			||||||
      yield(@user) if block_given?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      @user.assign_attributes(params) if params.any?
 | 
					      @user.assign_attributes(params) if params.any?
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -132,11 +132,13 @@ module API
 | 
				
			||||||
          return { success: false, message: 'Two-factor authentication is not enabled for this user' }
 | 
					          return { success: false, message: 'Two-factor authentication is not enabled for this user' }
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        codes = nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ::Users::UpdateService.new(user).execute! do |user|
 | 
					        ::Users::UpdateService.new(user).execute! do |user|
 | 
				
			||||||
          @codes = user.generate_otp_backup_codes!
 | 
					          codes = user.generate_otp_backup_codes!
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        { success: true, recovery_codes: @codes }
 | 
					        { success: true, recovery_codes: codes }
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      post "/notify_post_receive" do
 | 
					      post "/notify_post_receive" do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,7 @@ describe Users::UpdateService, services: true do
 | 
				
			||||||
    it 'updates the name' do
 | 
					    it 'updates the name' do
 | 
				
			||||||
      result = update_user(user, name: 'New Name')
 | 
					      result = update_user(user, name: 'New Name')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      expect(result).to eq({ status: :success })
 | 
					      expect(result).to eq(status: :success)
 | 
				
			||||||
      expect(user.name).to eq('New Name')
 | 
					      expect(user.name).to eq('New Name')
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,9 +30,9 @@ describe Users::UpdateService, services: true do
 | 
				
			||||||
      expect(user.name).to eq('New Name')
 | 
					      expect(user.name).to eq('New Name')
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it 'returns an error result when record cannot be updated' do
 | 
					    it 'raises an error when record cannot be updated' do
 | 
				
			||||||
      expect do
 | 
					      expect do
 | 
				
			||||||
        update_user(user, { email: 'invalid' })
 | 
					        update_user(user, email: 'invalid')
 | 
				
			||||||
      end.to raise_error(ActiveRecord::RecordInvalid)
 | 
					      end.to raise_error(ActiveRecord::RecordInvalid)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue