-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AF-29] Create actions for pause and unpause auctions (#30)
* Create actions for pause and unpause auctions * Upgrade CHANGELOG and lib version
- Loading branch information
1 parent
9b1aeac
commit a9a595e
Showing
15 changed files
with
449 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
auction_fun_core (0.8.1) | ||
auction_fun_core (0.8.3) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
|
34 changes: 34 additions & 0 deletions
34
lib/auction_fun_core/contracts/auction_context/processor/pause_contract.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module AuctionFunCore | ||
module Contracts | ||
module AuctionContext | ||
module Processor | ||
## | ||
# Contract class for pause auction. | ||
# | ||
class PauseContract < Contracts::ApplicationContract | ||
option :auction_repository, default: proc { Repos::AuctionContext::AuctionRepository.new } | ||
|
||
params do | ||
required(:auction_id).filled(:integer) | ||
end | ||
|
||
# Validation for auction. | ||
# Validates if the auction exists in the database and check if only auctions | ||
# with a "running" status can be paused. | ||
rule(:auction_id) do |context:| | ||
context[:auction] ||= auction_repository.by_id(value) | ||
key.failure(I18n.t("contracts.errors.custom.not_found")) unless context[:auction] | ||
|
||
unless %w[running].include?(context[:auction].status) | ||
key.failure( | ||
I18n.t("contracts.errors.custom.bids.invalid_status", status: context[:auction].status) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
lib/auction_fun_core/contracts/auction_context/processor/unpause_contract.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module AuctionFunCore | ||
module Contracts | ||
module AuctionContext | ||
module Processor | ||
## | ||
# Contract class for unpause auction. | ||
# | ||
class UnpauseContract < Contracts::ApplicationContract | ||
option :auction_repository, default: proc { Repos::AuctionContext::AuctionRepository.new } | ||
|
||
params do | ||
required(:auction_id).filled(:integer) | ||
end | ||
|
||
# Validation for auction. | ||
# Validates if the auction exists in the database and and checks if the | ||
# auction has a paused status. | ||
rule(:auction_id) do |context:| | ||
context[:auction] ||= auction_repository.by_id(value) | ||
key.failure(I18n.t("contracts.errors.custom.not_found")) unless context[:auction] | ||
|
||
unless %w[paused].include?(context[:auction].status) | ||
key.failure( | ||
I18n.t("contracts.errors.custom.bids.invalid_status", status: context[:auction].status) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
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
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
57 changes: 57 additions & 0 deletions
57
lib/auction_fun_core/operations/auction_context/processor/pause_operation.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module AuctionFunCore | ||
module Operations | ||
module AuctionContext | ||
module Processor | ||
## | ||
# Operation class for dispatch pause auction. | ||
# By default, this change auction status from 'running' to 'paused'. | ||
# | ||
class PauseOperation < AuctionFunCore::Operations::Base | ||
include Import["repos.auction_context.auction_repository"] | ||
include Import["contracts.auction_context.processor.pause_contract"] | ||
|
||
# @todo Add custom doc | ||
def self.call(attributes, &block) | ||
operation = new.call(attributes) | ||
|
||
return operation unless block | ||
|
||
Dry::Matcher::ResultMatcher.call(operation, &block) | ||
end | ||
|
||
def call(attributes) | ||
attrs = yield validate(attributes) | ||
|
||
auction_repository.transaction do |_t| | ||
@auction, _ = auction_repository.update(attrs[:auction_id], status: "paused") | ||
|
||
publish_auction_pause_event(@auction) | ||
end | ||
|
||
Success(attrs[:auction_id]) | ||
end | ||
|
||
private | ||
|
||
# Calls the finish contract class to perform the validation | ||
# of the informed attributes. | ||
# @param attrs [Hash] auction attributes | ||
# @return [Dry::Monads::Result::Success, Dry::Monads::Result::Failure] | ||
def validate(attributes) | ||
contract = pause_contract.call(attributes) | ||
|
||
return Failure(contract.errors.to_h) if contract.failure? | ||
|
||
Success(contract.to_h) | ||
end | ||
|
||
def publish_auction_pause_event(auction) | ||
Application[:event].publish("auctions.paused", auction.to_h) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
57 changes: 57 additions & 0 deletions
57
lib/auction_fun_core/operations/auction_context/processor/unpause_operation.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module AuctionFunCore | ||
module Operations | ||
module AuctionContext | ||
module Processor | ||
## | ||
# Operation class for dispatch unpause auction. | ||
# By default, this change auction status from 'paused' to 'running'. | ||
# | ||
class UnpauseOperation < AuctionFunCore::Operations::Base | ||
include Import["repos.auction_context.auction_repository"] | ||
include Import["contracts.auction_context.processor.unpause_contract"] | ||
|
||
# @todo Add custom doc | ||
def self.call(attributes, &block) | ||
operation = new.call(attributes) | ||
|
||
return operation unless block | ||
|
||
Dry::Matcher::ResultMatcher.call(operation, &block) | ||
end | ||
|
||
def call(attributes) | ||
attrs = yield validate(attributes) | ||
|
||
auction_repository.transaction do |_t| | ||
@auction, _ = auction_repository.update(attrs[:auction_id], status: "running") | ||
|
||
publish_auction_unpause_event(@auction) | ||
end | ||
|
||
Success(attrs[:auction_id]) | ||
end | ||
|
||
private | ||
|
||
# Calls the unpause contract class to perform the validation | ||
# of the informed attributes. | ||
# @param attrs [Hash] auction attributes | ||
# @return [Dry::Monads::Result::Success, Dry::Monads::Result::Failure] | ||
def validate(attributes) | ||
contract = unpause_contract.call(attributes) | ||
|
||
return Failure(contract.errors.to_h) if contract.failure? | ||
|
||
Success(contract.to_h) | ||
end | ||
|
||
def publish_auction_unpause_event(auction) | ||
Application[:event].publish("auctions.unpaused", auction.to_h) | ||
end | ||
end | ||
end | ||
end | ||
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
39 changes: 39 additions & 0 deletions
39
spec/auction_fun_core/contracts/auction_context/processor/pause_contract_spec.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe AuctionFunCore::Contracts::AuctionContext::Processor::PauseContract, type: :contract do | ||
let(:auction) { Factory[:auction, :default_standard] } | ||
|
||
describe "#call" do | ||
subject(:contract) { described_class.new.call(attributes) } | ||
|
||
context "when attributes are invalid" do | ||
let(:attributes) { Dry::Core::Constants::EMPTY_HASH } | ||
|
||
it "expect failure with error messages" do | ||
expect(contract).to be_failure | ||
expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.key?")) | ||
end | ||
end | ||
|
||
context "when auction status is different to 'running'" do | ||
let(:attributes) { {auction_id: auction.id} } | ||
|
||
it "expect failure with error messages" do | ||
expect(contract).to be_failure | ||
expect(contract.errors[:auction_id]).to include( | ||
I18n.t("contracts.errors.custom.bids.invalid_status") | ||
) | ||
end | ||
end | ||
|
||
context "when auction status is equal to 'running'" do | ||
let(:auction) { Factory[:auction, :default_running_standard] } | ||
let(:attributes) { {auction_id: auction.id} } | ||
|
||
it "expect return success" do | ||
expect(contract).to be_success | ||
expect(contract.context[:auction]).to be_a(AuctionFunCore::Entities::Auction) | ||
end | ||
end | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
spec/auction_fun_core/contracts/auction_context/processor/unpause_contract_spec.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe AuctionFunCore::Contracts::AuctionContext::Processor::UnpauseContract, type: :contract do | ||
let(:auction) { Factory[:auction, :default_standard] } | ||
|
||
describe "#call" do | ||
subject(:contract) { described_class.new.call(attributes) } | ||
|
||
context "when attributes are invalid" do | ||
let(:attributes) { Dry::Core::Constants::EMPTY_HASH } | ||
|
||
it "expect failure with error messages" do | ||
expect(contract).to be_failure | ||
expect(contract.errors[:auction_id]).to include(I18n.t("contracts.errors.key?")) | ||
end | ||
end | ||
|
||
context "when auction status is different to 'paused'" do | ||
let(:attributes) { {auction_id: auction.id} } | ||
|
||
it "expect failure with error messages" do | ||
expect(contract).to be_failure | ||
expect(contract.errors[:auction_id]).to include( | ||
I18n.t("contracts.errors.custom.bids.invalid_status") | ||
) | ||
end | ||
end | ||
|
||
context "when auction status is equal to 'paused'" do | ||
let(:auction) { Factory[:auction, :default_paused_standard] } | ||
let(:attributes) { {auction_id: auction.id} } | ||
|
||
it "expect return success" do | ||
expect(contract).to be_success | ||
expect(contract.context[:auction]).to be_a(AuctionFunCore::Entities::Auction) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.