diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py index 57e38e17a..0e3065a85 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py @@ -1211,29 +1211,14 @@ class ProcessInstanceProcessor: ) processor = ProcessInstanceProcessor(process_instance) deleted_tasks = processor.bpmn_process_instance.reset_from_task_id(UUID(to_task_guid)) + spiff_tasks = processor.bpmn_process_instance.get_tasks() - # Remove all the deleted/pruned tasks from the database. - deleted_task_ids = list(map(lambda t: str(t.id), deleted_tasks)) - tasks_to_clear = TaskModel.query.filter(TaskModel.guid.in_(deleted_task_ids)).all() - human_tasks_to_clear = HumanTaskModel.query.filter(HumanTaskModel.task_id.in_(deleted_task_ids)).all() - for task in tasks_to_clear + human_tasks_to_clear: - db.session.delete(task) - - # Update the database with new taskss - Perhaps we should pull this out into it's own method, as it should help - # anytime we need to update the database with all modified tasks. - spiff_tasks_updated = {} - # Note: Can't restrict this to definite, because some things are updated and are now CANCELLED - for spiff_task in processor.bpmn_process_instance.get_tasks(): - if spiff_task.last_state_change > start_time: - spiff_tasks_updated[str(spiff_task.id)] = spiff_task task_service = TaskService( process_instance=processor.process_instance_model, serializer=processor._serializer, bpmn_definition_to_task_definitions_mappings=processor.bpmn_definition_to_task_definitions_mappings, ) - for id, spiff_task in spiff_tasks_updated.items(): - task_service.update_task_model_with_spiff_task(spiff_task) - task_service.save_objects_to_database() + task_service.update_all_tasks_from_spiff_tasks(spiff_tasks, deleted_tasks, start_time) # Save the process processor.save() diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py index 1eb93e9a5..d7c438939 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py @@ -1,5 +1,6 @@ import copy import json +from spiffworkflow_backend.models.human_task import HumanTaskModel import time from hashlib import sha256 from typing import List @@ -452,6 +453,29 @@ class TaskService: self.update_task_model(task_model, spiff_task) self.task_models[task_model.guid] = task_model + def update_all_tasks_from_spiff_tasks( + self, + spiff_tasks: list[SpiffTask], + deleted_spiff_tasks: list[SpiffTask], + start_time: float + ) -> None: + # Remove all the deleted/pruned tasks from the database. + deleted_task_ids = list(map(lambda t: str(t.id), deleted_spiff_tasks)) + tasks_to_clear = TaskModel.query.filter(TaskModel.guid.in_(deleted_task_ids)).all() # type: ignore + human_tasks_to_clear = HumanTaskModel.query.filter(HumanTaskModel.task_id.in_(deleted_task_ids)).all() # type: ignore + for task in tasks_to_clear + human_tasks_to_clear: + db.session.delete(task) + + # Note: Can't restrict this to definite, because some things are updated and are now CANCELLED + # and other things may have been COMPLETED and are now MAYBE + spiff_tasks_updated = {} + for spiff_task in spiff_tasks: + if spiff_task.last_state_change > start_time: + spiff_tasks_updated[str(spiff_task.id)] = spiff_task + for _id, spiff_task in spiff_tasks_updated.items(): + self.update_task_model_with_spiff_task(spiff_task) + self.save_objects_to_database() + @classmethod def remove_spiff_task_from_parent(cls, spiff_task: SpiffTask, task_models: dict[str, TaskModel]) -> None: """Removes the given spiff task from its parent and then updates the task_models dict with the changes."""