feat: enhance workflow deletion with error handling for in-use and draft workflows
Signed-off-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
parent
295a00ba0d
commit
5e7890f36f
@ -27,7 +27,7 @@ from models.account import Account
|
|||||||
from models.model import AppMode
|
from models.model import AppMode
|
||||||
from services.app_generate_service import AppGenerateService
|
from services.app_generate_service import AppGenerateService
|
||||||
from services.errors.app import WorkflowHashNotEqualError
|
from services.errors.app import WorkflowHashNotEqualError
|
||||||
from services.workflow_service import WorkflowService
|
from services.workflow_service import DraftWorkflowDeletionError, WorkflowInUseError, WorkflowService
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -596,15 +596,18 @@ class WorkflowByIdApi(Resource):
|
|||||||
|
|
||||||
# Create a session and manage the transaction
|
# Create a session and manage the transaction
|
||||||
with Session(db.engine) as session:
|
with Session(db.engine) as session:
|
||||||
success = workflow_service.delete_workflow(
|
try:
|
||||||
session=session, workflow_id=workflow_id, tenant_id=app_model.tenant_id
|
workflow_service.delete_workflow(
|
||||||
)
|
session=session, workflow_id=workflow_id, tenant_id=app_model.tenant_id
|
||||||
|
)
|
||||||
if not success:
|
# Commit the transaction in the controller
|
||||||
raise NotFound("Workflow not found or cannot be deleted")
|
session.commit()
|
||||||
|
except ValueError as e:
|
||||||
# Commit the transaction in the controller
|
raise NotFound(str(e))
|
||||||
session.commit()
|
except WorkflowInUseError as e:
|
||||||
|
abort(400, description=str(e))
|
||||||
|
except DraftWorkflowDeletionError as e:
|
||||||
|
abort(400, description=str(e))
|
||||||
|
|
||||||
return None, 204
|
return None, 204
|
||||||
|
|
||||||
|
10
api/services/errors/workflow_service.py
Normal file
10
api/services/errors/workflow_service.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class WorkflowInUseError(ValueError):
|
||||||
|
"""Raised when attempting to delete a workflow that's in use by an app"""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DraftWorkflowDeletionError(ValueError):
|
||||||
|
"""Raised when attempting to delete a draft workflow"""
|
||||||
|
|
||||||
|
pass
|
@ -35,7 +35,8 @@ from models.workflow import (
|
|||||||
WorkflowType,
|
WorkflowType,
|
||||||
)
|
)
|
||||||
from services.errors.app import WorkflowHashNotEqualError
|
from services.errors.app import WorkflowHashNotEqualError
|
||||||
from services.workflow.workflow_converter import WorkflowConverter
|
|
||||||
|
from .errors.workflow_service import DraftWorkflowDeletionError, WorkflowInUseError
|
||||||
|
|
||||||
|
|
||||||
class WorkflowService:
|
class WorkflowService:
|
||||||
@ -491,20 +492,27 @@ class WorkflowService:
|
|||||||
:param session: SQLAlchemy database session
|
:param session: SQLAlchemy database session
|
||||||
:param workflow_id: Workflow ID
|
:param workflow_id: Workflow ID
|
||||||
:param tenant_id: Tenant ID
|
:param tenant_id: Tenant ID
|
||||||
:return: True if deletable, False if not found or in use
|
:return: True if successful
|
||||||
|
:raises: ValueError if workflow not found
|
||||||
|
:raises: WorkflowInUseError if workflow is in use
|
||||||
|
:raises: DraftWorkflowDeletionError if workflow is a draft version
|
||||||
"""
|
"""
|
||||||
stmt = select(Workflow).where(Workflow.id == workflow_id, Workflow.tenant_id == tenant_id)
|
stmt = select(Workflow).where(Workflow.id == workflow_id, Workflow.tenant_id == tenant_id)
|
||||||
workflow = session.scalar(stmt)
|
workflow = session.scalar(stmt)
|
||||||
|
|
||||||
if not workflow:
|
if not workflow:
|
||||||
return False
|
raise ValueError(f"Workflow with ID {workflow_id} not found")
|
||||||
|
|
||||||
|
# Check if workflow is a draft version
|
||||||
|
if workflow.version == "draft":
|
||||||
|
raise DraftWorkflowDeletionError("Cannot delete draft workflow versions")
|
||||||
|
|
||||||
# Check if this workflow is currently referenced by an app
|
# Check if this workflow is currently referenced by an app
|
||||||
stmt = select(App).where(App.workflow_id == workflow_id)
|
stmt = select(App).where(App.workflow_id == workflow_id)
|
||||||
app = session.scalar(stmt)
|
app = session.scalar(stmt)
|
||||||
if app:
|
if app:
|
||||||
# Cannot delete a workflow that's currently in use by an app
|
# Cannot delete a workflow that's currently in use by an app
|
||||||
return False
|
raise WorkflowInUseError(f"Cannot delete workflow that is currently in use by app '{app.name}'")
|
||||||
|
|
||||||
session.delete(workflow)
|
session.delete(workflow)
|
||||||
return True
|
return True
|
||||||
|
Loading…
Reference in New Issue
Block a user