知你客服
This commit is contained in:
@@ -22,6 +22,22 @@ logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/v1/workflows", tags=["workflows"])
|
||||
|
||||
|
||||
def _workflow_to_response(workflow: Workflow) -> dict:
|
||||
"""将Workflow对象转换为响应格式"""
|
||||
return {
|
||||
"id": workflow.id,
|
||||
"name": workflow.name,
|
||||
"description": workflow.description,
|
||||
"nodes": workflow.nodes,
|
||||
"edges": workflow.edges,
|
||||
"version": workflow.version,
|
||||
"status": workflow.status,
|
||||
"user_id": workflow.user_id if workflow.user_id else None,
|
||||
"created_at": workflow.created_at if workflow.created_at else datetime.now(),
|
||||
"updated_at": workflow.updated_at if workflow.updated_at else datetime.now()
|
||||
}
|
||||
|
||||
|
||||
class WorkflowCreate(BaseModel):
|
||||
"""工作流创建模型"""
|
||||
name: str
|
||||
@@ -48,7 +64,7 @@ class WorkflowResponse(BaseModel):
|
||||
edges: List[Dict[str, Any]]
|
||||
version: int
|
||||
status: str
|
||||
user_id: str
|
||||
user_id: Optional[str] # 允许为None
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
@@ -117,7 +133,7 @@ async def create_workflow_from_template(
|
||||
db.add(workflow)
|
||||
db.commit()
|
||||
db.refresh(workflow)
|
||||
return workflow
|
||||
return _workflow_to_response(workflow)
|
||||
|
||||
|
||||
@router.get("", response_model=List[WorkflowResponse])
|
||||
@@ -187,7 +203,9 @@ async def get_workflows(
|
||||
query = query.order_by(order_by.desc())
|
||||
|
||||
workflows = query.offset(skip).limit(limit).all()
|
||||
return workflows
|
||||
|
||||
# 转换为响应格式,确保user_id和日期时间字段正确处理
|
||||
return [_workflow_to_response(w) for w in workflows]
|
||||
|
||||
|
||||
@router.post("", response_model=WorkflowResponse, status_code=status.HTTP_201_CREATED)
|
||||
@@ -216,7 +234,7 @@ async def create_workflow(
|
||||
db.add(workflow)
|
||||
db.commit()
|
||||
db.refresh(workflow)
|
||||
return workflow
|
||||
return _workflow_to_response(workflow)
|
||||
|
||||
|
||||
@router.get("/{workflow_id}", response_model=WorkflowResponse)
|
||||
@@ -235,7 +253,7 @@ async def get_workflow(
|
||||
if not check_workflow_permission(db, current_user, workflow, "read"):
|
||||
raise HTTPException(status_code=403, detail="无权访问此工作流")
|
||||
|
||||
return workflow
|
||||
return _workflow_to_response(workflow)
|
||||
|
||||
|
||||
@router.put("/{workflow_id}", response_model=WorkflowResponse)
|
||||
@@ -299,7 +317,7 @@ async def update_workflow(
|
||||
workflow.version += 1
|
||||
db.commit()
|
||||
db.refresh(workflow)
|
||||
return workflow
|
||||
return _workflow_to_response(workflow)
|
||||
|
||||
|
||||
@router.delete("/{workflow_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
@@ -395,7 +413,7 @@ async def import_workflow(
|
||||
db.add(workflow)
|
||||
db.commit()
|
||||
db.refresh(workflow)
|
||||
return workflow
|
||||
return _workflow_to_response(workflow)
|
||||
|
||||
|
||||
@router.post("/{workflow_id}/execute")
|
||||
@@ -631,4 +649,4 @@ async def rollback_workflow_version(
|
||||
|
||||
logger.info(f"工作流 {workflow_id} 已回滚到版本 {version}")
|
||||
|
||||
return workflow
|
||||
return _workflow_to_response(workflow)
|
||||
|
||||
Reference in New Issue
Block a user