Skip to content

Commit

Permalink
Merge pull request #1448 from senid231/customer-api-cdrs-default-filt…
Browse files Browse the repository at this point in the history
…er-time-start-gteq

Customer API cdrs add default filter time_start_gteq 24 hours ago
  • Loading branch information
dmitry-sinina authored Mar 23, 2024
2 parents 0ef28da + fe5ea52 commit e2d24e7
Show file tree
Hide file tree
Showing 27 changed files with 161 additions and 6 deletions.
9 changes: 8 additions & 1 deletion app/resources/api/rest/customer/v1/cdr_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.default_sort
has_one :account, class_name: 'Account', relation_name: :customer_acc, foreign_key_on: :related

ransack_filter :uuid, type: :uuid
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 @@ -105,4 +105,11 @@ def self.apply_allowed_accounts(records, options)
scope = scope.where_customer_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
13 changes: 10 additions & 3 deletions app/resources/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,22 @@ def self.type(custom_type)
# @see RansackFilterBuilder::RANSACK_TYPE_SUFIXES_DIC
# @param column [Symbol]
# @param verify [Proc, nil] custom validate/change values (receives [Array<String>] values)
def self.ransack_filter(attr, type:, column: nil, verify: nil, collection: nil)
def self.ransack_filter(attr, type:, column: nil, verify: nil, collection: nil, default: nil)
raise ArgumentError, "type #{type} is not supported" unless RansackFilterBuilder.type_supported?(type)
raise ArgumentError, ":collection option for type #{type} is not supported" if collection && type != :enum

RansackFilterBuilder.suffixes_for_type(type).each do |suf|
suffixes = RansackFilterBuilder.suffixes_for_type(type)
if default
wrong_suffixes = default.keys - suffixes.map(&:to_sym)
raise ArgumentError, "ransack_filter :#{attr} has wrong default suffixes: #{wrong_suffixes.join(', ')}" if wrong_suffixes.any?
end
suffixes.each do |suf|
builder = RansackFilterBuilder.new(attr: attr, operator: suf, column: column, verify: verify, collection: collection)
filter builder.filter_name,
verify: ->(values, _ctx) { builder.verify(values) },
apply: ->(records, values, _opts) { builder.apply(records, values) }
apply: ->(records, values, _opts) { builder.apply(records, values) },
# see JsonapiRequestParserPatch#set_default_filters
default: default&.fetch(suf.to_sym, nil)
end
end

Expand Down
1 change: 1 addition & 0 deletions config/initializers/jsonapi_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'jsonapi/exceptions/authentication_failed'
require 'jsonapi/operation_dispatcher_patch'
require 'jsonapi/relationship_patch'
require 'jsonapi/request_parser_patch'

JSONAPI.configure do |config|
# can be paged, offset, none (default)
Expand Down
21 changes: 21 additions & 0 deletions lib/jsonapi/request_parser_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module JsonapiRequestParserPatch
def set_default_filters
@resource_klass._allowed_filters.each do |filter, opts|
next if opts[:default].nil? || !@filters[filter].nil?

if opts[:default].is_a?(Proc)
value = opts[:default].call({ params:, context: })
elsif opts[:default].is_a?(Symbol)
value = @resource_klass.public_send(opts[:default], { params:, context: })
else
value = opts[:default]
end

@filters[filter] = value unless value.nil?
end
end
end

JSONAPI::RequestParser.prepend(JsonapiRequestParserPatch)
2 changes: 2 additions & 0 deletions spec/controllers/api/rest/admin/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get :index, params: json_api_request_query
end
before { create_list :account, 2 }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_name do
let(:subject_record) { create(:account) }
Expand All @@ -36,6 +37,7 @@
end
let(:factory) { :account }
let(:trait) { :with_max_balance }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
it_behaves_like :jsonapi_filters_by_number_field, :balance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
end
let(:factory) { :auth_log }
let(:trait) { :with_id }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_datetime_field, :request_time
it_behaves_like :jsonapi_filters_by_boolean_field, :success, type: :boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@
end
let(:factory) { :cdr }
let(:trait) { :with_id_and_uuid }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_datetime_field, :time_start
it_behaves_like :jsonapi_filters_by_number_field, :destination_next_rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :codec_group }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :contact }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :email
it_behaves_like :jsonapi_filters_by_string_field, :notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get :index, params: json_api_request_query
end
before { create_list :contractor, 2, vendor: true }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_name do
let(:subject_record) { create(:contractor, vendor: true) }
Expand All @@ -35,6 +36,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :vendor }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
it_behaves_like :jsonapi_filters_by_boolean_field, :enabled
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/api/rest/admin/dialpeers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get :index, params: json_api_request_query
end
before { create_list :dialpeer, 2 }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_external_id do
let(:subject_record) { create :dialpeer }
Expand Down Expand Up @@ -46,6 +47,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :dialpeer }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_boolean_field, :enabled
it_behaves_like :jsonapi_filters_by_number_field, :next_rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :gateway_group }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
end
Expand Down Expand Up @@ -53,6 +54,7 @@
get :index, params: json_api_request_query
end
before { create_list :gateway_group, 2 }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_name do
let(:subject_record) { create :gateway_group }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
end
let(:factory) { :gateway }
let(:trait) { :with_incoming_auth }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :host
it_behaves_like :jsonapi_filters_by_number_field, :port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :payment }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_number_field, :amount
it_behaves_like :jsonapi_filters_by_string_field, :notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :area_prefix }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :prefix
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :area }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
end

let(:factory) { :destination_next_rate }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_number_field, :initial_rate
it_behaves_like :jsonapi_filters_by_number_field, :next_rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
get :index, params: json_api_request_query
end
before { create_list :destination, 2 }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_external_id do
let(:subject_record) { create(:destination) }
Expand Down Expand Up @@ -152,6 +153,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :destination }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_boolean_field, :enabled
it_behaves_like :jsonapi_filters_by_string_field, :prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :dialpeer_next_rate }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_number_field, :next_rate
it_behaves_like :jsonapi_filters_by_number_field, :initial_rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :numberlist_item }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :key
it_behaves_like :jsonapi_filters_by_number_field, :action_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :numberlist }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
it_behaves_like :jsonapi_filters_by_number_field, :default_action_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get :index, params: json_api_request_query
end
before { create_list :rateplan, 2 }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_name do
let(:subject_record) { create :rateplan }
Expand All @@ -35,6 +36,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :rateplan }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get :index, params: json_api_request_query
end
before { create_list(:routing_group, 2) }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_name do
let(:subject_record) { create(:routing_group) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :routing_tag_detection_rule }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :src_prefix
it_behaves_like :jsonapi_filters_by_string_field, :dst_prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get :index, params: json_api_request_query
end
before { create_list :routing_plan, 2 }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filter_by_name do
let(:subject_record) { create :routing_plan }
Expand All @@ -35,6 +36,7 @@
get :index, params: json_api_request_query
end
let(:factory) { :routing_plan }
let(:json_api_request_query) { nil }

it_behaves_like :jsonapi_filters_by_string_field, :name
it_behaves_like :jsonapi_filters_by_number_field, :rate_delta_max
Expand Down
Loading

0 comments on commit e2d24e7

Please sign in to comment.