[fix] Fix event_schedule# happening_now?; Add tests for happening_now JSON endpoint

This commit is contained in:
Jimmy 2021-03-18 18:53:55 +08:00 committed by CactusPuppy
parent 6523eff5ab
commit 2628952142
No known key found for this signature in database
GPG key ID: 4B33B0A3E15E2C82
2 changed files with 41 additions and 10 deletions

View file

@ -55,16 +55,9 @@ class EventSchedule < ApplicationRecord
# True within `threshold` before and after the event.
#
def happening_now?(threshold = 30.minutes)
in_tz_start = start_time.in_time_zone(timezone)
in_tz_end = end_time.in_time_zone(timezone)
in_tz_start -= in_tz_start.utc_offset
in_tz_end -= in_tz_end.utc_offset
begin_range = Time.now - threshold
end_range = Time.now + threshold
event_time_range = in_tz_start..in_tz_end
now_range = begin_range..end_range
# TODO: There's probably better logic.
event_time_range.overlaps?(now_range) && (in_tz_end > Time.now)
event_time_range = start_time..end_time
now_range = (Time.now - threshold)..(Time.now + threshold)
event_time_range.overlaps?(now_range) && (end_time > Time.now)
end
def self.withdrawn_or_canceled_event_schedules(schedule_ids)