Created
August 2, 2019 12:04
-
-
Save 0legovich/7eb540e351a61bf155bbed7bfb13c3eb to your computer and use it in GitHub Desktop.
Пример использования dry-transaction и dry-container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class OperationProxyContainer # :nodoc: | |
extend Dry::Container::Mixin | |
Import = Dry::AutoInject(self) | |
register 'make_document_file' do | |
MakeOperationProxyDocumentFileService | |
end | |
register 'approve_operation_proxy_assets' do | |
ApproveOperationProxyAssets | |
end | |
register 'rollback_checker' do | |
RollbackOperationProxyChecker | |
end | |
register 'rollback_operation_proxy_assets' do | |
RollbackOperationProxyAssets | |
end | |
register 'asset_creator' do | |
AssetsCreator | |
end | |
register 'operation_proxy_assets_creator' do | |
OperationProxyAssetsCreator | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module OperationProxyTransaction | |
class Rollback # :nodoc: | |
include Dry::Transaction | |
include Storage::OperationProxyContainer::Import[:rollback_checker, :rollback_operation_proxy_assets] | |
step :check_operation_proxy | |
step :rollback_assets | |
step :soft_delete_document | |
step :rollback_memo_orders | |
def check_operation_proxy(operation_proxy:, password:, current_user:) | |
return Failure("OperationProxy should not be pending") if operation_proxy.pending? | |
operation_proxy.lock! | |
unless rollback_checker.rollbackable?(operation_proxy) | |
return Failure("OperationProxy #{operation_proxy.id} is not rollbackble") | |
end | |
Success(operation_proxy: operation_proxy, password: password, current_user: current_user) | |
end | |
def rollback_assets(operation_proxy:, **_args) | |
return Success(operation_proxy: operation_proxy, **_args) if operation_proxy.wait_felix? | |
rollback_operation_proxy_assets.call(operation_proxy) | |
Success(operation_proxy: operation_proxy, **_args) | |
end | |
def soft_delete_document(operation_proxy:, password:, **_args) | |
return Success(operation_proxy: operation_proxy, **_args) if operation_proxy.wait_felix? | |
document = operation_proxy.movement_asset.document | |
document.soft_delete!(password) | |
emails = document.steps.flat_map { |step| step.user ? step.user.email : step.role.users.pluck(:email) }.uniq.compact | |
MovementAssetMailer.destroy_process(operation_proxy, emails).deliver_now | |
Success(operation_proxy: operation_proxy, **_args) | |
end | |
def rollback_memo_orders(operation_proxy:, current_user:, **_args) | |
operation_proxy.pending! | |
operation_proxy.memo_orders.where(status: %w[sent paid]).each { |mo| CancelMemoOrderJob.perform_later(mo, current_user) } | |
Success(operation_proxy: operation_proxy, **_args) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment