Fix price checking for all the domains

This commit is contained in:
Alex Sherman 2020-12-14 13:52:21 +05:00
parent 7d2aad570e
commit a954f3c8e6
5 changed files with 51 additions and 34 deletions

View file

@ -10,26 +10,24 @@ class Registrar
def bulk_renew
authorize! :manage, :repp
@expire_date = params[:expire_date].to_date
@domains = domains_by_date(@expire_date)
@period = params[:period]
set_form_data
if domain_ids_for_bulk_renew.present?
domains = Epp::Domain.where(id: domain_ids_for_bulk_renew).to_a
task = Domains::BulkRenew::Start.run(domains: domains,
period_element: @period,
registrar: current_registrar_user.registrar)
flash[:notice] = if task.valid?
t(:bulk_renew_completed)
else
task.errors.full_messages.join(' and ')
end
task = renew_task(domains)
flash[:notice] = flash_message(task)
end
render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_renew }
end
private
def set_form_data
@expire_date = params[:expire_date].to_date
@domains = domains_by_date(@expire_date)
@period = params[:period]
end
def available_contacts
current_registrar_user.registrar.contacts.order(:name).pluck(:name, :code)
end
@ -47,7 +45,21 @@ class Registrar
end
def domain_ids_for_bulk_renew
params.dig('domain_ids')&.reject{ |id| id.blank? }
params.dig('domain_ids')&.reject { |id| id.blank? }
end
def renew_task(domains)
Domains::BulkRenew::Start.run(domains: domains,
period_element: @period,
registrar: current_registrar_user.registrar)
end
def flash_message(task)
if task.valid?
t(:bulk_renew_completed)
else
task.errors.full_messages.join(' and ')
end
end
end
end

View file

@ -8,7 +8,7 @@ module Domains
object :registrar
def execute
in_transaction_with_retries_do {
in_transaction_with_retries do
success = domain.renew(domain.valid_to, period, unit)
if success
@ -17,7 +17,7 @@ module Domains
else
errors.add(:domain, I18n.t('domain_renew_error_for_domain', domain: domain.name))
end
}
end
end
def check_balance
@ -36,7 +36,7 @@ module Domains
price: domain_pricelist)
end
def in_transaction_with_retries_do
def in_transaction_with_retries
ActiveRecord::Base.transaction(isolation: :serializable) do
yield if block_given?
end

View file

@ -8,7 +8,7 @@ module Domains
object :registrar
def execute
if mass_check_balance.valid?
if mass_check_balance.valid? && mass_check_balance.result
domains.each do |domain|
Domains::BulkRenew::SingleDomainRenew.run(domain: domain,
period: period,
@ -34,7 +34,8 @@ module Domains
Domains::CheckBalance::Mass.run(domains: domains,
operation: 'renew',
period: period,
unit: unit)
unit: unit,
balance: registrar.balance)
end
end
end

View file

@ -7,14 +7,25 @@ module Domains
string :operation
integer :period
string :unit
float :balance
attr_accessor :total_price
def execute
calculate_total_price
balance >= @total_price
end
def calculate_total_price
@total_price = 0
domains.each do |domain|
compose(Domains::CheckBalance::SingleDomain,
domain: domain,
operation: 'renew',
period: period,
unit: unit)
task = Domains::CheckBalance::SingleDomain.run(domain: domain,
operation: 'renew',
period: period,
unit: unit)
task.valid? ? @total_price += task.result : errors.merge!(task.errors)
end
end
end

View file

@ -9,18 +9,11 @@ module Domains
string :unit
def execute
if domain_pricelist.try(:price) # checking if price list is not found
if current_user.registrar.balance < domain_pricelist.price.amount
errors.add(:domain, I18n.t('billing_failure_credit_balance_low_for_domain',
domain: domain.name))
return false
end
else
errors.add(:domain, I18n.t(:active_price_missing_for_operation_with_domain,
domain: domain.name))
return false
end
true
return domain_pricelist.price.amount if domain_pricelist.try(:price)
errors.add(:domain, I18n.t(:active_price_missing_for_operation_with_domain,
domain: domain.name))
false
end
private