Skip to content

Commit

Permalink
Filter events by datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
pezholio committed Dec 13, 2024
1 parent b0d56d6 commit 814ede4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
2 changes: 2 additions & 0 deletions app/controllers/v2/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def events
render json: Queries::GetEvents.call(
content_id: path_params[:content_id],
action: query_params[:action],
from: query_params[:from],
to: query_params[:to],
)
end

Expand Down
16 changes: 11 additions & 5 deletions app/queries/get_events.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
module Queries
class GetEvents
def self.call(content_id:, action: nil)
new(content_id, action).call
def self.call(content_id:, action: nil, from: nil, to: nil)
new(content_id, action, from, to).call
end

def call
Event.where(**query)
result = Event.where(**query)
result = result.where("created_at >= ?", from) if from.present?
result = result.where("created_at <= ?", to) if to.present?

result
end

private

attr_reader :content_id, :action
attr_reader :content_id, :action, :from, :to

def query
{ content_id:, action: }.compact_blank!
end

def initialize(content_id, action)
def initialize(content_id, action, from, to)
@content_id = content_id
@action = action
@from = from
@to = to
end
end
end
3 changes: 3 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ Retrieves a list of all events for a given `:content_id`
- `action` *(optional)*
- Specify what action type to filter for
- If omitted, returns all actions
- `from` *(optional)*
- Filter for actions created after that particular datetime
- If omitted, returns actions for all time

## `GET /v2/content/:content_id/host-content`

Expand Down
38 changes: 35 additions & 3 deletions spec/controllers/v2/content_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,9 @@
let(:edition) { create(:live_edition) }
let(:document) { edition.document }

let!(:put_content_events) { create_list(:event, 3, content_id: document.content_id, action: "PutContent") }
let!(:publish_events) { create_list(:event, 3, content_id: document.content_id, action: "Publish") }
let!(:other_events) { create_list(:event, 3) }
let!(:put_content_events) { create_list(:event, 3, content_id: document.content_id, action: "PutContent", created_at: Time.zone.now - 1.day) }
let!(:publish_events) { create_list(:event, 3, content_id: document.content_id, action: "Publish", created_at: Time.zone.now - 2.days) }
let!(:other_events) { create_list(:event, 3, created_at: Time.zone.now - 3.days) }

it "returns all events for a content_id" do
get :events, params: { content_id: document.content_id }
Expand All @@ -573,5 +573,37 @@

expect(parsed_response).to eq(publish_events.flatten.map(&:as_json))
end

context "filtering by datetime" do
let(:start_date) { Time.zone.now - 12.hours }
let(:end_date) { Time.zone.now - 2.hours }

let!(:new_put_content_events) { create_list(:event, 3, content_id: document.content_id, action: "PutContent", created_at: start_date + 2.hours) }
let!(:new_publish_events) { create_list(:event, 3, content_id: document.content_id, action: "Publish", created_at: start_date + 2.hours) }

it "returns all events for a content_id and start_date" do
new_events = [new_put_content_events, new_publish_events].flatten

get :events, params: { content_id: document.content_id, from: start_date }

expect(parsed_response).to eq(new_events.map(&:as_json))
end

it "filters by action" do
get :events, params: { content_id: document.content_id, from: start_date, action: "Publish" }
expect(parsed_response).to eq(new_publish_events.map(&:as_json))
end

it "filters by start and end date" do
create_list(:event, 3, content_id: document.content_id, action: "PutContent", created_at: end_date + 1.hour)
create_list(:event, 3, content_id: document.content_id, action: "Publish", created_at: end_date + 2.hours)

get :events, params: { content_id: document.content_id, from: start_date, to: end_date }

expected_events = [new_put_content_events, new_publish_events].flatten

expect(parsed_response).to eq(expected_events.map(&:as_json))
end
end
end
end
34 changes: 34 additions & 0 deletions spec/queries/get_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,38 @@

expect(result).to match_array(host_content_update_events)
end

context "filtering by date" do
let(:start_date) { Time.zone.now }

it "returns no events when events have not been created since the start date" do
result = described_class.call(content_id: document.content_id, from: start_date)
expect(result).to match_array([])
end

it "returns all events created since the start date" do
create_list(:event, 2, content_id: document.content_id, created_at: start_date - 2.hours)
result = described_class.call(content_id: document.content_id, from: start_date - 1.hour)

expect(result).to match_array(all_events)
end

it "returns all events create between the start and end dates" do
create_list(:event, 2, content_id: document.content_id, created_at: start_date - 2.hours)
create_list(:event, 2, content_id: document.content_id, created_at: start_date + 2.hours)

result = described_class.call(content_id: document.content_id, from: start_date - 1.hour, to: start_date + 1.hour)

expect(result).to match_array(all_events)
end

it "filters by action" do
create_list(:event, 2, content_id: document.content_id, action: "PatchLinkSet", created_at: start_date + 1.hour)
publish_actions = create_list(:event, 2, content_id: document.content_id, action: "Publish", created_at: start_date + 1.hour)

result = described_class.call(content_id: document.content_id, from: start_date, action: "Publish")

expect(result).to match_array(publish_actions)
end
end
end

0 comments on commit 814ede4

Please sign in to comment.