Skip to content

Commit

Permalink
use instead
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Sep 30, 2024
1 parent 3da5e03 commit 61692b9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/services/charges/pay_in_advance_aggregation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def aggregation_filters
filters = {event:}

properties = charge_filter&.properties || charge.properties
if charge.standard? && properties['grouped_by'].present?
if charge.supports_grouped_by? && properties['grouped_by'].present?
filters[:grouped_by_values] = properties['grouped_by'].index_with do |grouped_by|
event.properties[grouped_by]
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/fees/charge_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def aggregation_filters(charge_filter: nil)
filters = {}

properties = charge_filter&.properties || charge.properties
filters[:grouped_by] = properties['grouped_by'] if charge.standard? && properties['grouped_by'].present?
filters[:grouped_by] = properties['grouped_by'] if charge.supports_grouped_by? && properties['grouped_by'].present?

if charge_filter.present?
result = ChargeFilters::MatchingAndIgnoredService.call(charge:, filter: charge_filter)
Expand Down
61 changes: 60 additions & 1 deletion spec/scenarios/charge_models/dynamic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
create(
:dynamic_charge,
plan:,
billable_metric:
billable_metric:,
properties: charge_properties
)
end

let(:charge_properties) { {} }

before do
organization
customer
Expand Down Expand Up @@ -65,4 +68,60 @@
expect(json[:customer_usage][:charges_usage][0][:units]).to eq('20.0')
end
end

context 'with grouping' do
let(:charge_properties) { {grouped_by: [:group_key]} }

it 'returns the expected customer usage' do
travel_to(DateTime.new(2023, 3, 5)) do
create_subscription(
{
external_customer_id: customer.external_id,
external_id: customer.external_id,
plan_code: plan.code
}
)
end

travel_to(DateTime.new(2023, 3, 6)) do
create_event(
{
code: billable_metric.code,
transaction_id: SecureRandom.uuid,
external_subscription_id: customer.external_id,
precise_total_amount_cents: '10.1',
properties: {item_id: '10', group_key: 'value 1'}
}
)

fetch_current_usage(customer:)

expect(json[:customer_usage][:total_amount_cents]).to eq(10)
expect(json[:customer_usage][:charges_usage][0][:units]).to eq('10.0')
expect(json[:customer_usage][:charges_usage][0][:grouped_usage][0][:grouped_by]).to eq({group_key: "value 1"})

create_event(
{
code: billable_metric.code,
transaction_id: SecureRandom.uuid,
external_subscription_id: customer.external_id,
precise_total_amount_cents: '901.9',
properties: {item_id: '10', group_key: 'value 2'}
}
)

fetch_current_usage(customer:)
expect(json[:customer_usage][:total_amount_cents]).to eq(912)
expect(json[:customer_usage][:charges_usage][0][:units]).to eq('20.0')
expect(json[:customer_usage][:charges_usage][0][:grouped_usage].size).to eq(2)

expect(json[:customer_usage][:charges_usage][0][:grouped_usage]).to match_array(
[
{amount_cents: 902, events_count: 1, units: "10.0", grouped_by: {group_key: "value 2"}, filters: [], groups: []},
{amount_cents: 10, events_count: 1, units: "10.0", grouped_by: {group_key: "value 1"}, filters: [], groups: []}
]
)
end
end
end
end

0 comments on commit 61692b9

Please sign in to comment.