Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Event sorting shows oldest events first #429

Merged
merged 7 commits into from
Dec 14, 2023
Merged
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
11 changes: 8 additions & 3 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,17 @@ module ::DiscoursePostEvent
category = Category.find_by(id: topic_query.options[:category_id])
if category && category.custom_fields &&
category.custom_fields["sort_topics_by_event_start_date"]
reorder_sql = <<~SQL
CASE WHEN COALESCE(custom_fields.value::timestamptz, topics.bumped_at) > NOW() THEN 0 ELSE 1 END,
CASE WHEN COALESCE(custom_fields.value::timestamptz, topics.bumped_at) > NOW() THEN COALESCE(custom_fields.value::timestamptz, topics.bumped_at) ELSE NULL END,
CASE WHEN COALESCE(custom_fields.value::timestamptz, topics.bumped_at) < NOW() THEN COALESCE(custom_fields.value::timestamptz, topics.bumped_at) ELSE NULL END DESC
SQL
results =
results.joins(
"LEFT JOIN topic_custom_fields AS custom_fields on custom_fields.topic_id = topics.id
AND custom_fields.name = '#{DiscoursePostEvent::TOPIC_POST_EVENT_STARTS_AT}'
",
).reorder("topics.pinned_at ASC, custom_fields.value ASC")
AND custom_fields.name = '#{DiscoursePostEvent::TOPIC_POST_EVENT_STARTS_AT}'
",
).reorder(reorder_sql)
end
end
results
Expand Down
54 changes: 54 additions & 0 deletions spec/models/topic_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true
require "rails_helper"

describe TopicQuery do
describe "sorts events" do
fab!(:user) { Fabricate(:user, admin: true) }
fab!(:notified_user) { Fabricate(:user) }
fab!(:topic_1) { Fabricate(:topic, user: user) }
fab!(:topic_2) { Fabricate(:topic, user: user) }
fab!(:topic_3) { Fabricate(:topic, user: user) }
fab!(:topic_4) { Fabricate(:topic, user: user) }
fab!(:post_1) { Fabricate(:post, topic: topic_1) }
fab!(:post_2) { Fabricate(:post, topic: topic_2) }
fab!(:post_3) { Fabricate(:post, topic: topic_3) }
fab!(:post_4) { Fabricate(:post, topic: topic_4) }

fab!(:future_event_1) do
DiscoursePostEvent::Event.create!(
id: post_1.id,
original_starts_at: Time.now + 5.hours,
original_ends_at: Time.now + 7.hours,
)
end
fab!(:future_event_2) do
DiscoursePostEvent::Event.create!(
id: post_2.id,
original_starts_at: Time.now + 1.hours,
original_ends_at: Time.now + 2.hours,
)
end
fab!(:past_event_1) do
DiscoursePostEvent::Event.create!(
id: post_3.id,
original_starts_at: Time.now - 10.hours,
original_ends_at: Time.now - 8.hours,
)
end
fab!(:past_event_2) do
DiscoursePostEvent::Event.create!(
id: post_4.id,
original_starts_at: Time.now - 7.hours,
original_ends_at: Time.now - 5.hours,
)
end

it "upcoming events first, sorted by ascending order. expired events last, sorted by descending order" do
ordered_topics =
TopicQuery.new(nil, order_by_event_date: [topic_1, topic_2, topic_3, topic_4]).options[
:order_by_event_date
]
expect(ordered_topics).to eq([topic_1, topic_2, topic_3, topic_4])
end
end
end