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.
This commit is contained in:
Joffrey JAFFEUX
2024-04-19 14:23:57 +02:00
committed by GitHub
parent 5021f8a7da
commit bf715c8235
5 changed files with 152 additions and 15 deletions

View File

@ -40,4 +40,49 @@ describe "PointInTime" do
end
end
end
context "when updating automation" do
fab!(:automation) do
Fabricate(:automation, trigger: DiscourseAutomation::Triggers::POINT_IN_TIME, script: "test")
end
before do
DiscourseAutomation::Scriptable.add("test") do
triggerables [DiscourseAutomation::Triggers::POINT_IN_TIME]
field :test, component: :text
end
automation.upsert_field!(
"execute_at",
"date_time",
{ value: 2.hours.from_now },
target: "trigger",
)
automation.upsert_field!("test", "text", { value: "something" }, target: "script")
end
context "when execute_at changes" do
it "resets the pending automations" do
expect {
automation.upsert_field!(
"execute_at",
"date_time",
{ value: 3.hours.from_now },
target: "trigger",
)
}.to change { DiscourseAutomation::PendingAutomation.last.execute_at }
expect(DiscourseAutomation::PendingAutomation.count).to eq(1)
end
end
context "when a field other than execute_at changes" do
it "doesn't reset the pending automations" do
expect {
automation.upsert_field!("test", "text", { value: "somethingelse" }, target: "script")
}.to_not change { DiscourseAutomation::PendingAutomation.last.execute_at }
expect(DiscourseAutomation::PendingAutomation.count).to eq(1)
end
end
end
end

View File

@ -58,6 +58,70 @@ describe "Recurring" do
end
end
context "when updating automation" do
fab!(:automation) do
Fabricate(:automation, trigger: DiscourseAutomation::Triggers::RECURRING, script: "test")
end
before do
DiscourseAutomation::Scriptable.add("test") do
triggerables [DiscourseAutomation::Triggers::RECURRING]
field :test, component: :text
end
automation.upsert_field!(
"start_date",
"date_time",
{ value: 2.hours.from_now },
target: "trigger",
)
upsert_period_field!(1, "week")
automation.upsert_field!("test", "text", { value: "something" }, target: "script")
end
context "when start date changes" do
it "resets the pending automations" do
expect {
automation.upsert_field!(
"start_date",
"date_time",
{ value: 1.day.from_now },
target: "trigger",
)
}.to change { DiscourseAutomation::PendingAutomation.last.execute_at }
expect(DiscourseAutomation::PendingAutomation.count).to eq(1)
end
end
context "when interval changes" do
it "resets the pending automations" do
expect { upsert_period_field!(2, "week") }.to change {
DiscourseAutomation::PendingAutomation.last.execute_at
}
expect(DiscourseAutomation::PendingAutomation.count).to eq(1)
end
end
context "when frequency changes" do
it "resets the pending automations" do
expect { upsert_period_field!(1, "month") }.to change {
DiscourseAutomation::PendingAutomation.last.execute_at
}
expect(DiscourseAutomation::PendingAutomation.count).to eq(1)
end
end
context "when a non recurrence related field changes" do
it "doesn't reset the pending automations" do
expect {
automation.upsert_field!("test", "text", { value: "somethingelse" }, target: "script")
}.to_not change { DiscourseAutomation::PendingAutomation.last.execute_at }
expect(DiscourseAutomation::PendingAutomation.count).to eq(1)
end
end
end
context "when trigger is called" do
before do
freeze_time Time.zone.parse("2021-06-04 10:00")