Joffrey JAFFEUX bf715c8235
FIX: resets pending automations only if necessary (#26685)
Prior to this fix, any change to an automation would reset `pending_automations`, now we only do it if any value related to recurrence (start_date, interval, frequency, execute_at...) has been changed.

It means that any trigger creating `pending_automations` now needs to manage them in the `on_update` callback.
2024-04-19 14:23:57 +02:00

20 lines
672 B
Ruby

# frozen_string_literal: true
DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::POINT_IN_TIME) do
field :execute_at, component: :date_time, required: true
on_update do |automation, fields, previous_fields|
execute_at = fields.dig("execute_at", "value")
previous_execute_at = previous_fields&.dig("execute_at", "value")
if execute_at != previous_execute_at
automation.pending_automations.destroy_all
# prevents creating a new pending automation on save when date is expired
if execute_at && execute_at > Time.zone.now
automation.pending_automations.create!(execute_at: execute_at)
end
end
end
end