Skip to content

Commit

Permalink
removed duplicate project and billing id checks
Browse files Browse the repository at this point in the history
  • Loading branch information
timalces committed Feb 29, 2024
1 parent 61941f9 commit 05a33fe
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions app/controllers/credit_deposits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ def new
@credit_deposit = CreditDeposit.new(team: @team)
authorize! :create, @credit_deposit
@cloud_service_config = CloudServiceConfig.first
check_config_and_external_ids
if @cloud_service_config.nil?
flash[:alert] = "Unable to add credits: cloud environment config not set"
redirect_to teams_path
elsif !@credit_deposit.valid?
flash[:alert] = "Unable to add credits: #{@credit_deposit.errors.full_messages.join("; ")}"
redirect_to teams_path
end
end

def create
Expand All @@ -13,20 +19,23 @@ def create
@credit_deposit = CreditDeposit.new(team: @team, amount: credit_deposit_params[:amount])
authorize! :create, @credit_deposit

if check_config_and_external_ids
unless @credit_deposit.valid?
render action: :new
return
end
if @cloud_service_config.nil?
flash[:alert] = "Unable to add credits: cloud environment config not set"
redirect_to teams_path
return
elsif !@credit_deposit.valid?
flash.now[:alert] = "Unable to add credits: #{@credit_deposit.errors.full_messages.join("; ")}"
render :new
return
end

result = CreateCreditDepositJob.perform_now(@credit_deposit, @cloud_service_config)
if result.success?
flash[:success] = "Credit deposit submitted for #{@team.name}. It may take a few minutes for the team's new balance to be reflected."
redirect_to teams_path
else
flash.now[:alert] = "Unable to submit credit deposit: #{result.error_message}"
render :new
end
result = CreateCreditDepositJob.perform_now(@credit_deposit, @cloud_service_config)
if result.success?
flash[:success] = "Credit deposit submitted for #{@team.name}. It may take a few minutes for the team's new balance to be reflected."
redirect_to teams_path
else
flash.now[:alert] = "Unable to submit credit deposit: #{result.error_message}"
render :new
end
end

Expand All @@ -36,27 +45,4 @@ def create
def credit_deposit_params
params.require(:credit_deposit).permit(*PERMITTED_PARAMS)
end

def check_config_and_external_ids
redirect = false
if @cloud_service_config.nil?
flash[:alert] = "Unable to add credits: cloud environment config not set"
redirect = true
elsif @team.project_id.nil?
flash[:alert] = "Unable to add credits: team does not yet have a project id. " \
"This should be added automatically shortly."
redirect = true
elsif @team.billing_acct_id.nil?
flash[:alert] = "Unable to add credits: team does not yet have a billing account id. " \
"This should be added automatically shortly."
redirect = true
end

if redirect
redirect_to teams_path
false
else
true
end
end
end

0 comments on commit 05a33fe

Please sign in to comment.