FIX: simplified event expired logic

FIX: simplified event expired logic

If an event has no en date or has an end date before Time.now then it is not expired.

diff --git a/app/models/discourse_post_event/event.rb b/app/models/discourse_post_event/event.rb
index b3f62d0..8f2fff7 100644
--- a/app/models/discourse_post_event/event.rb
+++ b/app/models/discourse_post_event/event.rb
@@ -74,23 +74,27 @@ module DiscoursePostEvent
 
     scope :visible, -> { where(deleted_at: nil) }
 
-    scope :expired,     -> { where('COALESCE(ends_at, starts_at) < ?',  Time.now) }
-    scope :not_expired, -> { where('COALESCE(ends_at, starts_at) >= ?', Time.now) }
+    scope :expired,     -> { where('ends_at IS NOT NULL AND ends_at < ?',  Time.now) }
+    scope :not_expired, -> { where('ends_at IS NULL OR ends_at > ?', Time.now) }
 
     def is_expired?
-      self.ends_at.present? ? Time.now > self.ends_at : Time.now > self.starts_at
+      !!(self.ends_at && Time.now > self.ends_at)
     end
 
     validates :starts_at, presence: true
 
     def on_going_event_invitees
-      starts_at = self.starts_at
-      ends_at = (self.ends_at || starts_at + 1.hour) + SiteSetting.discourse_post_event_edit_notifications_time_extension.minutes
-
-      if !(starts_at..ends_at).cover?(Time.now)
+      if !self.ends_at && self.starts_at < Time.now
         return []
       end
 
+      if self.ends_at
+        extended_ends_at = self.ends_at + SiteSetting.discourse_post_event_edit_notifications_time_extension.minutes
+        if !(self.starts_at..extended_ends_at).cover?(Time.now)
+          return []
+        end
+      end
+
       invitees.where(status: DiscoursePostEvent::Invitee.statuses[:going])
     end
 
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 5dc2bf0..cedc7a5 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -275,7 +275,8 @@ en:
         invite_user_notification: "%{username} has invited you to %{description}"
         invite_user_auto_notification: "%{username} has automatically set your attendance and invited you to %{description}"
         before_event_reminder: "An event is about to start %{description}"
-        after_event_reminder: "An event has started %{description}"
+        after_event_reminder: "An event has ended %{description}"
+        ongoing_event_reminder: "An event is ongoing  %{description}"
       preview:
         more_than_one_event: You can’t have more than one event.
       edit_reason: Event updated
diff --git a/jobs/regular/discourse_post_event/send_reminder.rb b/jobs/regular/discourse_post_event/send_reminder.rb
index 892f204..2edacfc 100644
--- a/jobs/regular/discourse_post_event/send_reminder.rb
+++ b/jobs/regular/discourse_post_event/send_reminder.rb
@@ -32,6 +32,14 @@ module Jobs
         invitees = invitees.where.not(user_id: already_notified_users.pluck(:user_id))
       end
 
+      event_ended = event.ends_at && Time.now > event.ends_at
+      prefix = 'before'
+      if event_ended
+        prefix = 'after'
+      elsif event_started && !event_ended
+        prefix = 'ongoing'
+      end
+
       invitees.find_each do |invitee|
         invitee.user.notifications.create!(
           notification_type: Notification.types[:custom],
@@ -40,7 +48,7 @@ module Jobs
           data: {
             topic_title: event.post.topic.title,
             display_username: invitee.user.username,
-            message: "discourse_post_event.notifications.#{event_started ? 'after' : 'before'}_event_reminder"
+            message: "discourse_post_event.notifications.#{prefix}_event_reminder"
           }.to_json
         )
       end
diff --git a/spec/acceptance/post_spec.rb b/spec/acceptance/post_spec.rb
index 07bbef7..f670940 100644
--- a/spec/acceptance/post_spec.rb
+++ b/spec/acceptance/post_spec.rb
@@ -54,7 +54,7 @@ describe Post do
 
         context 'when in edit grace period' do
           before do
-            post_1.reload.event.update_with_params!(starts_at: 2.hours.ago)
+            post_1.reload.event.update_with_params!(starts_at: 3.hours.ago, ends_at: 2.hours.ago)
           end
 
           it 'sends a post revision to going invitees' do
diff --git a/spec/models/discourse_post_event/event_spec.rb b/spec/models/discourse_post_event/event_spec.rb
index 8550d30..eb3590e 100644
--- a/spec/models/discourse_post_event/event_spec.rb
+++ b/spec/models/discourse_post_event/event_spec.rb
@@ -278,7 +278,7 @@ describe DiscoursePostEvent::Event do
             starts_at: DateTime.parse('2020-04-24 14:05')
           )
 
-          expect(post_event.is_expired?).to be(true)
+          expect(post_event.is_expired?).to be(false)
         end
       end
 

GitHub sha: d7bc0659