Enhancement: support negative offset in scheduled workflows (#9746)

This commit is contained in:
shamoon
2025-05-11 13:04:46 -07:00
committed by GitHub
parent 73f0f1212d
commit 344cc70cd5
7 changed files with 227 additions and 97 deletions

View File

@@ -1336,6 +1336,8 @@ class TestWorkflows(
GIVEN:
- Existing workflow with SCHEDULED trigger against the created field and action that assigns owner
- Existing doc that matches the trigger
- Workflow set to trigger at (now - offset) = now - 1 day
- Document created date is 2 days ago → trigger condition met
WHEN:
- Scheduled workflows are checked
THEN:
@@ -1359,7 +1361,7 @@ class TestWorkflows(
w.save()
now = timezone.localtime(timezone.now())
created = now - timedelta(weeks=520)
created = now - timedelta(days=2)
doc = Document.objects.create(
title="sample test",
correspondent=self.c,
@@ -1377,6 +1379,8 @@ class TestWorkflows(
GIVEN:
- Existing workflow with SCHEDULED trigger against the added field and action that assigns owner
- Existing doc that matches the trigger
- Workflow set to trigger at (now - offset) = now - 1 day
- Document added date is 365 days ago
WHEN:
- Scheduled workflows are checked
THEN:
@@ -1418,6 +1422,8 @@ class TestWorkflows(
GIVEN:
- Existing workflow with SCHEDULED trigger against the modified field and action that assigns owner
- Existing doc that matches the trigger
- Workflow set to trigger at (now - offset) = now - 1 day
- Document modified date is mocked as sufficiently in the past
WHEN:
- Scheduled workflows are checked
THEN:
@@ -1458,6 +1464,8 @@ class TestWorkflows(
GIVEN:
- Existing workflow with SCHEDULED trigger against a custom field and action that assigns owner
- Existing doc that matches the trigger
- Workflow set to trigger at (now - offset) = now - 1 day
- Custom field date is 2 days ago
WHEN:
- Scheduled workflows are checked
THEN:
@@ -1502,6 +1510,7 @@ class TestWorkflows(
GIVEN:
- Existing workflow with SCHEDULED trigger
- Existing doc that has already had the workflow run
- Document created 2 days ago, workflow offset = 1 day → trigger time = yesterday
WHEN:
- Scheduled workflows are checked
THEN:
@@ -1552,6 +1561,7 @@ class TestWorkflows(
GIVEN:
- Existing workflow with SCHEDULED trigger and recurring interval of 7 days
- Workflow run date is 6 days ago
- Document created 40 days ago, offset = 30 → trigger time = 10 days ago
WHEN:
- Scheduled workflows are checked
THEN:
@@ -1600,6 +1610,58 @@ class TestWorkflows(
doc.refresh_from_db()
self.assertIsNone(doc.owner)
def test_workflow_scheduled_trigger_negative_offset(self):
"""
GIVEN:
- Existing workflow with SCHEDULED trigger and negative offset of -7 days (so 7 days after date)
- Custom field date initially set to 5 days ago → trigger time = 2 days in future
- Then updated to 8 days ago → trigger time = 1 day ago
WHEN:
- Scheduled workflows are checked for document with custom field date 8 days in the past
THEN:
- Workflow runs and document owner is updated
"""
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.SCHEDULED,
schedule_offset_days=-7,
schedule_date_field=WorkflowTrigger.ScheduleDateField.CUSTOM_FIELD,
schedule_date_custom_field=self.cf1,
)
action = WorkflowAction.objects.create(
assign_title="Doc assign owner",
assign_owner=self.user2,
)
w = Workflow.objects.create(
name="Workflow 1",
order=0,
)
w.triggers.add(trigger)
w.actions.add(action)
w.save()
doc = Document.objects.create(
title="sample test",
correspondent=self.c,
original_filename="sample.pdf",
)
cfi = CustomFieldInstance.objects.create(
document=doc,
field=self.cf1,
value_date=timezone.now() - timedelta(days=5),
)
tasks.check_scheduled_workflows()
doc.refresh_from_db()
self.assertIsNone(doc.owner) # has not triggered yet
cfi.value_date = timezone.now() - timedelta(days=8)
cfi.save()
tasks.check_scheduled_workflows()
doc.refresh_from_db()
self.assertEqual(doc.owner, self.user2)
def test_workflow_enabled_disabled(self):
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,