Class: AuctionFunCore::Operations::BidContext::CreateBidPennyOperation

Inherits:
AuctionFunCore::Operations::Base show all
Defined in:
lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb

Overview

Operation class for create new bids for penny auctions.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(attributes, &block) ⇒ Object

TODO:

Add custom doc



16
17
18
19
20
21
22
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 16

def self.call(attributes, &block)
  operation = new.call(attributes)

  return operation unless block

  Dry::Matcher::ResultMatcher.call(operation, &block)
end

Instance Method Details

#call(attributes) ⇒ Object

TODO:

Add custom doc



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 25

def call(attributes)
  auction, values = yield validate_contract(attributes)

  bid_repository.transaction do |_t|
    @bid = yield persist(values)
    updated_auction = yield update_end_auction(auction)

    yield reschedule_end_auction(updated_auction)
    yield publish_bid_created(@bid)
  end

  Success(@bid)
end

#persist(values) ⇒ Dry::Monads::Result::Success<ROM::Struct::Bid>, Dry::Monads::Result::Failure

Calls the bid repository class to persist the attributes in the database.

Parameters:

  • result (Hash)

    Bid validated attributes

Returns:

  • (Dry::Monads::Result::Success<ROM::Struct::Bid>, Dry::Monads::Result::Failure)


77
78
79
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 77

def persist(values)
  Success(bid_repository.create(values))
end

#publish_bid_created(bid) ⇒ Dry::Monads::Result::Success

Triggers the publication of event bids.created.

Parameters:

  • bid (ROM::Struct::Bid)

    Bid Object

Returns:

  • (Dry::Monads::Result::Success)


84
85
86
87
88
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 84

def publish_bid_created(bid)
  Application[:event].publish("bids.created", bid.to_h)

  Success()
end

#reschedule_end_auction(auction) ⇒ Dry::Monads::Result::Success<ROM::Struct::Auction>, Dry::Monads::Result::Success<String>

TODO: Added a small delay to perform operations (such as sending broadcasts and/or other operations). Reschedules the end time of an auction’s background job if the auction has already started.

This method checks if the auction is running. If so, it schedules a background job to execute at the auction’s current finish time using the job class defined by the penny_operation_job attribute of the auction. If the auction has not started, it simply returns a Success object with no parameters.

Returns:

  • (Dry::Monads::Result::Success<ROM::Struct::Auction>, Dry::Monads::Result::Success<String>)


99
100
101
102
103
104
105
106
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 99

def reschedule_end_auction(auction)
  # binding.pry
  return Success(auction) unless started_auction?(auction)

  perform_at = auction.finished_at

  Success(penny_operation_job.class.perform_at(perform_at, auction.id))
end

#started_auction?(auction) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 108

def started_auction?(auction)
  auction.status == "running"
end

#update_end_auction(auction) ⇒ Dry::Monads::Result::Success<ROM::Struct::Auction>, Dry::Monads::Result::Failure

Updates the end time of an auction if it has already started.

This method checks whether an auction is currently running. If the auction is running, it calculates a new end time based on the current time and the duration specified by the auction’s stopwatch. The auction’s finish time is then updated in the repository. If the auction has not started, it returns the auction as is without any modifications.

Parameters:

  • auction (ROM::Struct::Auction)

    An instance of Auction to be checked and potentially updated.

Returns:

  • (Dry::Monads::Result::Success<ROM::Struct::Auction>, Dry::Monads::Result::Failure)


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 60

def update_end_auction(auction)
  return Success(auction) unless started_auction?(auction)

  updated_attributes = {
    finished_at: Time.current + auction.stopwatch.seconds,
    kind: auction.kind,
    status: auction.status
  }

  updated_auction, _ = auction_repository.update(auction.id, updated_attributes)

  Success(updated_auction)
end

#validate_contract(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure

Calls the bid creation contract class to perform the validation of the informed attributes.

Parameters:

  • attrs (Hash)

    bid attributes

Returns:

  • (Dry::Monads::Result::Success, Dry::Monads::Result::Failure)


43
44
45
46
47
48
49
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 43

def validate_contract(attrs)
  contract = create_bid_penny_contract.call(attrs)

  return Failure(contract.errors.to_h) if contract.failure?

  Success([contract.context[:auction], contract.to_h])
end