Skip to content

Commit

Permalink
Withdraw interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindew committed Apr 17, 2019
1 parent 611daf0 commit 63e83e9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
51 changes: 22 additions & 29 deletions app/controllers/withdraw_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,32 @@ def new
end

def create
unless current_user.has_permission?(User::MANAGING_EDITOR_PERMISSION)
result = Withdraw::CreateInteractor.call(params: params, user: current_user)
edition, no_permission, issues, api_error = result.to_h.values_at(:edition,
:no_permission,
:issues,
:api_error)
if no_permission
# FIXME: this shouldn't be an exception but we've not worked out the
# right response - maybe bad request or a redirect with flash?
raise "Can't withdraw an edition without managing editor permissions"
end

public_explanation = params[:public_explanation]

Edition.find_and_lock_current(document: params[:document]) do |edition|
issues = Requirements::WithdrawalChecker.new(public_explanation, edition)
.pre_withdrawal_issues

if issues.any?
flash["alert_with_items"] = {
"title" => I18n.t!("withdraw.new.flashes.requirements"),
"items" => issues.items,
}

render :new,
assigns: { edition: edition, public_explanation: public_explanation, issues: issues },
status: :unprocessable_entity
next
end

#FIXME We should check that the edition is withdrawable before passing
# it to the WithdrawService
WithdrawService.new.call(edition, public_explanation, current_user)
elsif issues
flash["alert_with_items"] = {
"title" => I18n.t!("withdraw.new.flashes.requirements"),
"items" => issues.items,
}

render :new,
assigns: { edition: edition,
public_explanation: params[:public_explanation],
issues: issues },
status: :unprocessable_entity
elsif api_error
redirect_to withdraw_path(params[:document]),
alert_with_description: t("withdraw.new.flashes.publishing_api_error"),
public_explanation: params[:public_explanation]
else
redirect_to document_path(edition.document)
end
rescue GdsApi::BaseError => e
GovukError.notify(e)
redirect_to withdraw_path(params[:document]),
alert_with_description: t("withdraw.new.flashes.publishing_api_error"),
public_explanation: params[:public_explanation]
end
end
48 changes: 48 additions & 0 deletions app/interactors/withdraw/create_interactor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

class Withdraw::CreateInteractor
include Interactor

delegate :params,
:user,
:edition,
:no_permission,
:issues,
:api_error,
to: :context

def call
check_permission

Edition.transaction do
find_and_lock_edition
check_for_issues
withdraw
end
end

private

def check_permission
unless user.has_permission?(User::MANAGING_EDITOR_PERMISSION)
context.fail!(no_permission: true)
end
end

def find_and_lock_edition
context.edition = Edition.lock.find_current(document: params[:document])
end

def check_for_issues
issues = Requirements::WithdrawalChecker.new(params[:public_explanation], edition)
.pre_withdrawal_issues
context.fail!(issues: issues) if issues.any?
end

def withdraw
WithdrawService.new.call(edition, params[:public_explanation], user)
rescue GdsApi::BaseError => e
GovukError.notify(e)
context.fail!(api_error: true)
end
end

0 comments on commit 63e83e9

Please sign in to comment.