Skip to content

Commit

Permalink
FIX: delete past holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
ZogStriP committed May 28, 2019
1 parent f216ad8 commit 271b9e9
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions jobs/scheduled/check_next_regional_holidays.rb
@@ -1,6 +1,6 @@
module Jobs
class ::DiscourseCalendar::CheckNextRegionalHolidays < Jobs::Scheduled
every 12.hours
every 1.hour

def execute(args)
return unless SiteSetting.calendar_enabled
Expand All @@ -20,23 +20,26 @@ def execute(args)

usernames = User.where(id: user_ids).pluck(:id, :username_lower).to_h

regional_holidays = op.custom_fields[::DiscourseCalendar::CALENDAR_HOLIDAYS_CUSTOM_FIELD] || []
old_regional_holidays = op.custom_fields[::DiscourseCalendar::CALENDAR_HOLIDAYS_CUSTOM_FIELD] || []
new_regional_holidays = []

users_in_region.keys.each do |region|
users_in_region.keys.sort.each do |region|
next if !(next_holiday = Holidays.next_holidays(1, [region]).first)
next if next_holiday[:date] > 1.month.from_now

date = next_holiday[:date].to_s

users_in_region[region].each do |user_id|
if !regional_holidays.find { |r, _, d, u| r == region && d == date && u == usernames[user_id] }
regional_holidays << [region, next_holiday[:name], date, usernames[user_id]]
if !new_regional_holidays.find { |r, _, d, u| r == region && d == date && u == usernames[user_id] }
new_regional_holidays << [region, next_holiday[:name], date, usernames[user_id]]
end
end
end

op.custom_fields[::DiscourseCalendar::CALENDAR_HOLIDAYS_CUSTOM_FIELD] = regional_holidays
op.save_custom_fields(true)
if old_regional_holidays != new_regional_holidays
op.custom_fields[::DiscourseCalendar::CALENDAR_HOLIDAYS_CUSTOM_FIELD] = new_regional_holidays
op.save_custom_fields(true)
end
end
end
end

0 comments on commit 271b9e9

Please sign in to comment.