Skip to content

Commit 3061fbd

Browse files
fix(events): Cache counter for GET /events
1 parent 5b3b562 commit 3061fbd

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

app/controllers/api/v1/events_controller.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ def index
8383
result.events,
8484
::V1::EventSerializer,
8585
collection_name: "events",
86-
meta: pagination_metadata(result.events)
86+
meta: pagination_metadata(
87+
result.events,
88+
key: "events",
89+
organization_id: current_organization.id,
90+
params: index_filters.merge(page: params[:page], per_page: params[:per_page])
91+
)
8792
)
8893
)
8994
else
@@ -109,7 +114,12 @@ def index_enriched
109114
result.events,
110115
::V1::EventEnrichedSerializer,
111116
collection_name: "events",
112-
meta: pagination_metadata(result.events)
117+
meta: pagination_metadata(
118+
result.events,
119+
key: "events_enriched",
120+
organization_id: current_organization.id,
121+
params: index_filters.merge(page: params[:page], per_page: params[:per_page])
122+
)
113123
)
114124
)
115125
else

app/models/metadata/item_metadata.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def value_correctness
4545
# Table name: item_metadata
4646
# Database name: primary
4747
#
48-
# id :uuid not null, primary key
49-
# owner_type(Polymorphic owner type) :string not null
50-
# value(item_metadata key-value pairs) :jsonb not null
51-
# created_at :datetime not null
52-
# updated_at :datetime not null
53-
# organization_id(Reference to the organization) :uuid not null
54-
# owner_id(Polymorphic owner id) :uuid not null
48+
# id :uuid not null, primary key
49+
# owner_type :string not null
50+
# value :jsonb not null
51+
# created_at :datetime not null
52+
# updated_at :datetime not null
53+
# organization_id :uuid not null
54+
# owner_id :uuid not null
5555
#
5656
# Indexes
5757
#

spec/requests/api/v1/events_controller_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,25 @@
275275
end
276276
end
277277

278+
context "with unknown params", cache: :memory do
279+
let(:params) { {page: 1, per_page: 1} }
280+
281+
before { create(:event, organization:) }
282+
283+
it "ignores unknown params for caching" do
284+
# First request populates the cache
285+
get_with_token(organization, "/api/v1/events", page: 1, per_page: 1)
286+
expect(json[:meta][:total_count]).to eq(2)
287+
288+
# Add a third event
289+
create(:event, organization:)
290+
291+
# Request with unknown param should return cached count (2), not fresh count (3)
292+
get_with_token(organization, "/api/v1/events", page: 1, per_page: 1, unknown_param: "value")
293+
expect(json[:meta][:total_count]).to eq(2)
294+
end
295+
end
296+
278297
context "with code" do
279298
let(:params) { {code: event.code} }
280299

0 commit comments

Comments
 (0)