Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomerAPI: configurable CDR fields #1702

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion app/forms/customer_api/cdr_export_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,30 @@ def self.policy_class
time_end
success
duration
src_name_in
src_prefix_in
dst_prefix_in
from_domain
to_domain
ruri_domain
lega_disconnect_code
lega_disconnect_reason
auth_orig_ip
auth_orig_port
src_prefix_routing
dst_prefix_routing
destination_prefix
destination_initial_interval
destination_next_interval
destination_initial_rate
destination_next_rate
destination_fee
customer_price
customer_duration
orig_call_id
local_tag
lega_user_agent
diversion_in
].freeze

model_class 'CdrExport'
Expand All @@ -50,7 +70,8 @@ def self.policy_class
private

def apply_fields
model.fields = FIELDS
hidden_fields = YetiConfig.customer_api_outgoing_cdr_hide_fields || []
model.fields = FIELDS - hidden_fields
end

def validate_account
Expand Down
2 changes: 1 addition & 1 deletion app/resources/api/rest/customer/v1/cdr_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def rec

def fetchable_fields
fields = super
hidden_fields = YetiConfig.customer_api_cdr_hide_fields || []
hidden_fields = YetiConfig.customer_api_outgoing_cdr_hide_fields || []
fields - hidden_fields.map(&:to_sym)
end

Expand Down
15 changes: 14 additions & 1 deletion app/resources/api/rest/customer/v1/incoming_cdr_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def self.default_sort
has_one :account, class_name: 'Account', relation_name: :vendor_acc, foreign_key_on: :related

ransack_filter :id, type: :number
ransack_filter :time_start, type: :datetime
ransack_filter :time_start, type: :datetime, default: { gteq: :apply_default_filter_time_start_gteq }
ransack_filter :time_connect, type: :datetime
ransack_filter :time_end, type: :datetime
ransack_filter :duration, type: :number
Expand Down Expand Up @@ -83,10 +83,23 @@ def rec
_model.has_recording?
end

def fetchable_fields
fields = super
hidden_fields = YetiConfig.customer_api_incoming_cdr_hide_fields || []
fields - hidden_fields.map(&:to_sym)
end

def self.apply_allowed_accounts(records, options)
context = options[:context]
scope = records.where_vendor(context[:customer_id])
scope = scope.where_vendor_account(context[:allowed_account_ids]) if context[:allowed_account_ids].present?
scope
end

def self.apply_default_filter_time_start_gteq(options)
return nil if options.dig(:params, :filter, :time_start_gt).present?
return nil if options.dig(:params, :filter, :time_start_eq).present?

24.hours.ago.strftime('%F %T')
end
end
3 changes: 2 additions & 1 deletion config/initializers/_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def self.setting_files(config_root, _env)
optional(:url_return).maybe(:string)
end

optional(:customer_api_cdr_hide_fields).array(:string)
optional(:customer_api_outgoing_cdr_hide_fields).array(:string)
optional(:customer_api_incoming_cdr_hide_fields).array(:string)

optional(:invoice).schema do
optional(:auto_approve).value(:bool)
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/rest/customer/v1/cdrs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@
)
end

context 'when customer_api_cdr_hide_fields configured' do
context 'when customer_api_outgoing_cdr_hide_fields configured' do
before do
allow(YetiConfig).to receive(:customer_api_cdr_hide_fields).and_return(
allow(YetiConfig).to receive(:customer_api_outgoing_cdr_hide_fields).and_return(
%w[auth_orig_ip lega_user_agent]
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@
let(:pk) { :id }

it_behaves_like :jsonapi_filters_by_number_field, :id
it_behaves_like :jsonapi_filters_by_datetime_field, :time_start
it_behaves_like :jsonapi_filters_by_datetime_field, :time_start do
# overrides default filter to avoid conflicts with tests
let(:json_api_request_query) do
{ filter: { time_start_gteq: 50.days.ago.strftime('%F %T') } }
end
end
it_behaves_like :jsonapi_filters_by_datetime_field, :time_connect
it_behaves_like :jsonapi_filters_by_datetime_field, :time_end
it_behaves_like :jsonapi_filters_by_number_field, :duration
Expand Down Expand Up @@ -251,6 +256,22 @@
)
end

context 'when customer_api_incoming_cdr_hide_fields configured' do
before do
allow(YetiConfig).to receive(:customer_api_incoming_cdr_hide_fields).and_return(
%w[diversion_out legb_user_agent]
)
end

it 'returns record with expected attributes' do
subject
attribute_keys = response_json[:data][:attributes].keys
expect(attribute_keys).to include(:'src-name-out')
expect(attribute_keys).not_to include(:'diversion-out')
expect(attribute_keys).not_to include(:'legb-user-agent')
end
end

context 'with include account' do
let(:json_api_request_query) { { include: 'account' } }

Expand Down
Loading