Skip to content

Commit

Permalink
Unwithdraw interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindew committed Apr 17, 2019
1 parent 567dfe9 commit 611daf0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/controllers/unwithdraw_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def confirm
end

def unwithdraw
Edition.find_and_lock_current(document: params[:document]) do |edition|
UnwithdrawService.new.call(edition, current_user)
redirect_to edition.document
result = Unwithdraw::UnwithdrawInteractor.call(params: params, user: current_user)

if result.api_error
redirect_to document_path(params[:document]),
alert_with_description: t("documents.show.flashes.unwithdraw_error")
else
redirect_to document_path(params[:document])
end
rescue GdsApi::BaseError => e
GovukError.notify(e)
redirect_to document_path(params[:document]),
alert_with_description: t("documents.show.flashes.unwithdraw_error")
end
end
27 changes: 27 additions & 0 deletions app/interactors/unwithdraw/unwithdraw_interactor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class Unwithdraw::UnwithdrawInteractor
include Interactor

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

def call
Edition.transaction do
find_and_lock_edition
unwithdraw
end
end

private

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

def unwithdraw
UnwithdrawService.new.call(edition, user)
rescue GdsApi::BaseError => e
GovukError.notify(e)
context.fail!(api_error: true)
end
end

0 comments on commit 611daf0

Please sign in to comment.