Skip to content
Draft
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
1 change: 1 addition & 0 deletions app/contracts/queries/events_query_filters_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class EventsQueryFiltersContract < Dry::Validation::Contract
optional(:timestamp_from)
optional(:timestamp_to)
optional(:enriched).value(:bool)
optional(:transaction_id).maybe(:string)
end

rule("timestamp_from_started_at", "timestamp_from") do
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ def index_filters
:external_subscription_id,
:timestamp_from_started_at,
:timestamp_from,
:timestamp_to
:timestamp_to,
:transaction_id
)
end

Expand Down
8 changes: 7 additions & 1 deletion app/queries/events_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class EventsQuery < BaseQuery
:timestamp_from_started_at,
:timestamp_from,
:timestamp_to,
:enriched
:enriched,
:transaction_id
]

def call
Expand All @@ -28,6 +29,7 @@ def call

events = with_code(events) if filters.code
events = with_external_subscription_id(events) if filters.external_subscription_id
events = with_transaction_id(events) if filters.transaction_id
events = with_timestamp_range(events)

result.event_model = event_model.to_s
Expand Down Expand Up @@ -61,6 +63,10 @@ def with_external_subscription_id(scope)
scope.where(external_subscription_id: filters.external_subscription_id)
end

def with_transaction_id(scope)
scope.where(transaction_id: filters.transaction_id)
end

def with_timestamp_range(scope)
if timestamp_from_started_at? && subscription
scope = scope.where(timestamp: subscription.started_at..)
Expand Down
14 changes: 14 additions & 0 deletions spec/queries/events_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@
end
end

context "with transaction_id filter" do
let(:event2) { create(:event, organization:) }
let(:filters) { {transaction_id: event.transaction_id} }

before { event2 }

it "applies the filter" do
result = events_query.call

expect(result).to be_success
expect(result.events.count).to eq(1)
end
end

context "with timestamp filters" do
let(:filters) {
{
Expand Down
14 changes: 14 additions & 0 deletions spec/requests/api/v1/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@
end
end

context "with transaction_id" do
let(:params) { {transaction_id: event.transaction_id} }

before { create(:event, organization:) }

it "returns events" do
subject

expect(response).to have_http_status(:ok)
expect(json[:events].count).to eq(1)
expect(json[:events].first[:lago_id]).to eq(event.id)
end
end

context "with timestamp" do
let(:params) do
{timestamp_from: 2.days.ago.to_date, timestamp_to: Date.tomorrow.to_date}
Expand Down
Loading