Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions django_forms_workflows/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,24 +1166,60 @@ def check_approval_deadlines() -> str:
if not locked_submission:
continue

pending_locked_tasks = list(
locked_tasks = list(
locked_submission.approval_tasks.select_related(
"workflow_stage__workflow"
).filter(status="pending")
)
)
pending_locked_tasks = [
task for task in locked_tasks if task.status == "pending"
]
if not pending_locked_tasks:
continue

def task_track(task: ApprovalTask) -> tuple[str, int | None]:
if task.workflow_stage_id:
return ("workflow", task.workflow_stage.workflow_id)
return ("legacy", None)

tracks: dict[tuple[str, int | None], list[ApprovalTask]] = (
defaultdict(list)
)
for task in pending_locked_tasks:
if task.workflow_stage_id:
key = ("workflow", task.workflow_stage.workflow_id)
else:
# Legacy tasks without a stage belong to the fallback track.
key = ("legacy", None)
tracks[key].append(task)
tracks[task_track(task)].append(task)

# A send-back starts a new round only when fresh tasks were
# created on that same track after the returned decision.
# Keep failures on other tracks (or in the current round)
# blocking approval, before changing any pending task state.
latest_task_created_by_track = {}
for task in locked_tasks:
key = task_track(task)
latest_created = latest_task_created_by_track.get(key)
if latest_created is None or task.created_at > latest_created:
latest_task_created_by_track[key] = task.created_at

round_start_by_track = {}
for task in locked_tasks:
if task.status != "returned" or task.completed_at is None:
continue
key = task_track(task)
if latest_task_created_by_track[key] <= task.completed_at:
continue
previous_start = round_start_by_track.get(key)
if previous_start is None or task.completed_at > previous_start:
round_start_by_track[key] = task.completed_at

if any(
task.status in ("expired", "rejected", "returned")
and (
task_track(task) not in round_start_by_track
or (task.completed_at or task.created_at)
> round_start_by_track[task_track(task)]
)
for task in locked_tasks
):
continue

for track_key, track_pending_tasks in tracks.items():
latest_track_task = max(
Expand Down Expand Up @@ -1225,13 +1261,6 @@ def check_approval_deadlines() -> str:
).exists():
continue

# A different track may have failed already. Auto-skipped
# tasks are valid here, but a failed decision is not.
if locked_submission.approval_tasks.filter(
status__in=("expired", "rejected", "returned")
).exists():
continue

updated = FormSubmission.objects.filter(
id=locked_submission.id,
status="pending_approval",
Expand Down
116 changes: 116 additions & 0 deletions tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -3349,6 +3349,122 @@ def test_auto_approve_does_not_override_failed_track(
due_task.refresh_from_db()
assert "auto_approved=0" in result
assert submission.status == "pending_approval"
assert due_task.status == "pending"


@pytest.mark.django_db
def test_auto_approve_after_send_back_starts_new_round(
form_definition, user, approver_user, django_capture_on_commit_callbacks
):
workflow = WorkflowDefinition.objects.create(
form_definition=form_definition, auto_approve_after_days=2
)
first_stage = WorkflowStage.objects.create(
workflow=workflow, name="Correction", order=1
)
second_stage = WorkflowStage.objects.create(
workflow=workflow, name="Review", order=2
)
submission = FormSubmission.objects.create(
form_definition=form_definition,
submitter=user,
form_data={},
status="pending_approval",
)
returned_task = ApprovalTask.objects.create(
submission=submission,
workflow_stage=second_stage,
step_name="Review",
assigned_to=approver_user,
status="returned",
)
ApprovalTask.objects.filter(pk=returned_task.pk).update(
created_at=timezone.now() - timedelta(days=5),
completed_at=timezone.now() - timedelta(days=4),
)
correction_task = ApprovalTask.objects.create(
submission=submission,
workflow_stage=first_stage,
step_name="Correction",
assigned_to=approver_user,
status="pending",
)
ApprovalTask.objects.filter(pk=correction_task.pk).update(
created_at=timezone.now() - timedelta(days=3)
)

with (
patch.object(tasks_module.send_notification_rules, "delay") as notify,
django_capture_on_commit_callbacks(execute=True),
):
result = check_approval_deadlines()

submission.refresh_from_db()
correction_task.refresh_from_db()
assert "auto_approved=1" in result
assert submission.status == "approved"
assert correction_task.status == "skipped"
notify.assert_called_once_with(submission.pk, "workflow_approved")


@pytest.mark.django_db
def test_auto_approve_ignores_recovered_track_return(
form_definition, user, approver_user
):
recovered_workflow = WorkflowDefinition.objects.create(
form_definition=form_definition
)
due_workflow = WorkflowDefinition.objects.create(
form_definition=form_definition, auto_approve_after_days=2
)
recovered_stage = WorkflowStage.objects.create(
workflow=recovered_workflow, name="Recovered", order=1
)
due_stage = WorkflowStage.objects.create(workflow=due_workflow, name="Due", order=1)
submission = FormSubmission.objects.create(
form_definition=form_definition,
submitter=user,
form_data={},
status="pending_approval",
)
returned_task = ApprovalTask.objects.create(
submission=submission,
workflow_stage=recovered_stage,
step_name="Returned",
assigned_to=approver_user,
status="returned",
)
ApprovalTask.objects.filter(pk=returned_task.pk).update(
created_at=timezone.now() - timedelta(days=6),
completed_at=timezone.now() - timedelta(days=5),
)
recovered_task = ApprovalTask.objects.create(
submission=submission,
workflow_stage=recovered_stage,
step_name="Recovered",
assigned_to=approver_user,
status="approved",
)
ApprovalTask.objects.filter(pk=recovered_task.pk).update(
created_at=timezone.now() - timedelta(days=4)
)
due_task = ApprovalTask.objects.create(
submission=submission,
workflow_stage=due_stage,
step_name="Due",
assigned_to=approver_user,
status="pending",
)
ApprovalTask.objects.filter(pk=due_task.pk).update(
created_at=timezone.now() - timedelta(days=3)
)

result = check_approval_deadlines()

submission.refresh_from_db()
due_task.refresh_from_db()
assert "auto_approved=1" in result
assert submission.status == "approved"
assert due_task.status == "skipped"


Expand Down
Loading