From 611daf02c50c1c9890e7f0f058ff79f63d28ca33 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Wed, 17 Apr 2019 21:03:51 +0100 Subject: [PATCH] Unwithdraw interactor --- app/controllers/unwithdraw_controller.rb | 14 +++++----- .../unwithdraw/unwithdraw_interactor.rb | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 app/interactors/unwithdraw/unwithdraw_interactor.rb diff --git a/app/controllers/unwithdraw_controller.rb b/app/controllers/unwithdraw_controller.rb index 4a3fb96973..bba608a659 100644 --- a/app/controllers/unwithdraw_controller.rb +++ b/app/controllers/unwithdraw_controller.rb @@ -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 diff --git a/app/interactors/unwithdraw/unwithdraw_interactor.rb b/app/interactors/unwithdraw/unwithdraw_interactor.rb new file mode 100644 index 0000000000..ad1308f2b4 --- /dev/null +++ b/app/interactors/unwithdraw/unwithdraw_interactor.rb @@ -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