A platform for community discussion. Free, open, simple.
FIX: Remove scheduled DND timings when schedule is disabed (#11814)
Mark VanLandingham56294b4fba9a
diff --git a/app/models/user_notification_schedule.rb b/app/models/user_notification_schedule.rbindex 18892506db3..f2346d4e173 100644--- a/app/models/user_notification_schedule.rb+++ b/app/models/user_notification_schedule.rb@@ -18,10 +18,14 @@ class UserNotificationSchedule < ActiveRecord::Basescope :enabled, -> { where(enabled: true) }def create_do_not_disturb_timings(delete_existing: false)- user.do_not_disturb_timings.where(scheduled: true).destroy_all if delete_existing+ destroy_scheduled_timings if delete_existingUserNotificationScheduleProcessor.create_do_not_disturb_timings_for(self)end+ def destroy_scheduled_timings+ user.do_not_disturb_timings.where(scheduled: true).destroy_all+ end+privatedef has_valid_timesdiff --git a/app/services/user_updater.rb b/app/services/user_updater.rbindex 41593b84db1..544b9883ca3 100644--- a/app/services/user_updater.rb+++ b/app/services/user_updater.rb@@ -200,7 +200,9 @@ class UserUpdaterif savedif user_notification_schedule- user_notification_schedule.create_do_not_disturb_timings(delete_existing: true)+ user_notification_schedule.enabled ?+ user_notification_schedule.create_do_not_disturb_timings(delete_existing: true) :+ user_notification_schedule.destroy_scheduled_timingsendDiscourseEvent.trigger(:user_updated, user)enddiff --git a/spec/services/user_updater_spec.rb b/spec/services/user_updater_spec.rbindex abbaadf9359..7ed290999e5 100644--- a/spec/services/user_updater_spec.rb+++ b/spec/services/user_updater_spec.rb@@ -251,6 +251,18 @@ describe UserUpdater doupdater.update(user_notification_schedule: schedule_attrs)}.to change { user.do_not_disturb_timings.count }.by(4)end++ it "removes do_not_disturb_timings when the schedule is disabled" do+ updater = UserUpdater.new(acting_user, user)+ updater.update(user_notification_schedule: schedule_attrs)+ expect(user.user_notification_schedule.enabled).to eq(true)++ schedule_attrs[:enabled] = false+ updater.update(user_notification_schedule: schedule_attrs)++ expect(user.user_notification_schedule.enabled).to eq(false)+ expect(user.do_not_disturb_timings.count).to eq(0)+ endendcontext 'when sso overrides bio' do