Skip to content

Commit

Permalink
feat(DynamicPricing): Accept precise_total_amount_cents when creating…
Browse files Browse the repository at this point in the history
… an event (#2610)

## Context

AI, CPaaS and Fintech customers can apply a price to a unit that
fluctuates over time. Currently Lago does not support this usecase.

## Description

This PR updates `POST /api/v1/events` and `POST /api/v1/batch_events` to
accept the new `precise_total_amount_cents` field on event model.
  • Loading branch information
vincent-pochet authored Sep 23, 2024
1 parent 6f6b7ff commit eab2aab
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def create_params
:code,
:timestamp,
:external_subscription_id,
:precise_total_amount_cents,
properties: {}
)
end
Expand All @@ -102,6 +103,7 @@ def batch_params
:code,
:timestamp,
:external_subscription_id,
:precise_total_amount_cents,
properties: {} # rubocop:disable Style/HashAsLastArrayItem
]
).to_h.deep_symbolize_keys
Expand Down
1 change: 1 addition & 0 deletions app/serializers/v1/event_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def serialize
lago_customer_id: model.customer_id,
code: model.code,
timestamp: model.timestamp.iso8601(3),
precise_total_amount_cents: model.precise_total_amount_cents&.to_s,
properties: model.properties,
lago_subscription_id: model.subscription_id,
external_subscription_id: model.external_subscription_id,
Expand Down
4 changes: 3 additions & 1 deletion app/services/events/create_batch_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def validate_events
event.properties = event_params[:properties] || {}
event.metadata = metadata || {}
event.timestamp = Time.zone.at(event_params[:timestamp] ? event_params[:timestamp].to_f : timestamp)
event.precise_total_amount_cents = event_params[:precise_total_amount_cents]

result.events.push(event)
result.errors = result.errors.merge({index => event.errors.messages}) unless event.valid?
Expand Down Expand Up @@ -80,7 +81,8 @@ def produce_kafka_event(event)
timestamp: event.timestamp.to_f,
code: event.code,
properties: event.properties,
ingested_at: Time.zone.now.iso8601[...-1]
ingested_at: Time.zone.now.iso8601[...-1],
precise_total_amount_cents: event.precise_total_amount_cents
}.to_json
)
end
Expand Down
2 changes: 2 additions & 0 deletions app/services/events/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def call
event.properties = params[:properties] || {}
event.metadata = metadata || {}
event.timestamp = Time.zone.at(params[:timestamp] ? params[:timestamp].to_f : timestamp)
event.precise_total_amount_cents = params[:precise_total_amount_cents]

event.save! unless organization.clickhouse_aggregation?

Expand Down Expand Up @@ -52,6 +53,7 @@ def produce_kafka_event(event)
transaction_id: event.transaction_id,
timestamp: event.timestamp.iso8601[...-1], # NOTE: Removes trailing 'Z' to allow clickhouse parsing
code: event.code,
precise_total_amount_cents: event.precise_total_amount_cents,
properties: event.properties,
ingested_at: Time.zone.now.iso8601[...-1]
}.to_json
Expand Down
4 changes: 4 additions & 0 deletions spec/requests/api/v1/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
transaction_id: SecureRandom.uuid,
external_subscription_id: subscription.external_id,
timestamp: Time.current.to_i,
precise_total_amount_cents: '123.45',
properties: {
foo: 'bar'
}
Expand All @@ -48,6 +49,7 @@
transaction_id: event.transaction_id,
external_subscription_id: subscription.external_id,
timestamp: Time.current.to_i,
precise_total_amount_cents: '123.45',
properties: {
foo: 'bar'
}
Expand All @@ -72,6 +74,7 @@
transaction_id: SecureRandom.uuid,
external_subscription_id: subscription.external_id,
timestamp: Time.current.to_i,
precise_total_amount_cents: '123.45',
properties: {
foo: 'bar'
}
Expand Down Expand Up @@ -136,6 +139,7 @@
code: metric.code,
external_subscription_id: subscription.external_id,
transaction_id: SecureRandom.uuid,
precise_total_amount_cents: '123.45',
properties: {
foo: 'bar'
}
Expand Down
2 changes: 2 additions & 0 deletions spec/serializers/v1/event_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:event,
customer_id: nil,
subscription_id: nil,
precise_total_amount_cents: '123.6',
properties: {
item_value: '12'
}
Expand All @@ -26,6 +27,7 @@
'lago_customer_id' => event.customer_id,
'code' => event.code,
'timestamp' => event.timestamp.iso8601(3),
'precise_total_amount_cents' => '123.6',
'properties' => event.properties,
'lago_subscription_id' => event.subscription_id,
'external_subscription_id' => event.external_subscription_id,
Expand Down
4 changes: 4 additions & 0 deletions spec/services/events/create_batch_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
let(:code) { 'sum_agg' }
let(:metadata) { {} }
let(:creation_timestamp) { Time.current.to_f }
let(:precise_total_amount_cents) { '123.34' }

let(:events_params) do
events = []
Expand All @@ -26,6 +27,7 @@
external_subscription_id: SecureRandom.uuid,
code:,
transaction_id: SecureRandom.uuid,
precise_total_amount_cents:,
properties: {foo: 'bar'},
timestamp:
}
Expand Down Expand Up @@ -178,6 +180,7 @@
external_subscription_id: SecureRandom.uuid,
code:,
transaction_id: SecureRandom.uuid,
precise_total_amount_cents:,
properties: {foo: 'bar'},
timestamp:
}
Expand All @@ -204,6 +207,7 @@
external_subscription_id: SecureRandom.uuid,
code:,
transaction_id: SecureRandom.uuid,
precise_total_amount_cents:,
properties: {foo: 'bar'},
timestamp:
}
Expand Down
27 changes: 26 additions & 1 deletion spec/services/events/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
let(:external_subscription_id) { SecureRandom.uuid }
let(:timestamp) { Time.current.to_f }
let(:transaction_id) { SecureRandom.uuid }
let(:precise_total_amount_cents) { nil }

let(:creation_timestamp) { Time.current.to_f }

Expand All @@ -26,6 +27,7 @@
external_subscription_id:,
code:,
transaction_id:,
precise_total_amount_cents:,
properties: {foo: 'bar'},
timestamp:
}
Expand All @@ -46,7 +48,8 @@
transaction_id:,
code:,
timestamp: Time.zone.at(timestamp),
properties: {'foo' => 'bar'}
properties: {'foo' => 'bar'},
precise_total_amount_cents: nil
)
end
end
Expand Down Expand Up @@ -131,5 +134,27 @@
expect(karafka_producer).to have_received(:produce_async)
end
end

context 'with a precise_total_amount_cents' do
let(:precise_total_amount_cents) { "123.45" }

it 'creates an event with the precise_total_amount_cents' do
result = create_service.call

expect(result).to be_success
expect(result.event.precise_total_amount_cents).to eq(123.45)
end

context 'when precise_total_amount_cents is not a valid decimal value' do
let(:precise_total_amount_cents) { "asdfa" }

it 'creates an event' do
result = create_service.call

expect(result).to be_success
expect(result.event.precise_total_amount_cents).to eq(0)
end
end
end
end
end

0 comments on commit eab2aab

Please sign in to comment.