From ef2b92a39761fb4ddae251bc9e76e262bf50cd6d Mon Sep 17 00:00:00 2001 From: -LAN- Date: Fri, 9 May 2025 12:24:18 +0800 Subject: [PATCH] Update service layer to work with domain model --- api/controllers/console/app/workflow_run.py | 13 +++++++++--- api/services/workflow_run_service.py | 22 ++++++++++----------- api/services/workflow_service.py | 6 ++++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/api/controllers/console/app/workflow_run.py b/api/controllers/console/app/workflow_run.py index 08ab61bbb9..9099700213 100644 --- a/api/controllers/console/app/workflow_run.py +++ b/api/controllers/console/app/workflow_run.py @@ -1,3 +1,6 @@ +from typing import cast + +from flask_login import current_user from flask_restful import Resource, marshal_with, reqparse from flask_restful.inputs import int_range @@ -12,8 +15,7 @@ from fields.workflow_run_fields import ( ) from libs.helper import uuid_value from libs.login import login_required -from models import App -from models.model import AppMode +from models import Account, App, AppMode, EndUser from services.workflow_run_service import WorkflowRunService @@ -90,7 +92,12 @@ class WorkflowRunNodeExecutionListApi(Resource): run_id = str(run_id) workflow_run_service = WorkflowRunService() - node_executions = workflow_run_service.get_workflow_run_node_executions(app_model=app_model, run_id=run_id) + user = cast("Account | EndUser", current_user) + node_executions = workflow_run_service.get_workflow_run_node_executions( + app_model=app_model, + run_id=run_id, + user=user, + ) return {"data": node_executions} diff --git a/api/services/workflow_run_service.py b/api/services/workflow_run_service.py index 6d5b737962..d5f9a46016 100644 --- a/api/services/workflow_run_service.py +++ b/api/services/workflow_run_service.py @@ -1,4 +1,5 @@ import threading +from collections.abc import Sequence from typing import Optional import contexts @@ -6,12 +7,7 @@ from core.repositories import SQLAlchemyWorkflowNodeExecutionRepository from core.workflow.repository.workflow_node_execution_repository import OrderConfig from extensions.ext_database import db from libs.infinite_scroll_pagination import InfiniteScrollPagination -from models.enums import WorkflowRunTriggeredFrom -from models.model import App -from models.workflow import ( - WorkflowNodeExecution, - WorkflowRun, -) +from models import Account, App, EndUser, WorkflowNodeExecution, WorkflowRun, WorkflowRunTriggeredFrom class WorkflowRunService: @@ -116,7 +112,12 @@ class WorkflowRunService: return workflow_run - def get_workflow_run_node_executions(self, app_model: App, run_id: str) -> list[WorkflowNodeExecution]: + def get_workflow_run_node_executions( + self, + app_model: App, + run_id: str, + user: Account | EndUser, + ) -> Sequence[WorkflowNodeExecution]: """ Get workflow run node execution list """ @@ -128,13 +129,12 @@ class WorkflowRunService: if not workflow_run: return [] - # Use the repository to get the node executions repository = SQLAlchemyWorkflowNodeExecutionRepository( - session_factory=db.engine, tenant_id=app_model.tenant_id, app_id=app_model.id + session_factory=db.engine, user=user, app_id=app_model.id ) # Use the repository to get the node executions with ordering order_config = OrderConfig(order_by=["index"], order_direction="desc") - node_executions = repository.get_by_workflow_run(workflow_run_id=run_id, order_config=order_config) + node_executions = repository.get_db_models_by_workflow_run(workflow_run_id=run_id, order_config=order_config) - return list(node_executions) + return node_executions diff --git a/api/services/workflow_service.py b/api/services/workflow_service.py index 331dba8bf1..a6eeb09ded 100644 --- a/api/services/workflow_service.py +++ b/api/services/workflow_service.py @@ -284,9 +284,11 @@ class WorkflowService: workflow_node_execution.created_by = account.id workflow_node_execution.workflow_id = draft_workflow.id - # Use the repository to save the workflow node execution repository = SQLAlchemyWorkflowNodeExecutionRepository( - session_factory=db.engine, tenant_id=app_model.tenant_id, app_id=app_model.id + session_factory=db.engine, + user=account, + app_id=app_model.id, + triggered_from=WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP, ) repository.save(workflow_node_execution)