Skip to content

Commit

Permalink
Merge pull request #1170 from internetee/hotfix-duplicated-deposit-re…
Browse files Browse the repository at this point in the history
…cords

hotfix: check for dublicate deposit record
  • Loading branch information
vohmar authored Oct 25, 2023
2 parents f707f93 + d150148 commit 2229921
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
21 changes: 10 additions & 11 deletions app/jobs/refund_job.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
class RefundJob < ApplicationJob
BASE_URL = AuctionCenter::Application.config
.customization[:billing_system_integration]
.customization[:billing_system_integration]
&.compact&.fetch(:eis_billing_system_base_url, '')
PATH = '/api/v1/refund/auction'.freeze

INITIATOR = 'auction'.freeze

def perform(domain_participate_auction_id, invoice_number)
response = post(PATH, params: { invoice_number: invoice_number })
response = post(PATH, params: { invoice_number: })
d = DomainParticipateAuction.find(domain_participate_auction_id)

if response.status == 200
d.update(status: 'returned')

JSON.parse response.body
else
inform(d, response.body)

JSON.parse response.body
end

JSON.parse response.body
end

def inform(domain_participate_auction, error_message)
admin_emails = User.where(roles: ["administrator"]).pluck(:email)
admin_emails = User.where(roles: ['administrator']).pluck(:email)

InvoiceMailer.refund_failed(admin_emails, domain_participate_auction.auction, domain_participate_auction.user, error_message).deliver_later
InvoiceMailer.refund_failed(admin_emails, domain_participate_auction.auction, domain_participate_auction.user,
error_message).deliver_later
Rails.logger.info error_message
end

def post(path, params = {})
connection.post(path, JSON.dump(params))
connection.post(path, JSON.dump(params))
end

def connection
Expand All @@ -44,9 +43,9 @@ def options
{
headers: {
'Authorization' => "Bearer #{generate_token}",
'Content-Type' => 'application/json',
'Content-Type' => 'application/json'
},
url: BASE_URL,
url: BASE_URL
}
end

Expand Down
11 changes: 7 additions & 4 deletions app/models/domain_participate_auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ class DomainParticipateAuction < ApplicationRecord
belongs_to :user
belongs_to :auction

enum status: ['paid', 'prepayment', 'returned']
validates :user_id, uniqueness: { scope: :auction_id }

scope :search_by_auction_name_and_user_email, ->(origin) {
enum status: %w[paid prepayment returned]

scope :search_by_auction_name_and_user_email, lambda { |origin|
if origin.present?
joins(:user).joins(:auction).where('users.email ILIKE ? OR auctions.domain_name ILIKE ?', "%#{origin}%", "%#{origin}%")
joins(:user).joins(:auction)
.where('users.email ILIKE ? OR auctions.domain_name ILIKE ?', "%#{origin}%", "%#{origin}%")
end
}

Expand All @@ -15,6 +18,6 @@ def self.search(params = {})
end

def send_deposit_status_to_billing_system
EisBilling::DepositStatusService.call(domain_name: auction.domain_name, user_uuid: user.uuid, status: status)
EisBilling::DepositStatusService.call(domain_name: auction.domain_name, user_uuid: user.uuid, status:)
end
end
16 changes: 9 additions & 7 deletions app/services/eis_billing/check_for_deposit_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def initialize(domain_name:, user_uuid:, user_email:, transaction_amount:, invoi
end

def self.call(domain_name:, user_uuid:, user_email:, transaction_amount:, invoice_number:)
new(domain_name: domain_name,
user_uuid: user_uuid,
user_email: user_email,
transaction_amount: transaction_amount,
invoice_number: invoice_number).call
new(domain_name:,
user_uuid:,
user_email:,
transaction_amount:,
invoice_number:).call
end

def call
Expand All @@ -29,13 +29,15 @@ def call
private

def set_available_for_user
auction = Auction.where(domain_name: domain_name).order(created_at: :desc).first
auction = Auction.where(domain_name:).order(created_at: :desc).first

return false unless auction.enable_deposit?
return false unless auction.deposit.to_f <= transaction_amount.to_f

user = User.find_by_uuid(user_uuid)
DomainParticipateAuction.create!(user: user, auction: auction, invoice_number: invoice_number)
return false if auction.domain_participate_auctions.where(user:).present?

DomainParticipateAuction.create!(user:, auction:, invoice_number:)
end
end
end
30 changes: 30 additions & 0 deletions test/services/check_for_deposit_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,34 @@ def test_should_not_create_if_transaction_is_less_of_requirement_sum

refute @auction.allow_to_set_bid?(@user)
end

def test_should_not_create_duplicates
@auction.update(enable_deposit: true, requirement_deposit_in_cents: 50000)
@auction.reload

refute @auction.allow_to_set_bid?(@user)
refute @auction.domain_participate_auctions.where(user: @user).present?

EisBilling::CheckForDepositService.call(
domain_name: @auction.domain_name,
user_uuid: @user.uuid,
user_email: @user.email,
transaction_amount: 500.0,
invoice_number: @invoice.number
)

EisBilling::CheckForDepositService.call(
domain_name: @auction.domain_name,
user_uuid: @user.uuid,
user_email: @user.email,
transaction_amount: 500.0,
invoice_number: @invoice.number
)
@user.reload && @auction.reload

assert @auction.allow_to_set_bid?(@user)
assert @auction.domain_participate_auctions.where(user: @user).present?

assert_equal @auction.domain_participate_auctions.count, 1
end
end

0 comments on commit 2229921

Please sign in to comment.