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

View file

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

View file

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

View file

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

View file

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