diff --git a/spec/scenarios/billable_metrics/custom_aggregation_spec.rb b/spec/scenarios/billable_metrics/custom_aggregation_spec.rb index 3a4e05213cd..9d6ee489aca 100644 --- a/spec/scenarios/billable_metrics/custom_aggregation_spec.rb +++ b/spec/scenarios/billable_metrics/custom_aggregation_spec.rb @@ -19,30 +19,34 @@ def aggregate(event, previous_state, aggregation_properties) ranges = aggregation_properties['ranges'] result_amount = ranges.reduce(0) do |amount, range| + to = range['to'] + to = BigDecimal(to.to_s) if to + # Range was already reached - next amount if range['to'] && previous_units > range['to'] + next amount if to && previous_units > to + from = BigDecimal(range['from'].to_s) certif_amount = BigDecimal(range[certif] ? range[certif].to_s : '0') - if !range['to'] || total_units <= range['to'] + if !to || total_units <= to # Last matching range is reached - units_to_use = if previous_units >= range['from'] + units_to_use = if previous_units >= from # All new units are in the current range event_units else # Takes only the new units in the current range - total_units - range['from'] + 1 + total_units - from + 1 end break amount += certif_amount * units_to_use else # Range is not the last one - units_to_use = if previous_units >= range['from'] + units_to_use = if previous_units >= from # All remaining units in the range - range['to'] - previous_units + to - previous_units else # All units in the range - range['to'] - range['from'] + 1 + to - from + 1 end amount += certif_amount * units_to_use @@ -594,7 +598,7 @@ def aggregate(event, previous_state, aggregation_properties) amount_property = aggregation_properties['amount'] rate_property = aggregation_properties['rate'] min_amount_property = aggregation_properties['min_amount'] - + fx_rate = BigDecimal(aggregation_properties['fx_rate'] ? aggregation_properties['fx_rate'].to_s : '1') event_value = BigDecimal(event.properties['value'].to_s) total_units = previous_units + 1 @@ -602,15 +606,12 @@ def aggregate(event, previous_state, aggregation_properties) if ranges_property != nil # The aggregation uses a range logic - range = ranges_property.find { |r| r['from'] <= total_units && (r['to'].nil? || total_units < r['to']) } - - amount = range['amount'] - rate = range['rate'] + range = ranges_property.find { |r| BigDecimal(r['from'].to_s) <= total_units && (r['to'].nil? || total_units <= BigDecimal(r['to'].to_s)) } - if amount != nil - result_amount += BigDecimal(amount.to_s) + if range['amount'] != nil + result_amount += BigDecimal(range['amount'].to_s) else - result_amount += event_value * BigDecimal(rate.to_s) / 100 + result_amount += (event_value * BigDecimal(range['rate'].to_s) / 100) * fx_rate end elsif amount_property != nil # The aggregation uses an amount logic @@ -691,9 +692,10 @@ def aggregate(event, previous_state, aggregation_properties) {from: 10_001, to: 15_000, rate: '0.3'}, {from: 15_001, to: 22_000, rate: '0.25'}, {from: 22_001, to: nil, rate: '0.2'}, - ] + ], + fx_rate: 0.88 } - }, + } ) end @@ -1086,7 +1088,7 @@ def aggregate(event, previous_state, aggregation_properties) expect(CachedAggregation.where(organization_id: organization.id).count).to eq(8) fee = subscription.fees.where(charge:).order(created_at: :desc).first - expect(fee.amount_cents).to eq(40_000) + expect(fee.amount_cents).to eq(35_200) expect(fee.events_count).to eq(1) expect(fee.units).to eq(1) end @@ -1125,7 +1127,7 @@ def aggregate(event, previous_state, aggregation_properties) expect(CachedAggregation.where(organization_id: organization.id).count).to eq(10) fee = subscription.fees.where(charge:).order(created_at: :desc).first - expect(fee.amount_cents).to eq(300) + expect(fee.amount_cents).to eq(264) expect(fee.events_count).to eq(1) expect(fee.units).to eq(1) end @@ -1134,13 +1136,13 @@ def aggregate(event, previous_state, aggregation_properties) travel_to(DateTime.new(2024, 2, 6, 10)) do fetch_current_usage(customer:) - expect(json[:customer_usage][:total_amount_cents]).to eq(65_620) # TODO + expect(json[:customer_usage][:total_amount_cents]).to eq(60_772) expect(json[:customer_usage][:charges_usage].count).to eq(1) charge_usage = json[:customer_usage][:charges_usage].first expect(charge_usage[:units]).to eq('8.0') expect(charge_usage[:events_count]).to eq(8) - expect(charge_usage[:amount_cents]).to eq(65_620) # TODO + expect(charge_usage[:amount_cents]).to eq(60_772) expect(charge_usage[:filters].count).to eq(9) gbp_domestic_fps_charge = charge_usage[:filters].find do |f| @@ -1190,7 +1192,7 @@ def aggregate(event, previous_state, aggregation_properties) end expect(chf_charge[:events_count]).to eq(2) expect(chf_charge[:units]).to eq('2.0') - expect(chf_charge[:amount_cents]).to eq(40_400) + expect(chf_charge[:amount_cents]).to eq(35_552) end end end