Skip to content

Commit

Permalink
feat(dynamic-pricing): Accept precise_total_amount_cents when creatin…
Browse files Browse the repository at this point in the history
…g an event
  • Loading branch information
vincent-pochet committed Sep 20, 2024
1 parent fd78551 commit 2a2ee33
Show file tree
Hide file tree
Showing 6 changed files with 41 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
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
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 2a2ee33

Please sign in to comment.