Refactor domain transfer

#694
This commit is contained in:
Artur Beljajev 2018-02-19 02:52:35 +02:00
parent 34f1e2be2f
commit 8606cf7b48
4 changed files with 73 additions and 18 deletions

View file

@ -25,7 +25,7 @@ module Repp
if domain
if domain.transfer_code == transfer_code
domain.transfer(new_registrar)
DomainTransfer.request(domain, new_registrar)
successful_domain_transfers << { type: 'domain_transfer' }
else
errors << { title: "#{domain_name} transfer code is wrong" }

View file

@ -10,27 +10,10 @@ module Concerns::Domain::Transferable
end
def transfer(new_registrar)
old_registrar = registrar
self.registrar = new_registrar
regenerate_transfer_code
contact_codes = contacts.pluck(:code).sort.uniq
registrant_code = registrant.code
transaction do
old_registrar.messages.create!(
body: I18n.t('domain_transfer_was_approved', contacts: contact_codes, registrant: registrant_code),
attached_obj_id: id,
attached_obj_type: self.class.name
)
domain_transfers.create!(
transfer_requested_at: Time.zone.now,
old_registrar: old_registrar,
new_registrar: new_registrar
)
transfer_contacts(new_registrar)
save(validate: false)
end

View file

@ -11,6 +11,25 @@ class DomainTransfer < ActiveRecord::Base
before_create :set_wait_until
class << self
def request(domain, new_registrar)
domain_transfer = create!(
transfer_requested_at: Time.zone.now,
domain: domain,
old_registrar: domain.registrar,
new_registrar: new_registrar
)
domain_transfer.approve if approve_automatically?
end
private
def approve_automatically?
Setting.transfer_wait_time.zero?
end
end
def set_wait_until
wait_time = Setting.transfer_wait_time
return if wait_time == 0
@ -45,4 +64,27 @@ class DomainTransfer < ActiveRecord::Base
attached_obj_type: self.class.to_s
)
end
def approve
transaction do
self.status = SERVER_APPROVED
save!
notify_old_registrar
domain.transfer(new_registrar)
end
end
private
def notify_old_registrar
old_contacts_codes = domain.contacts.pluck(:code).sort.uniq
old_registrant_code = domain.registrant.code
old_registrar.messages.create!(
body: I18n.t('domain_transfer_was_approved', contacts: old_contacts_codes, registrant: old_registrant_code),
attached_obj_id: id,
attached_obj_type: self.class.name
)
end
end

View file

@ -0,0 +1,30 @@
require 'test_helper'
class DomainTransferTest < ActiveSupport::TestCase
def setup
@domain_transfer = domain_transfers(:shop)
end
def test_approval
@domain_transfer.approve
@domain_transfer.reload
assert @domain_transfer.approved?
end
def test_notifies_old_registrar_on_approval
old_registrar = @domain_transfer.old_registrar
assert_difference -> { old_registrar.messages.count } do
@domain_transfer.approve
end
body = 'Domain transfer was approved, associated contacts were: ["jane-001", "william-001"] and registrant was john-001'
id = @domain_transfer.id
class_name = @domain_transfer.class.name
message = old_registrar.messages.last
assert_equal body, message.body
assert_equal id, message.attached_obj_id
assert_equal class_name, message.attached_obj_type
end
end