FIX: Clean up topic_timers when no longer valid.

FIX: Clean up topic_timers when no longer valid.

This was causing a bug where the jobs for the topic_timers will keep getting enqueued over and over again.

diff --git a/app/jobs/regular/toggle_topic_closed.rb b/app/jobs/regular/toggle_topic_closed.rb
index 2cd0a31..7e459bc 100644
--- a/app/jobs/regular/toggle_topic_closed.rb
+++ b/app/jobs/regular/toggle_topic_closed.rb
@@ -6,11 +6,12 @@ module Jobs
       topic_timer = TopicTimer.find_by(id: args[:topic_timer_id] || args[:topic_status_update_id])
       state = !!args[:state]
 
-      if topic_timer.blank? ||
-         topic_timer.execute_at > Time.zone.now ||
-         (topic = topic_timer.topic).blank? ||
-         topic.closed == state
+      if topic_timer.blank? || topic_timer.execute_at > Time.zone.now
+        return
+      end
 
+      if (topic = topic_timer.topic).blank? || topic.closed == state
+        topic_timer.destroy!
         return
       end
 
diff --git a/spec/jobs/toggle_topic_closed_spec.rb b/spec/jobs/toggle_topic_closed_spec.rb
index d903b69..5cbd3e2 100644
--- a/spec/jobs/toggle_topic_closed_spec.rb
+++ b/spec/jobs/toggle_topic_closed_spec.rb
@@ -72,16 +72,33 @@ describe Jobs::ToggleTopicClosed do
     end
   end
 
+  describe 'when trying to close a topic that has already been closed' do
+    it 'should delete the topic timer' do
+      freeze_time(topic.public_topic_timer.execute_at + 1.minute)
+
+      topic.update!(closed: true)
+
+      expect do
+        described_class.new.execute(
+          topic_timer_id: topic.public_topic_timer.id,
+          state: true
+        )
+      end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
+    end
+  end
+
   describe 'when trying to close a topic that has been deleted' do
-    it 'should not do anything' do
-      topic.trash!
+    it 'should delete the topic timer' do
+      freeze_time(topic.public_topic_timer.execute_at + 1.minute)
 
-      Topic.any_instance.expects(:update_status).never
+      topic.trash!
 
-      described_class.new.execute(
-        topic_timer_id: topic.public_topic_timer.id,
-        state: true
-      )
+      expect do
+        described_class.new.execute(
+          topic_timer_id: topic.public_topic_timer.id,
+          state: true
+        )
+      end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
     end
   end
 

GitHub sha: 5bca1aec