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

fix(grouped-by): Current usage with in advance aggregation #1652

Merged
merged 2 commits into from
Feb 1, 2024
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
4 changes: 2 additions & 2 deletions app/services/billable_metrics/aggregations/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ def support_grouped_aggregation?
false
end

def empty_results(options: {})
def empty_results
empty_result = BaseService::Result.new
empty_result.grouped_by = grouped_by.index_with { nil }
empty_result.aggregation = 0
empty_result.count = 0
empty_result.current_usage_units = 0 if options[:is_pay_in_advance]
empty_result.current_usage_units = 0

result.aggregations = [empty_result]
result
Expand Down
2 changes: 1 addition & 1 deletion app/services/billable_metrics/aggregations/sum_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def compute_aggregation(options: {})
# with the grouped_by_values filter
def compute_grouped_by_aggregation(options: {})
aggregations = event_store.grouped_sum
return empty_results(options:) if aggregations.blank?
return empty_results if aggregations.blank?

counts = event_store.grouped_count

Expand Down
7 changes: 6 additions & 1 deletion app/services/charges/validators/standard_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Validators
class StandardService < Charges::Validators::BaseService
def valid?
validate_amount
validate_grouped_by

super
end
Expand All @@ -15,6 +16,10 @@ def amount
properties['amount']
end

def grouped_by
properties['grouped_by']
end

def validate_amount
return if ::Validators::DecimalAmountService.new(amount).valid_amount?

Expand All @@ -25,7 +30,7 @@ def validate_grouped_by
return if grouped_by.blank?
return if grouped_by.is_a?(Array) && grouped_by.all? { |f| f.is_a?(String) }

add_error(field: :grouped_by, error_code: 'invalid_grouped_by')
add_error(field: :grouped_by, error_code: 'invalid_type')
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/services/charges/validators/standard_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,38 @@

it { expect(standard_service).to be_valid }
end

describe 'grouped_by' do
let(:properties) { { 'amount' => '12', 'grouped_by' => grouped_by } }
let(:grouped_by) { [] }

it { expect(standard_service).to be_valid }

context 'when attribute is not an array' do
let(:grouped_by) { 'group' }

it 'is invalid' do
aggregate_failures do
expect(standard_service).not_to be_valid
expect(standard_service.result.error).to be_a(BaseService::ValidationFailure)
expect(standard_service.result.error.messages.keys).to include(:grouped_by)
expect(standard_service.result.error.messages[:grouped_by]).to include('invalid_type')
end
end
end

context 'when attribute is not a list of string' do
let(:grouped_by) { [12, 45] }

it 'is invalid' do
aggregate_failures do
expect(standard_service).not_to be_valid
expect(standard_service.result.error).to be_a(BaseService::ValidationFailure)
expect(standard_service.result.error.messages.keys).to include(:grouped_by)
expect(standard_service.result.error.messages[:grouped_by]).to include('invalid_type')
end
end
end
end
end
end
Loading