Skip to content

Commit

Permalink
FIX: Event sorting shows oldest events first (#429)
Browse files Browse the repository at this point in the history
* FIX: Event sorting shows oldest events first

---------

Co-authored-by: Jean Perez <[email protected]>
  • Loading branch information
jdmartinez1062 and jmperez127 authored Dec 14, 2023
1 parent d873057 commit ffe812b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
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

0 comments on commit ffe812b

Please sign in to comment.