auction_fun_core 0.8.5 → 0.8.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.standard.yml +2 -0
- data/CHANGELOG.md +48 -0
- data/Procfile +1 -0
- data/README.md +34 -32
- data/Rakefile +1 -1
- data/auction_fun_core.gemspec +1 -0
- data/db/migrate/20240229143000_create_auctions.rb +1 -0
- data/db/seeds.rb +87 -36
- data/i18n/en-US/contracts/contracts.en-US.yml +12 -0
- data/i18n/en-US/mail/application.en-US.yml +66 -0
- data/i18n/en-US/mail/auction_context/post_auction/participant.en-US.yml +13 -0
- data/i18n/en-US/mail/auction_context/post_auction/winner.en-US.yml +13 -0
- data/i18n/en-US/mail/auction_context/pre_auction/auction_start_reminder.en-US.yml +11 -0
- data/i18n/pt-BR/contracts/contracts.pt-BR.yml +12 -0
- data/i18n/pt-BR/mail/application.pt-BR.yml +66 -0
- data/i18n/pt-BR/mail/auction_context/post_auction/participant.pt-BR.yml +13 -0
- data/i18n/pt-BR/mail/auction_context/post_auction/winner.pt-BR.yml +13 -0
- data/i18n/pt-BR/mail/auction_context/pre_auction/auction_start_reminder.pt-BR.yml +11 -0
- data/i18n/pt-BR/mail/user_context/registration.pt-BR.yml +0 -4
- data/lib/auction_fun_core/business/configuration.rb +31 -0
- data/lib/auction_fun_core/business/token_generator.rb +19 -1
- data/lib/auction_fun_core/contracts/application_contract.rb +9 -1
- data/lib/auction_fun_core/contracts/auction_context/create_contract.rb +35 -20
- data/lib/auction_fun_core/contracts/auction_context/post_auction/participant_contract.rb +64 -0
- data/lib/auction_fun_core/contracts/auction_context/post_auction/winner_contract.rb +62 -0
- data/lib/auction_fun_core/contracts/auction_context/pre_auction/auction_start_reminder_contract.rb +48 -0
- data/lib/auction_fun_core/contracts/auction_context/processor/finish/closed_contract.rb +59 -0
- data/lib/auction_fun_core/contracts/auction_context/processor/finish/penny_contract.rb +60 -0
- data/lib/auction_fun_core/contracts/auction_context/processor/finish/standard_contract.rb +60 -0
- data/lib/auction_fun_core/contracts/auction_context/processor/pause_contract.rb +16 -4
- data/lib/auction_fun_core/contracts/auction_context/processor/start_contract.rb +17 -5
- data/lib/auction_fun_core/contracts/auction_context/processor/unpause_contract.rb +16 -4
- data/lib/auction_fun_core/contracts/bid_context/create_bid_closed_contract.rb +20 -11
- data/lib/auction_fun_core/contracts/bid_context/create_bid_penny_contract.rb +18 -9
- data/lib/auction_fun_core/contracts/bid_context/create_bid_standard_contract.rb +19 -10
- data/lib/auction_fun_core/contracts/staff_context/authentication_contract.rb +18 -4
- data/lib/auction_fun_core/contracts/staff_context/registration_contract.rb +20 -8
- data/lib/auction_fun_core/contracts/user_context/authentication_contract.rb +18 -4
- data/lib/auction_fun_core/contracts/user_context/email_confirmation_contract.rb +17 -2
- data/lib/auction_fun_core/contracts/user_context/phone_confirmation_contract.rb +17 -2
- data/lib/auction_fun_core/contracts/user_context/registration_contract.rb +26 -8
- data/lib/auction_fun_core/entities/auction.rb +52 -2
- data/lib/auction_fun_core/entities/bid.rb +3 -2
- data/lib/auction_fun_core/entities/staff.rb +15 -2
- data/lib/auction_fun_core/entities/user.rb +31 -2
- data/lib/auction_fun_core/events/app.rb +8 -2
- data/lib/auction_fun_core/events/listener.rb +19 -16
- data/lib/auction_fun_core/operations/auction_context/create_operation.rb +37 -11
- data/lib/auction_fun_core/operations/auction_context/post_auction/participant_operation.rb +85 -0
- data/lib/auction_fun_core/operations/auction_context/post_auction/winner_operation.rb +85 -0
- data/lib/auction_fun_core/operations/auction_context/pre_auction/auction_start_reminder_operation.rb +96 -0
- data/lib/auction_fun_core/operations/auction_context/processor/finish/closed_operation.rb +172 -0
- data/lib/auction_fun_core/operations/auction_context/processor/finish/penny_operation.rb +171 -0
- data/lib/auction_fun_core/operations/auction_context/processor/finish/standard_operation.rb +171 -0
- data/lib/auction_fun_core/operations/auction_context/processor/pause_operation.rb +36 -1
- data/lib/auction_fun_core/operations/auction_context/processor/start_operation.rb +23 -11
- data/lib/auction_fun_core/operations/auction_context/processor/unpause_operation.rb +36 -1
- data/lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb +57 -7
- data/lib/auction_fun_core/operations/staff_context/registration_operation.rb +10 -0
- data/lib/auction_fun_core/relations/auctions.rb +252 -30
- data/lib/auction_fun_core/relations/bids.rb +18 -0
- data/lib/auction_fun_core/relations/staffs.rb +1 -1
- data/lib/auction_fun_core/repos/auction_context/auction_repository.rb +40 -11
- data/lib/auction_fun_core/repos/bid_context/bid_repository.rb +27 -5
- data/lib/auction_fun_core/repos/staff_context/staff_repository.rb +63 -21
- data/lib/auction_fun_core/repos/user_context/user_repository.rb +69 -25
- data/lib/auction_fun_core/services/mail/auction_context/post_auction/participant_mailer.rb +37 -0
- data/lib/auction_fun_core/services/mail/auction_context/post_auction/winner_mailer.rb +37 -0
- data/lib/auction_fun_core/services/mail/auction_context/pre_auction/auction_start_reminder_mailer.rb +35 -0
- data/lib/auction_fun_core/services/mail/templates/auction_context/post_auction/participant.html.erb +173 -0
- data/lib/auction_fun_core/services/mail/templates/auction_context/post_auction/winner.html.erb +174 -0
- data/lib/auction_fun_core/services/mail/templates/auction_context/pre_auction/auction_start_reminder.html.erb +192 -0
- data/lib/auction_fun_core/services/mail/templates/user_context/registration.html.erb +2 -2
- data/lib/auction_fun_core/services/mail/user_context/registration_mailer.rb +6 -0
- data/lib/auction_fun_core/version.rb +1 -1
- data/lib/auction_fun_core/workers/application_job.rb +12 -0
- data/lib/auction_fun_core/workers/operations/auction_context/post_auction/participant_operation_job.rb +38 -0
- data/lib/auction_fun_core/workers/operations/auction_context/post_auction/winner_operation_job.rb +37 -0
- data/lib/auction_fun_core/workers/operations/auction_context/pre_auction/auction_start_reminder_operation_job.rb +44 -0
- data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/closed_operation_job.rb +37 -0
- data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/penny_operation_job.rb +40 -0
- data/lib/auction_fun_core/workers/operations/auction_context/processor/finish/standard_operation_job.rb +38 -0
- data/lib/auction_fun_core/workers/operations/auction_context/processor/start_operation_job.rb +6 -3
- data/lib/auction_fun_core/workers/services/mail/auction_context/post_auction/participant_mailer_job.rb +56 -0
- data/lib/auction_fun_core/workers/services/mail/auction_context/post_auction/winner_mailer_job.rb +57 -0
- data/lib/auction_fun_core/workers/services/mail/auction_context/pre_auction/auction_start_reminder_mailer_job.rb +48 -0
- data/lib/auction_fun_core/workers/services/mail/user_context/registration_mailer_job.rb +8 -6
- data/system/providers/background_job.rb +25 -0
- metadata +51 -5
- data/lib/auction_fun_core/contracts/auction_context/processor/finish_contract.rb +0 -27
- data/lib/auction_fun_core/operations/auction_context/processor/finish_operation.rb +0 -61
- data/lib/auction_fun_core/workers/operations/auction_context/processor/finish_operation_job.rb +0 -32
@@ -5,28 +5,40 @@ module AuctionFunCore
|
|
5
5
|
module AuctionContext
|
6
6
|
module Processor
|
7
7
|
##
|
8
|
-
#
|
8
|
+
# This class is designed to validate the initiation of auctions. It ensures
|
9
|
+
# the auction exists and is of a valid kind, and also manages specific rules for auctions
|
10
|
+
# that require a stopwatch, such as penny auctions.
|
11
|
+
#
|
12
|
+
# @example Starting an auction
|
13
|
+
# contract = AuctionFunCore::Contracts::AuctionContext::Processor::StartContract.new
|
14
|
+
# attributes = { auction_id: 123, kind: "penny", stopwatch: 30 }
|
15
|
+
# result = contract.call(attributes)
|
16
|
+
# if result.success?
|
17
|
+
# puts "Auction started successfully."
|
18
|
+
# else
|
19
|
+
# puts "Failed to start auction: #{result.errors.to_h}"
|
20
|
+
# end
|
9
21
|
#
|
10
22
|
class StartContract < Contracts::ApplicationContract
|
11
23
|
include AuctionFunCore::Business::Configuration
|
12
24
|
|
25
|
+
# Repository initialized to retrieve auction data for validation.
|
13
26
|
option :auction_repo, default: proc { Repos::AuctionContext::AuctionRepository.new }
|
14
27
|
|
28
|
+
# Parameters specifying the required input types and fields.
|
15
29
|
params do
|
16
30
|
required(:auction_id).filled(:integer)
|
17
31
|
required(:kind).value(included_in?: AUCTION_KINDS)
|
18
32
|
optional(:stopwatch).filled(:integer)
|
19
33
|
end
|
20
34
|
|
21
|
-
#
|
22
|
-
# Validates if the auction exists in the database.
|
35
|
+
# Validates the existence of the auction.
|
23
36
|
rule(:auction_id) do |context:|
|
24
37
|
context[:auction] ||= auction_repo.by_id(value)
|
25
38
|
key.failure(I18n.t("contracts.errors.custom.not_found")) unless context[:auction]
|
26
39
|
end
|
27
40
|
|
28
|
-
#
|
29
|
-
#
|
41
|
+
# Validates the requirements for the stopwatch in penny auctions.
|
30
42
|
rule(:stopwatch) do
|
31
43
|
# Must be filled if auction kind is type penny.
|
32
44
|
key.failure(I18n.t("contracts.errors.filled?")) if !key? && values[:kind] == "penny"
|
@@ -5,18 +5,30 @@ module AuctionFunCore
|
|
5
5
|
module AuctionContext
|
6
6
|
module Processor
|
7
7
|
##
|
8
|
-
#
|
8
|
+
# This class is designed to validate the resumption of auctions.
|
9
|
+
# It ensures that the auction exists in the database and checks that the auction
|
10
|
+
# is currently in a "paused" status, allowing it to be unpaused.
|
11
|
+
#
|
12
|
+
# @example Unpausing an auction
|
13
|
+
# contract = AuctionFunCore::Contracts::AuctionContext::Processor::UnpauseContract.new
|
14
|
+
# attributes = { auction_id: 123 }
|
15
|
+
# result = contract.call(attributes)
|
16
|
+
# if result.success?
|
17
|
+
# puts "Auction resumed successfully."
|
18
|
+
# else
|
19
|
+
# puts "Failed to resume auction: #{result.errors.to_h}"
|
20
|
+
# end
|
9
21
|
#
|
10
22
|
class UnpauseContract < Contracts::ApplicationContract
|
23
|
+
# Repository initialized to retrieve auction data for validation.
|
11
24
|
option :auction_repository, default: proc { Repos::AuctionContext::AuctionRepository.new }
|
12
25
|
|
26
|
+
# Parameters specifying the required input types and fields.
|
13
27
|
params do
|
14
28
|
required(:auction_id).filled(:integer)
|
15
29
|
end
|
16
30
|
|
17
|
-
#
|
18
|
-
# Validates if the auction exists in the database and and checks if the
|
19
|
-
# auction has a paused status.
|
31
|
+
# Validates the existence of the auction and checks its status.
|
20
32
|
rule(:auction_id) do |context:|
|
21
33
|
context[:auction] ||= auction_repository.by_id(value)
|
22
34
|
key.failure(I18n.t("contracts.errors.custom.not_found")) unless context[:auction]
|
@@ -3,13 +3,27 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module BidContext
|
6
|
-
#
|
6
|
+
# This class validates the creation of new bids for closed-type auctions.
|
7
|
+
# It ensures the bid is placed by a valid user on a valid auction that is open for bids, and that
|
8
|
+
# the bid value meets or exceeds the starting bid required by the auction.
|
9
|
+
# Furthermore, only one bid per participant is allowed.
|
10
|
+
#
|
11
|
+
# @example Creating a bid for a closed auction
|
12
|
+
# contract = AuctionFunCore::Contracts::BidContext::CreateBidClosedContract.new
|
13
|
+
# attributes = { auction_id: 123, user_id: 2, value_cents: 10000 }
|
14
|
+
# result = contract.call(attributes)
|
15
|
+
# if result.success?
|
16
|
+
# puts "Bid created successfully."
|
17
|
+
# else
|
18
|
+
# puts "Failed to create bid: #{result.errors.to_h}"
|
19
|
+
# end
|
7
20
|
class CreateBidClosedContract < Contracts::ApplicationContract
|
21
|
+
# Repositories initialized to retrieve data for validation.
|
8
22
|
option :user_repository, default: proc { Repos::UserContext::UserRepository.new }
|
9
23
|
option :auction_repository, default: proc { Repos::AuctionContext::AuctionRepository.new }
|
10
24
|
option :bid_repository, default: proc { Repos::BidContext::BidRepository.new }
|
11
25
|
|
12
|
-
#
|
26
|
+
# Parameters specifying the required input types and fields.
|
13
27
|
params do
|
14
28
|
required(:auction_id).filled(:integer)
|
15
29
|
required(:user_id).filled(:integer)
|
@@ -21,9 +35,7 @@ module AuctionFunCore
|
|
21
35
|
end
|
22
36
|
end
|
23
37
|
|
24
|
-
#
|
25
|
-
# validate whether the given auction is valid at the database level.
|
26
|
-
# validate if the auction is open to receive bids
|
38
|
+
# Validates the auction's validity and status for receiving bids.
|
27
39
|
rule(:auction_id) do |context:|
|
28
40
|
context[:auction] ||= auction_repository.by_id(value)
|
29
41
|
|
@@ -42,9 +54,7 @@ module AuctionFunCore
|
|
42
54
|
end
|
43
55
|
end
|
44
56
|
|
45
|
-
#
|
46
|
-
# Validate whether the given user is valid at the database level.
|
47
|
-
# Validates if user has already placed a bid
|
57
|
+
# Validates the user's existence and checks if they have already placed a bid.
|
48
58
|
rule(:user_id) do |context:|
|
49
59
|
context[:user] ||= user_repository.by_id(value)
|
50
60
|
|
@@ -57,8 +67,7 @@ module AuctionFunCore
|
|
57
67
|
end
|
58
68
|
end
|
59
69
|
|
60
|
-
#
|
61
|
-
# The bid amount must be greater than or equal to the starting bid.
|
70
|
+
# Validates that the bid amount is greater than or equal to the auction's starting bid.
|
62
71
|
rule(:value_cents) do |context:|
|
63
72
|
unless rule_error?(:user_id)
|
64
73
|
closed_auction_bid_value_is_gteq_initial_bid?(key, value, context[:auction].initial_bid_cents)
|
@@ -67,7 +76,7 @@ module AuctionFunCore
|
|
67
76
|
|
68
77
|
private
|
69
78
|
|
70
|
-
#
|
79
|
+
# Helper method to check if the bid amount meets the minimum required bid.
|
71
80
|
def closed_auction_bid_value_is_gteq_initial_bid?(key, value_cents, minimal_bid_cents)
|
72
81
|
return unless value_cents < minimal_bid_cents
|
73
82
|
|
@@ -3,12 +3,25 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module BidContext
|
6
|
-
#
|
6
|
+
# This class validates the creation of new bids for penny-type auctions.
|
7
|
+
# It guarantees that the bid can only be made within the correct status of this type of auction,
|
8
|
+
# in addition, the participant must have sufficient balance in their wallet to place a new bid.
|
9
|
+
#
|
10
|
+
# @example Creating a bid for a penny auction
|
11
|
+
# contract = AuctionFunCore::Contracts::BidContext::CreateBidPennyContract.new
|
12
|
+
# attributes = { auction_id: 123, user_id: 2 }
|
13
|
+
# result = contract.call(attributes)
|
14
|
+
# if result.success?
|
15
|
+
# puts "Bid created successfully."
|
16
|
+
# else
|
17
|
+
# puts "Failed to create bid: #{result.errors.to_h}"
|
18
|
+
# end
|
7
19
|
class CreateBidPennyContract < Contracts::ApplicationContract
|
20
|
+
# Repositories initialized to retrieve data for validation.
|
8
21
|
option :user_repo, default: proc { Repos::UserContext::UserRepository.new }
|
9
22
|
option :auction_repo, default: proc { Repos::AuctionContext::AuctionRepository.new }
|
10
23
|
|
11
|
-
#
|
24
|
+
# Parameters specifying the required input types and fields.
|
12
25
|
params do
|
13
26
|
required(:auction_id).filled(:integer)
|
14
27
|
required(:user_id).filled(:integer)
|
@@ -19,9 +32,7 @@ module AuctionFunCore
|
|
19
32
|
end
|
20
33
|
end
|
21
34
|
|
22
|
-
#
|
23
|
-
# validate whether the given auction is valid at the database level.
|
24
|
-
# validate if the auction is open to receive bids
|
35
|
+
# Validates the auction's validity, kind, and status for receiving bids.
|
25
36
|
rule(:auction_id) do |context:|
|
26
37
|
context[:auction] ||= auction_repo.by_id(value)
|
27
38
|
|
@@ -40,9 +51,7 @@ module AuctionFunCore
|
|
40
51
|
end
|
41
52
|
end
|
42
53
|
|
43
|
-
#
|
44
|
-
# Validate whether the given user is valid at the database level.
|
45
|
-
# Validates if user has enough balance to bid.
|
54
|
+
# Validates the user's existence and ensures they have enough balance to bid.
|
46
55
|
rule(:user_id) do |context:|
|
47
56
|
context[:user] ||= user_repo.by_id(value)
|
48
57
|
|
@@ -59,7 +68,7 @@ module AuctionFunCore
|
|
59
68
|
|
60
69
|
private
|
61
70
|
|
62
|
-
#
|
71
|
+
# Helper method to check if the user has sufficient balance to place a bid in a penny auction.
|
63
72
|
def penny_auction_check_user_has_balance?(key, auction_bid_cents, balance_cents)
|
64
73
|
key.failure(I18n.t("contracts.errors.custom.bids.insufficient_balance")) if balance_cents < auction_bid_cents
|
65
74
|
end
|
@@ -3,12 +3,25 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module BidContext
|
6
|
-
#
|
6
|
+
# This class validates the creation of new bids for standard-type auctions.
|
7
|
+
# It ensures the bid is placed by a valid user on a valid auction that is open for bids,
|
8
|
+
# and that the bid value meets or exceeds the minimum bid required by the auction.
|
9
|
+
#
|
10
|
+
# @example Creating a bid for a standard auction
|
11
|
+
# contract = AuctionFunCore::Contracts::BidContext::CreateBidStandardContract.new
|
12
|
+
# attributes = { auction_id: 1, user_id: 2, value_cents: 5000 }
|
13
|
+
# result = contract.call(attributes)
|
14
|
+
# if result.success?
|
15
|
+
# puts "Bid created successfully."
|
16
|
+
# else
|
17
|
+
# puts "Failed to create bid: #{result.errors.to_h}"
|
18
|
+
# end
|
7
19
|
class CreateBidStandardContract < Contracts::ApplicationContract
|
20
|
+
# Repositories initialized to retrieve data for validation.
|
8
21
|
option :user_repo, default: proc { Repos::UserContext::UserRepository.new }
|
9
22
|
option :auction_repo, default: proc { Repos::AuctionContext::AuctionRepository.new }
|
10
23
|
|
11
|
-
#
|
24
|
+
# Parameters specifying the required input types and fields.
|
12
25
|
params do
|
13
26
|
required(:auction_id).filled(:integer)
|
14
27
|
required(:user_id).filled(:integer)
|
@@ -20,9 +33,7 @@ module AuctionFunCore
|
|
20
33
|
end
|
21
34
|
end
|
22
35
|
|
23
|
-
#
|
24
|
-
# validate whether the given auction is valid at the database level.
|
25
|
-
# validate if the auction is open to receive bids
|
36
|
+
# Validates the auction's validity, kind, and status for receiving bids.
|
26
37
|
rule(:auction_id) do |context:|
|
27
38
|
context[:auction] ||= auction_repo.by_id(value)
|
28
39
|
|
@@ -41,22 +52,20 @@ module AuctionFunCore
|
|
41
52
|
end
|
42
53
|
end
|
43
54
|
|
44
|
-
#
|
45
|
-
# Validate whether the given user is valid at the database level.
|
55
|
+
# Validates the user's existence in the database.
|
46
56
|
rule(:user_id) do |context:|
|
47
57
|
context[:user] ||= user_repo.by_id(value)
|
48
58
|
key.failure(I18n.t("contracts.errors.custom.not_found")) unless context[:user]
|
49
59
|
end
|
50
60
|
|
51
|
-
#
|
52
|
-
# must be greater than or equal to the auction's minimal bid.
|
61
|
+
# Validates that the bid amount is greater than or equal to the auction's minimum bid.
|
53
62
|
rule(:value_cents) do |context:|
|
54
63
|
standard_auction_valid_bid?(key, value, context[:auction].minimal_bid_cents)
|
55
64
|
end
|
56
65
|
|
57
66
|
private
|
58
67
|
|
59
|
-
#
|
68
|
+
# Helper method to check if the bid amount meets the minimum required bid.
|
60
69
|
def standard_auction_valid_bid?(key, value_cents, minimal_bid_cents)
|
61
70
|
return if value_cents >= minimal_bid_cents
|
62
71
|
|
@@ -3,12 +3,27 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module StaffContext
|
6
|
-
#
|
6
|
+
# This class is designed to authenticate staff members.
|
7
|
+
# It validates the login and password, checking their format and verifying them against the database records.
|
8
|
+
#
|
9
|
+
# @example Authenticating a staff member
|
10
|
+
# contract = AuctionFunCore::Contracts::StaffContext::AuthenticationContract.new
|
11
|
+
# attributes = { login: 'staff@example.com', password: 'securePassword123' }
|
12
|
+
# result = contract.call(attributes)
|
13
|
+
# if result.success?
|
14
|
+
# puts "Authentication successful."
|
15
|
+
# else
|
16
|
+
# puts "Authentication failed: #{result.errors.to_h}"
|
17
|
+
# end
|
18
|
+
#
|
7
19
|
class AuthenticationContract < ApplicationContract
|
20
|
+
# Scope for internationalization (i18n) entries specific to errors in this contract.
|
8
21
|
I18N_SCOPE = "contracts.errors.custom.default"
|
9
22
|
|
23
|
+
# Repositories initialized to retrieve data for validation.
|
10
24
|
option :staff_repository, default: proc { Repos::StaffContext::StaffRepository.new }
|
11
25
|
|
26
|
+
# Parameters specifying the required input types and fields.
|
12
27
|
params do
|
13
28
|
required(:login)
|
14
29
|
required(:password)
|
@@ -18,12 +33,11 @@ module AuctionFunCore
|
|
18
33
|
end
|
19
34
|
end
|
20
35
|
|
36
|
+
# Additional rules for validating the format of login and password.
|
21
37
|
rule(:login).validate(:login_format)
|
22
38
|
rule(:password).validate(:password_format)
|
23
39
|
|
24
|
-
#
|
25
|
-
# Searches for the staff in the database from the login, and, if found,
|
26
|
-
# compares the entered password.
|
40
|
+
# Validates the presence of the staff member in the database and checks if the password matches.
|
27
41
|
rule do |context:|
|
28
42
|
next if (rule_error?(:login) || schema_error?(:login)) || (rule_error?(:password) || schema_error?(:password))
|
29
43
|
|
@@ -3,13 +3,27 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module StaffContext
|
6
|
-
#
|
6
|
+
# This class is designed to create staff members.
|
7
|
+
# It validates the parameters, ensuring their format and uniqueness.
|
8
|
+
#
|
9
|
+
# @example Registering a new staff member
|
10
|
+
# contract = AuctionFunCore::Contracts::StaffContext::RegistrationContract.new
|
11
|
+
# attributes = { name: 'John Doe', email: 'john.doe@example.com', phone: '1234567890' }
|
12
|
+
# result = contract.call(attributes)
|
13
|
+
# if result.success?
|
14
|
+
# puts "Staff member registered successfully."
|
15
|
+
# else
|
16
|
+
# puts "Failed to register staff member: #{result.errors.to_h}"
|
17
|
+
# end
|
18
|
+
#
|
7
19
|
class RegistrationContract < ApplicationContract
|
20
|
+
# Scope for internationalization (i18n) entries specific to errors in this contract.
|
8
21
|
I18N_SCOPE = "contracts.errors.custom.default"
|
9
22
|
|
23
|
+
# Repositories initialized to retrieve data for validation.
|
10
24
|
option :staff_repository, default: proc { Repos::StaffContext::StaffRepository.new }
|
11
25
|
|
12
|
-
#
|
26
|
+
# Parameters specifying the required input types and fields.
|
13
27
|
params do
|
14
28
|
required(:name)
|
15
29
|
required(:email)
|
@@ -19,27 +33,25 @@ module AuctionFunCore
|
|
19
33
|
result.to_h.compact
|
20
34
|
end
|
21
35
|
|
22
|
-
#
|
36
|
+
# Normalizes and adds default values after coercion.
|
23
37
|
after(:value_coercer) do |result|
|
24
38
|
result.update(email: result[:email].strip.downcase) if result[:email]
|
25
39
|
result.update(phone: result[:phone].tr_s("^0-9", "")) if result[:phone]
|
26
40
|
end
|
27
41
|
end
|
28
42
|
|
43
|
+
# Validates the format of the staff member's name.
|
29
44
|
rule(:name).validate(:name_format)
|
30
45
|
|
31
|
-
#
|
32
|
-
# It must validate the format and uniqueness in the database.
|
46
|
+
# It ensures email format and checks for uniqueness in the database.
|
33
47
|
rule(:email).validate(:email_format)
|
34
48
|
rule(:email) do
|
35
|
-
# Email should be unique on database
|
36
49
|
if !rule_error?(:email) && staff_repository.exists?(email: value)
|
37
50
|
key.failure(I18n.t(:taken, scope: I18N_SCOPE))
|
38
51
|
end
|
39
52
|
end
|
40
53
|
|
41
|
-
#
|
42
|
-
# It must validate the format and uniqueness in the database.
|
54
|
+
# It ensures phone format and checks for uniqueness in the database.
|
43
55
|
rule(:phone).validate(:phone_format)
|
44
56
|
rule(:phone) do
|
45
57
|
if !rule_error?(:phone) && staff_repository.exists?(phone: value)
|
@@ -3,12 +3,27 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module UserContext
|
6
|
-
#
|
6
|
+
# This class is designed to authenticate users.
|
7
|
+
# It verifies the provided login credentials against stored user data in the system.
|
8
|
+
#
|
9
|
+
# @example Authenticating a user
|
10
|
+
# contract = AuctionFunCore::Contracts::UserContext::AuthenticationContract.new
|
11
|
+
# attributes = { login: 'example_user', password: 'securePassword123' }
|
12
|
+
# result = contract.call(attributes)
|
13
|
+
# if result.success?
|
14
|
+
# puts "User authenticated successfully."
|
15
|
+
# else
|
16
|
+
# puts "Authentication failed: #{result.errors.to_h}"
|
17
|
+
# end
|
18
|
+
#
|
7
19
|
class AuthenticationContract < ApplicationContract
|
20
|
+
# Scope for internationalization (i18n) entries specific to errors in this contract.
|
8
21
|
I18N_SCOPE = "contracts.errors.custom.default"
|
9
22
|
|
23
|
+
# Repositories initialized to retrieve data for validation.
|
10
24
|
option :user_repository, default: proc { Repos::UserContext::UserRepository.new }
|
11
25
|
|
26
|
+
# Parameters specifying the required input types and fields.
|
12
27
|
params do
|
13
28
|
required(:login)
|
14
29
|
required(:password)
|
@@ -18,12 +33,11 @@ module AuctionFunCore
|
|
18
33
|
end
|
19
34
|
end
|
20
35
|
|
36
|
+
# Additional rules for validating the format of login and password.
|
21
37
|
rule(:login).validate(:login_format)
|
22
38
|
rule(:password).validate(:password_format)
|
23
39
|
|
24
|
-
#
|
25
|
-
# Searches for the user in the database from the login, and, if found,
|
26
|
-
# compares the entered password.
|
40
|
+
# Validates the presence of the user in the database and checks if the password matches.
|
27
41
|
rule do |context:|
|
28
42
|
next if (rule_error?(:login) || schema_error?(:login)) || (rule_error?(:password) || schema_error?(:password))
|
29
43
|
|
@@ -3,12 +3,27 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module UserContext
|
6
|
-
#
|
6
|
+
# This class provides validation for confirming email addresses.
|
7
|
+
# It verifies the provided email confirmation token against stored user data in the system.
|
8
|
+
#
|
9
|
+
# @example Confirming an email address
|
10
|
+
# contract = AuctionFunCore::Contracts::UserContext::EmailConfirmationContract.new
|
11
|
+
# attributes = { email_confirmation_token: 'example_token' }
|
12
|
+
# result = contract.call(attributes)
|
13
|
+
# if result.success?
|
14
|
+
# puts "Email address enabled for confirmation."
|
15
|
+
# else
|
16
|
+
# puts "Failed to confirm email address: #{result.errors.to_h}"
|
17
|
+
# end
|
18
|
+
#
|
7
19
|
class EmailConfirmationContract < ApplicationContract
|
20
|
+
# Scope for internationalization (i18n) entries specific to errors in this contract.
|
8
21
|
I18N_SCOPE = "contracts.errors.custom.default"
|
9
22
|
|
23
|
+
# Repositories initialized to retrieve data for validation.
|
10
24
|
option :user_repository, default: proc { Repos::UserContext::UserRepository.new }
|
11
25
|
|
26
|
+
# Parameters specifying the required input types and fields.
|
12
27
|
params do
|
13
28
|
required(:email_confirmation_token)
|
14
29
|
|
@@ -17,7 +32,6 @@ module AuctionFunCore
|
|
17
32
|
end
|
18
33
|
end
|
19
34
|
|
20
|
-
# Validation for email_confirmation_token.
|
21
35
|
# Searches for the user in the database from the email_confirmation_token
|
22
36
|
rule do |context:|
|
23
37
|
next if schema_error?(:email_confirmation_token)
|
@@ -29,6 +43,7 @@ module AuctionFunCore
|
|
29
43
|
key(:email_confirmation_token).failure(I18n.t("not_found", scope: I18N_SCOPE))
|
30
44
|
end
|
31
45
|
|
46
|
+
# Additional validation to check if the user account associated with the token is active.
|
32
47
|
rule do |context:|
|
33
48
|
next if context[:user].blank? || context[:user].active?
|
34
49
|
|
@@ -3,12 +3,27 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module UserContext
|
6
|
-
#
|
6
|
+
# This class provides validation for confirming phone numbers.
|
7
|
+
# It verifies the provided phone confirmation token against stored user data in the system.
|
8
|
+
#
|
9
|
+
# @example Confirming a phone number
|
10
|
+
# contract = AuctionFunCore::Contracts::UserContext::PhoneConfirmationContract.new
|
11
|
+
# attributes = { phone_confirmation_token: 'example_token' }
|
12
|
+
# result = contract.call(attributes)
|
13
|
+
# if result.success?
|
14
|
+
# puts "Phone number enabled for confirmation."
|
15
|
+
# else
|
16
|
+
# puts "Failed to confirm phone number: #{result.errors.to_h}"
|
17
|
+
# end
|
18
|
+
#
|
7
19
|
class PhoneConfirmationContract < ApplicationContract
|
20
|
+
# Scope for internationalization (i18n) entries specific to errors in this contract.
|
8
21
|
I18N_SCOPE = "contracts.errors.custom.default"
|
9
22
|
|
23
|
+
# Repositories initialized to retrieve data for validation.
|
10
24
|
option :user_repository, default: proc { Repos::UserContext::UserRepository.new }
|
11
25
|
|
26
|
+
# Parameters specifying the required input types and fields.
|
12
27
|
params do
|
13
28
|
required(:phone_confirmation_token)
|
14
29
|
|
@@ -17,7 +32,6 @@ module AuctionFunCore
|
|
17
32
|
end
|
18
33
|
end
|
19
34
|
|
20
|
-
# Validation for token_confirmation_token.
|
21
35
|
# Searches for the user in the database from the phone_confirmation_token
|
22
36
|
rule do |context:|
|
23
37
|
next if schema_error?(:phone_confirmation_token)
|
@@ -29,6 +43,7 @@ module AuctionFunCore
|
|
29
43
|
key(:phone_confirmation_token).failure(I18n.t("not_found", scope: I18N_SCOPE))
|
30
44
|
end
|
31
45
|
|
46
|
+
# Additional validation to check if the user account associated with the token is active.
|
32
47
|
rule do |context:|
|
33
48
|
next if context[:user].blank? || context[:user].active?
|
34
49
|
|
@@ -3,13 +3,33 @@
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Contracts
|
5
5
|
module UserContext
|
6
|
-
#
|
6
|
+
# This class is designed to create new users.
|
7
|
+
# It validates the parameters, ensuring their format and uniqueness.
|
8
|
+
#
|
9
|
+
# @example Registering a new staff member
|
10
|
+
# contract = AuctionFunCore::Contracts::StaffContext::RegistrationContract.new
|
11
|
+
# attributes = {
|
12
|
+
# name: 'John Doe',
|
13
|
+
# email: 'john.doe@example.com',
|
14
|
+
# phone: '1234567890'
|
15
|
+
# password: 'password',
|
16
|
+
# password_confirmation: 'password'
|
17
|
+
# }
|
18
|
+
# result = contract.call(attributes)
|
19
|
+
# if result.success?
|
20
|
+
# puts "New user registered successfully."
|
21
|
+
# else
|
22
|
+
# puts "Failed to register new user: #{result.errors.to_h}"
|
23
|
+
# end
|
24
|
+
#
|
7
25
|
class RegistrationContract < ApplicationContract
|
26
|
+
# Scope for internationalization (i18n) entries specific to errors in this contract.
|
8
27
|
I18N_SCOPE = "contracts.errors.custom.default"
|
9
28
|
|
29
|
+
# Repositories initialized to retrieve data for validation.
|
10
30
|
option :user_repository, default: proc { Repos::UserContext::UserRepository.new }
|
11
31
|
|
12
|
-
#
|
32
|
+
# Parameters specifying the required input types and fields.
|
13
33
|
params do
|
14
34
|
required(:name)
|
15
35
|
required(:email)
|
@@ -21,17 +41,17 @@ module AuctionFunCore
|
|
21
41
|
result.to_h.compact
|
22
42
|
end
|
23
43
|
|
24
|
-
#
|
44
|
+
# Normalizes and adds default values after coercion.
|
25
45
|
after(:value_coercer) do |result|
|
26
46
|
result.update(email: result[:email].strip.downcase) if result[:email]
|
27
47
|
result.update(phone: result[:phone].tr_s("^0-9", "")) if result[:phone]
|
28
48
|
end
|
29
49
|
end
|
30
50
|
|
51
|
+
# Validates the format of the user's name.
|
31
52
|
rule(:name).validate(:name_format)
|
32
53
|
|
33
|
-
#
|
34
|
-
# It must validate the format and uniqueness in the database.
|
54
|
+
# It ensures email format and checks for uniqueness in the database.
|
35
55
|
rule(:email).validate(:email_format)
|
36
56
|
rule(:email) do
|
37
57
|
# Email should be unique on database
|
@@ -40,8 +60,7 @@ module AuctionFunCore
|
|
40
60
|
end
|
41
61
|
end
|
42
62
|
|
43
|
-
#
|
44
|
-
# It must validate the format and uniqueness in the database.
|
63
|
+
# It ensures phone format and checks for uniqueness in the database.
|
45
64
|
rule(:phone).validate(:phone_format)
|
46
65
|
rule(:phone) do
|
47
66
|
if !rule_error?(:phone) && user_repository.exists?(phone: value)
|
@@ -49,7 +68,6 @@ module AuctionFunCore
|
|
49
68
|
end
|
50
69
|
end
|
51
70
|
|
52
|
-
# Validation for password.
|
53
71
|
# Check if the confirmation matches the password.
|
54
72
|
rule(:password).validate(:password_format)
|
55
73
|
rule(:password, :password_confirmation) do
|
@@ -2,16 +2,66 @@
|
|
2
2
|
|
3
3
|
module AuctionFunCore
|
4
4
|
module Entities
|
5
|
-
|
6
|
-
# to
|
5
|
+
##
|
6
|
+
# Defines the Auction class as Entity. It appears to be a simple data structure
|
7
|
+
# class representing auction-related information.
|
7
8
|
class Auction < ROM::Struct
|
9
|
+
# Retrieves the initial bid amount for an auction as a Money object.
|
10
|
+
#
|
11
|
+
# This method creates and returns a new Money object that represents the initial bid
|
12
|
+
# amount required to start bidding in the auction. It utilizes `initial_bid_cents` and
|
13
|
+
# `initial_bid_currency` attributes to construct the Money object, ensuring that the amount
|
14
|
+
# is correctly represented in the specified currency.
|
15
|
+
#
|
16
|
+
# @return [Money] Returns a Money object representing the initial bid amount with the specified currency.
|
8
17
|
def initial_bid
|
9
18
|
Money.new(initial_bid_cents, initial_bid_currency)
|
10
19
|
end
|
11
20
|
|
21
|
+
# Retrieves the minimal bid amount for an auction as a Money object.
|
22
|
+
#
|
23
|
+
# This method creates and returns a new Money object that represents the minimal bid
|
24
|
+
# amount required to participate in the auction. It uses `minimal_bid_cents` and
|
25
|
+
# `minimal_bid_currency` attributes to construct the Money object.
|
26
|
+
#
|
27
|
+
# @return [Money] Returns a Money object representing the minimal bid amount with the appropriate currency.
|
12
28
|
def minimal_bid
|
13
29
|
Money.new(minimal_bid_cents, minimal_bid_currency)
|
14
30
|
end
|
31
|
+
|
32
|
+
# Checks if an auction has a winner.
|
33
|
+
#
|
34
|
+
# This method determines if an auction has a winner based on the presence of a `winner_id`.
|
35
|
+
# It returns true if the `winner_id` is present, indicating that the auction has concluded
|
36
|
+
# with a winning bidder.
|
37
|
+
#
|
38
|
+
# @return [Boolean] Returns `true` if there is a winner for the auction, otherwise returns `false`.
|
39
|
+
def winner?
|
40
|
+
winner_id.present?
|
41
|
+
end
|
42
|
+
|
43
|
+
# Checks if an auction has already started.
|
44
|
+
#
|
45
|
+
# This method determines if an auction has begun based on its status and by comparing
|
46
|
+
# the auction's start time (`started_at`) with the current time. An auction is considered
|
47
|
+
# started if it is no longer scheduled (i.e., its status is not "scheduled") and
|
48
|
+
# the start time (`started_at`) is equal to or before the current time.
|
49
|
+
# @return [Boolean] Returns `true` if the auction has already started, otherwise returns `false`.
|
50
|
+
def started?
|
51
|
+
status != "scheduled" && Time.current > started_at
|
52
|
+
end
|
53
|
+
|
54
|
+
# Checks if an auction has not started yet.
|
55
|
+
#
|
56
|
+
# This method verifies if an auction is still in the "scheduled" status and whether
|
57
|
+
# its start time (`started_at`) is still in the future compared to the current time.
|
58
|
+
# The auction is considered not started if it is scheduled and the start time
|
59
|
+
# has not yet been reached.
|
60
|
+
#
|
61
|
+
# @return [Boolean] Returns `true` if the auction has not started yet, otherwise returns `false`.
|
62
|
+
def not_started?
|
63
|
+
status == "scheduled" && Time.current <= started_at
|
64
|
+
end
|
15
65
|
end
|
16
66
|
end
|
17
67
|
end
|