From f803fbe9951b09bd677ba3b8c5dda88af258203b Mon Sep 17 00:00:00 2001 From: Jonas Metzener Date: Tue, 30 Aug 2022 13:32:59 +0200 Subject: [PATCH] fix(redo): recalculate deadline after updating a redo work item to ready --- caluma/caluma_workflow/domain_logic.py | 8 +---- .../caluma_workflow/tests/test_work_item.py | 32 +++++++++++++------ caluma/caluma_workflow/utils.py | 19 +++++++++++ 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/caluma/caluma_workflow/domain_logic.py b/caluma/caluma_workflow/domain_logic.py index b5f237043..f25fe2572 100644 --- a/caluma/caluma_workflow/domain_logic.py +++ b/caluma/caluma_workflow/domain_logic.py @@ -180,12 +180,8 @@ def post_complete( ) if all_siblings_complete: - redo_work_items = work_item.case.work_items.filter( - task__in=next_tasks, status=models.WorkItem.STATUS_REDO - ) - created_work_items = utils.create_work_items( - next_tasks.exclude(pk__in=redo_work_items.values("task")), + next_tasks, case, user, work_item, @@ -201,8 +197,6 @@ def post_complete( context=context, ) - redo_work_items.update(status=models.WorkItem.STATUS_READY) - if ( not next_tasks.exists() and not work_item.case.work_items.filter( diff --git a/caluma/caluma_workflow/tests/test_work_item.py b/caluma/caluma_workflow/tests/test_work_item.py index 7d63bb56c..a04cd5fdf 100644 --- a/caluma/caluma_workflow/tests/test_work_item.py +++ b/caluma/caluma_workflow/tests/test_work_item.py @@ -1578,7 +1578,11 @@ def redo_test_setup( task_flow_factory, admin_user, ): - tasks = task_factory.create_batch(5, type=models.Task.TYPE_SIMPLE) + tasks = task_factory.create_batch( + 5, + type=models.Task.TYPE_SIMPLE, + lead_time=86400, # 1 day + ) workflow.start_tasks.add(tasks[0]) @@ -1602,6 +1606,7 @@ def redo_test_setup( return task_flow, case, tasks, work_item_to_be_redone +@pytest.mark.freeze_time("2022-08-30") @pytest.mark.parametrize("use_graphql", [False, True]) def test_redo_work_item( db, @@ -1609,6 +1614,7 @@ def test_redo_work_item( schema_executor, admin_user, use_graphql, + freezer, ): task_flow, case, tasks, work_item_to_be_redone = redo_test_setup @@ -1635,20 +1641,28 @@ def test_redo_work_item( api.redo_work_item(work_item=work_item_to_be_redone, user=admin_user) assert case.work_items.get(task=tasks[0]).status == models.WorkItem.STATUS_READY - assert case.work_items.get(task=tasks[1]).status == models.WorkItem.STATUS_REDO - assert case.work_items.get(task=tasks[2]).status == models.WorkItem.STATUS_REDO - assert case.work_items.get(task=tasks[3]).status == models.WorkItem.STATUS_REDO - assert case.work_items.get(task=tasks[4]).status == models.WorkItem.STATUS_REDO + + for task in tasks[1:]: + work_item = case.work_items.get(task=task) + assert work_item.status == models.WorkItem.STATUS_REDO + assert work_item.deadline.date().isoformat() == "2022-08-31" + + freezer.move_to("2022-09-12") api.complete_work_item( work_item=case.work_items.get(task=tasks[0]), user=admin_user ) assert case.work_items.get(task=tasks[0]).status == models.WorkItem.STATUS_COMPLETED - assert case.work_items.get(task=tasks[1]).status == models.WorkItem.STATUS_READY - assert case.work_items.get(task=tasks[2]).status == models.WorkItem.STATUS_READY - assert case.work_items.get(task=tasks[3]).status == models.WorkItem.STATUS_REDO - assert case.work_items.get(task=tasks[4]).status == models.WorkItem.STATUS_REDO + + for task in tasks[1:3]: + work_item = case.work_items.get(task=task) + assert work_item.status == models.WorkItem.STATUS_READY + assert work_item.deadline.date().isoformat() == "2022-09-13" + + for task in tasks[3:5]: + work_item = case.work_items.get(task=task) + assert work_item.status == models.WorkItem.STATUS_REDO @pytest.mark.parametrize("use_graphql", [False, True]) diff --git a/caluma/caluma_workflow/utils.py b/caluma/caluma_workflow/utils.py index d84433b19..0fef043fa 100644 --- a/caluma/caluma_workflow/utils.py +++ b/caluma/caluma_workflow/utils.py @@ -106,6 +106,25 @@ def create_work_items(tasks, case, user, prev_work_item=None, context: dict = No # work item already exists, do not create a new one continue + work_items_to_redo = models.WorkItem.objects.filter( + addressed_groups=groups, + controlling_groups=controlling_groups, + task_id=task.pk, + case=case, + status=models.WorkItem.STATUS_REDO, + ) + + if work_items_to_redo.exists(): + # there is already an existing work item in status redo that + # needs to be reopened instead of creating a new one. + work_items_to_redo.update( + deadline=task.calculate_deadline(), + modified_by_user=user.username, + modified_by_group=user.username, + status=models.WorkItem.STATUS_READY, + ) + continue + work_items.append( models.WorkItem.objects.create( addressed_groups=groups,