From 80b5e28ac75f0c67b9daa686ad7dfabc795fb5cb Mon Sep 17 00:00:00 2001 From: artemlutsenko Date: Tue, 30 Apr 2024 14:03:04 +0300 Subject: [PATCH] add new network.name sorting to destination resource --- .../admin/routing/destination_resource.rb | 5 +- .../routing/destinations_controller_spec.rb | 63 +++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/app/resources/api/rest/admin/routing/destination_resource.rb b/app/resources/api/rest/admin/routing/destination_resource.rb index 25075084b..9306affe6 100644 --- a/app/resources/api/rest/admin/routing/destination_resource.rb +++ b/app/resources/api/rest/admin/routing/destination_resource.rb @@ -85,7 +85,7 @@ def self.creatable_fields(context) end def self.sortable_fields(_context = nil) - super + [:'country.name'] + super + %i[country.name network.name] end def self.resource_for(type) @@ -105,6 +105,9 @@ def self.sort_records(records, order_options, context = {}) when 'country.name' local_records = records.left_joins(:country).order("countries.name #{direction}") order_options.delete('country.name') + when 'network.name' + local_records = records.left_joins(:network).order("networks.name #{direction}") + order_options.delete('network.name') else local_records = apply_sort(local_records, order_options, context) end diff --git a/spec/controllers/api/rest/admin/routing/destinations_controller_spec.rb b/spec/controllers/api/rest/admin/routing/destinations_controller_spec.rb index 0e063ef51..b8d3d5168 100755 --- a/spec/controllers/api/rest/admin/routing/destinations_controller_spec.rb +++ b/spec/controllers/api/rest/admin/routing/destinations_controller_spec.rb @@ -20,14 +20,14 @@ end describe 'sort' do - context 'by country & prefix' do + context 'by country, network & prefix' do let(:destinations) { nil } let!(:net_type) { FactoryBot.create(:network_type) } - let!(:network) { FactoryBot.create(:network) } let!(:first_destination_afghanistan) do afghanistan = System::Country.find_by!(name: 'Afghanistan') - network_prefix = FactoryBot.create(:network_prefix, country: afghanistan) + network = FactoryBot.create(:network, name: 'AfghanistanNet', network_type: net_type) + network_prefix = FactoryBot.create(:network_prefix, country: afghanistan, network:) record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '111') record.update!(network_prefix_id: network_prefix.id) record @@ -35,7 +35,8 @@ let!(:second_destination_ukraine) do ukraine = System::Country.find_by!(name: 'Ukraine') - network_prefix = FactoryBot.create(:network_prefix, country: ukraine) + network = FactoryBot.create(:network, name: 'UkraineNetABC', network_type: net_type) + network_prefix = FactoryBot.create(:network_prefix, country: ukraine, network:) record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '999') record.update!(network_prefix_id: network_prefix.id) record @@ -96,6 +97,60 @@ end end end + + context 'by country, network and prefix' do + let!(:network_ua_cba) { FactoryBot.create(:network, name: 'UkraineNetCBA', network_type: net_type) } + + let!(:third_destination_ukraine) do + ukraine = System::Country.find_by!(name: 'Ukraine') + network_prefix = FactoryBot.create(:network_prefix, country: ukraine, network: network_ua_cba) + record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '777') + record.update!(network_prefix_id: network_prefix.id) + record + end + + let!(:fourth_destination_ukraine) do + ukraine = System::Country.find_by!(name: 'Ukraine') + network_prefix = FactoryBot.create(:network_prefix, country: ukraine, network: network_ua_cba) + record = FactoryBot.create(:destination, rate_group: rate_group, prefix: '444') + record.update!(network_prefix_id: network_prefix.id) + record + end + + context 'when sort by country name and then by prefix in ASC order' do + let(:index_params) { { sort: 'country.name,network.name,prefix' } } + + it 'returns ordered records' do + subject + + expect(response_body[:errors]).to be_nil + expect(response_body[:data].pluck(:id)).to eq([ + first_destination_afghanistan.id.to_s, + second_destination_ukraine.id.to_s, + fourth_destination_ukraine.id.to_s, + third_destination_ukraine.id.to_s + ]) + expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq(%w[111 999 444 777]) + end + end + + context 'when sort by country name and then by prefix in DESC order' do + let(:index_params) { { sort: 'country.name,network.name,-prefix' } } + + it 'returns ordered records' do + subject + + expect(response_body[:errors]).to be_nil + expect(response_body[:data].pluck(:id)).to eq([ + first_destination_afghanistan.id.to_s, + second_destination_ukraine.id.to_s, + third_destination_ukraine.id.to_s, + fourth_destination_ukraine.id.to_s + ]) + expect(response_body[:data].pluck(:attributes).pluck(:prefix)).to eq(%w[111 999 777 444]) + end + end + end end end end