feat(admin): notify registrar about domain status changes

- Add inform_registrar_about_status_changes method to detect added and removed statuses after domain update.
- Send notification to registrar with details of which statuses were set and which were removed.
- Integrate notification logic into domain update action.
This commit is contained in:
oleghasjanov 2025-07-17 15:04:48 +03:00
parent fa03ab92dd
commit a864056fbd
2 changed files with 37 additions and 0 deletions

View file

@ -31,6 +31,7 @@ module Admin
if @domain.update(dp)
flash[:notice] = I18n.t('domain_updated')
inform_registrar_about_status_changes
redirect_to [:admin, @domain]
else
@domain.reload
@ -111,6 +112,20 @@ module Admin
end
end
def inform_registrar_about_status_changes
return unless @domain.saved_change_to_statuses?
old_statuses, new_statuses = @domain.saved_change_to_statuses
removed = old_statuses - new_statuses
added = new_statuses - old_statuses
msg = []
msg << "Set on #{@domain.name}: #{added.join(', ')}" unless added.empty?
msg << "Removed from #{@domain.name}: #{removed.join(', ')}" unless removed.empty?
@domain.registrar.notifications.create!(text: msg.join('. ')) if msg.any?
end
def build_associations
@server_statuses = @domain.statuses.select { |x| DomainStatus::SERVER_STATUSES.include?(x) }
@server_statuses = [nil] if @server_statuses.empty?

View file

@ -0,0 +1,22 @@
require 'test_helper'
class DomainTest < ApplicationIntegrationTest
def setup
@domain = domains(:shop)
@admin = users(:admin)
sign_in @admin
end
def test_inform_registrar_about_status_changes
patch admin_domain_path(domains(:shop)), params: { domain: { statuses: [DomainStatus::PENDING_UPDATE,] } }
# Status OK is removed because, if:
# (statuses.length > 1) || !valid?
# then status OK is removed by manage_automatic_statuses method in domain.rb
assert_equal "Set on #{domains(:shop).name}: #{DomainStatus::PENDING_UPDATE}. Removed from #{domains(:shop).name}: #{DomainStatus::OK}", domains(:shop).registrar.notifications.last.text
patch admin_domain_path(domains(:shop)), params: { domain: { statuses: [DomainStatus::PENDING_DELETE_CONFIRMATION] } }
assert_equal "Set on #{domains(:shop).name}: #{DomainStatus::PENDING_DELETE_CONFIRMATION}. Removed from #{domains(:shop).name}: #{DomainStatus::PENDING_UPDATE}", domains(:shop).registrar.notifications.last.text
end
end