Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AF-33] Set up the end of the auction #38

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
## [Unreleased]

## [0.8.6] - 2024-04-23

### Added

- Auction finalization configured for each type of auction;
- Add operations for winner and participant of an auction;
- Configure background job to work with unique jobs;
- Add 'sidekiq-unique-jobs' to be responsible for finishing penny auctions;

### Changed:

- General improvements on seed data;

### Fixed:

- NameError: uninitialized constant AuctionFunCore::Workers::ApplicationJob::Sidekiq (NameError)

## [0.8.5] - 2024-04-03

### Added
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
auction_fun_core (0.8.5)
auction_fun_core (0.8.6)
activesupport (= 7.1.3.2)
bcrypt (= 3.1.20)
dotenv (= 3.1.0)
Expand All @@ -18,6 +18,7 @@ PATH
rom (= 5.3.0)
rom-sql (= 3.6.2)
sidekiq (= 7.2.2)
sidekiq-unique-jobs (= 8.0.10)
yard (= 0.9.36)
zeitwerk (= 2.6.13)

Expand Down Expand Up @@ -223,6 +224,10 @@ GEM
connection_pool (>= 2.3.0)
rack (>= 2.2.4)
redis-client (>= 0.19.0)
sidekiq-unique-jobs (8.0.10)
concurrent-ruby (~> 1.0, >= 1.0.5)
sidekiq (>= 7.0.0, < 8.0.0)
thor (>= 1.0, < 3.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -241,6 +246,7 @@ GEM
standard-performance (1.3.1)
lint_roller (~> 1.1)
rubocop-performance (~> 1.20.2)
thor (1.3.1)
timeout (0.4.1)
transproc (1.1.1)
tzinfo (2.0.6)
Expand Down
1 change: 1 addition & 0 deletions auction_fun_core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "rom", "5.3.0"
spec.add_dependency "rom-sql", "3.6.2"
spec.add_dependency "sidekiq", "7.2.2"
spec.add_dependency "sidekiq-unique-jobs", "8.0.10"
spec.add_dependency "yard", "0.9.36"
spec.add_dependency "zeitwerk", "2.6.13"

Expand Down
1 change: 1 addition & 0 deletions db/migrate/20240229143000_create_auctions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
create_table(:auctions) do
primary_key :id
foreign_key :staff_id, :staffs, null: false
foreign_key :winner_id, :users
column :title, String, null: false
column :description, :text
column :kind, :auction_kinds, null: false
Expand Down
123 changes: 87 additions & 36 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "pry"
require "faker"
require "pry"

I18n.enforce_available_locales = false
Faker::Config.locale = "pt-BR"
Expand All @@ -13,7 +13,6 @@
AuctionFunCore::Application.start(:core)

# Instantiate repos
auction_repository = AuctionFunCore::Repos::AuctionContext::AuctionRepository.new
staff_repository = AuctionFunCore::Repos::StaffContext::StaffRepository.new

# Create root staff. Create as a regular user using the normal flow and after that
Expand Down Expand Up @@ -44,6 +43,19 @@
result.success { |staff| @staff = staff }
end

# Add some users
100.times do
attributes = {
name: Faker::Name.name, email: Faker::Internet.unique.email,
phone: Faker::PhoneNumber.unique.cell_phone_in_e164, password: "password",
password_confirmation: "password"
}
AuctionFunCore::Operations::UserContext::RegistrationOperation.call(attributes) do |result|
result.failure { |failure| raise "Error to create user: #{failure}" }
result.success { |user| puts "Create user with: #{user.to_h}" }
end
end

# Create some standard auctions
(1..15).each do |i|
attributes = {
Expand All @@ -53,7 +65,31 @@
}
AuctionFunCore::Operations::AuctionContext::CreateOperation.call(attributes) do |result|
result.failure { |failure| raise "Error to create standard auction: #{failure}" }
result.success { |auction| puts "Create standard auction with: #{auction.to_h}" }
result.success do |auction|
@auction = auction
puts "Create standard auction with: #{auction.to_h}"
end
end

# Create auctions that have no bid. Multiples if 7
next if (i % 7).zero?

# Increase the value of each new bid by 10%
@current_bid = @auction.minimal_bid_cents
@minimal_percentage = 0.1
# Create some bids for standard auctions based on total users
(2..100).to_a.sample(rand(1..100)).each_with_index do |user_id, index|
@current_bid = index.zero? ? @current_bid : (@current_bid + (@current_bid * @minimal_percentage)).round(half: :up)
bid_params = {
auction_id: @auction.id,
user_id: user_id,
value_cents: @current_bid
}

AuctionFunCore::Operations::BidContext::CreateBidStandardOperation.call(bid_params) do |result|
result.failure { |failure| raise "Error to create bid: #{failure}" }
result.success { |bid| puts "Create standard bid with: #{bid.to_h}" }
end
end
end

Expand All @@ -65,52 +101,67 @@

attributes = {
staff_id: @staff.id, title: Faker::Commerce.product_name, description: Faker::Lorem.paragraph_by_chars,
kind: "penny", started_at: started_at, finished_at: finished_at, stopwatch: stopwatch
kind: "penny", started_at: started_at, finished_at: finished_at, stopwatch: stopwatch,
initial_bid_cents: (i * 100), minimal_bid_cents: (i * 100)
}
AuctionFunCore::Operations::AuctionContext::CreateOperation.call(attributes) do |result|
result.failure { |failure| raise "Error to create penny auction: #{failure}" }
result.success { |auction| puts "Create penny auction with: #{auction.to_h}" }
result.success do |auction|
@auction = auction
puts "Create penny auction with: #{auction.to_h}"
end
end

# Create auctions that have no bid. Multiples if 7
next if (i % 7).zero?

# Create some bids for penny auctions based on total users
(2..100).to_a.sample(rand(1..100)).each do |user_id|
bid_params = {
auction_id: @auction.id,
user_id: user_id,
value_cents: @auction.minimal_bid_cents
}

AuctionFunCore::Operations::BidContext::CreateBidPennyOperation.call(bid_params) do |result|
result.failure { |failure| raise "Error to create bid: #{failure}" }
result.success { |bid| puts "Create penny bid with: #{bid.to_h}" }
end
end
end

# Create some closed auctions
(1..3).each do |i|
(1..5).each do |i|
attributes = {
staff_id: @staff.id, title: Faker::Commerce.product_name, kind: "closed",
started_at: i.hour.from_now, finished_at: i.day.from_now.end_of_day,
staff_id: @staff.id, title: Faker::Commerce.product_name, description: Faker::Lorem.paragraph_by_chars,
kind: "closed", started_at: i.hour.from_now, finished_at: i.day.from_now.end_of_day,
initial_bid_cents: (i * 1000)
}
AuctionFunCore::Operations::AuctionContext::CreateOperation.call(attributes) do |result|
result.failure { |failure| raise "Error to create closed auction: #{failure}" }
result.success { |auction| puts "Create closed auction with: #{auction.to_h}" }
result.success do |auction|
@auction = auction
puts "Create closed auction with: #{auction.to_h}"
end
end
end

# Add some users
100.times do
attributes = {
name: Faker::Name.name, email: Faker::Internet.unique.email,
phone: Faker::PhoneNumber.unique.cell_phone_in_e164, password: "password",
password_confirmation: "password"
}
AuctionFunCore::Operations::UserContext::RegistrationOperation.call(attributes) do |result|
result.failure { |failure| raise "Error to create user: #{failure}" }
result.success { |user| puts "Create user with: #{user.to_h}" }
end
end

# Create some bids
auction_repository.all.each do |auction|
next if auction.id.even?

bid_params = {
auction_id: auction.id,
user_id: rand(2..100),
value_cents: auction.minimal_bid_cents + (auction.minimal_bid_cents * 0.10)
}

"AuctionFunCore::Operations::BidContext::CreateBid#{auction.kind.capitalize}Operation".constantize.call(bid_params) do |result|
result.failure { |failure| raise "Error to create bid: #{failure}" }
result.success { |bid| puts "Create bid with: #{bid.to_h}" }
# Create auctions that have no bid. Pair numbers
next if i.even?

@minimal_bid = @auction.minimal_bid_cents
# Create some bids for closed auctions based on total users
(2..100).to_a.sample(rand(1..100)).each_with_index do |user_id, index|
# Choosing a random value for the bid, obeying the rule that it has to be just greater than the minimum bid.
random_bid = index.zero? ? @minimal_bid : rand((@minimal_bid + 1)..10_000).round
bid_params = {
auction_id: @auction.id,
user_id: user_id,
value_cents: random_bid
}

AuctionFunCore::Operations::BidContext::CreateBidClosedOperation.call(bid_params) do |result|
result.failure { |failure| raise "Error to create bid: #{failure}" }
result.success { |bid| puts "Create closed bid with: #{bid.to_h}" }
end
end
end
9 changes: 9 additions & 0 deletions i18n/en-US/contracts/contracts.en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ en-US:
auction_context:
create:
finished_at: "must be after started time"
post_auction:
participant:
none: "there was no participation from this user in the auction reported"
winner:
wrong: "was not the winner of this auction"
processor:
finish:
invalid_kind: "auction with invalid type"
invalid_status: "auction with invalid status"
59 changes: 59 additions & 0 deletions i18n/en-US/mail/application.en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
en-US:
date:
abbr_day_names:
- Sun
- Mon
- Tue
- Wed
- Thu
- Fri
- Sat
abbr_month_names:
-
- Jan
- Feb
- Mar
- Apr
- May
- Jun
- Jul
- Aug
- Sep
- Oct
- Nov
- Dec
day_names:
- Sunday
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
formats:
default: "%Y-%m-%d"
long: "%B %d, %Y"
short: "%b %d"
month_names:
-
- January
- February
- March
- April
- May
- June
- July
- August
- September
- October
- November
- December
order:
- :year
- :month
- :day
application:
general:
hello: "Hi %{name}"
app_name: "AuctionFun"
team: "Team AuctionFun"
13 changes: 13 additions & 0 deletions i18n/en-US/mail/auction_context/post_auction/participant.en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
en-US:
mail:
auction_context:
post_auction:
participant_mailer:
subject: "Thank you for participating in the auction %{title}"
body:
description: "Thank you for participating in the auction <b>%{title}</b>! Although you weren't the winner this time, we would like to recognize your efforts and share some statistics on your performance:"
stats:
auction_total_bids: "Total auction bids: %{auction_total_bids}"
winner_bid: "Winner bid: %{winner_bid}"
auction_date: "Auction Date"
regards: "We hope you found the experience enriching and invite you to participate in our future auctions. Follow our website and social media to stay up to date with upcoming opportunities."
13 changes: 13 additions & 0 deletions i18n/en-US/mail/auction_context/post_auction/winner.en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
en-US:
mail:
auction_context:
post_auction:
winner_mailer:
subject: "Congratulations! You are the winner of the auction %{title}!"
body:
description: "We are very happy to inform you that you were the winner of our auction <b>%{title}</b>! Your bid was the highest, and you are now the new owner of item. Below is some information and statistics about the auction:"
item:
title: "Auctioned item: <b>%{title}</b>"
winner_bid: "Your Winning Bid: %{winner_bid}"
auction_total_bids: "Total Number of Bids: %{auction_total_bids}"
auction_date: "Auction Date"
9 changes: 9 additions & 0 deletions i18n/pt-BR/contracts/contracts.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ pt-BR:
auction_context:
create:
finished_at: "deve ser depois da hora de início"
post_auction:
participant:
none: "não houve nenhuma participação deste usuário no leilão informado"
winner:
wrong: "não foi o vencedor deste leilão"
processor:
finish:
invalid_kind: "leilão com tipo inválido"
invalid_status: "leilão com status inválido"
Loading
Loading