feat(fase4): add FlowTemplate model and API

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Claude AI
2026-01-29 11:10:54 +00:00
parent 835c4aa387
commit b8d97e2557
5 changed files with 142 additions and 1 deletions

View File

@@ -2,7 +2,7 @@ from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from app.core.config import get_settings from app.core.config import get_settings
from app.core.database import engine, Base from app.core.database import engine, Base
from app.routers import auth, whatsapp, flows, queues, supervisor from app.routers import auth, whatsapp, flows, queues, supervisor, flow_templates
settings = get_settings() settings = get_settings()
@@ -31,6 +31,7 @@ app.include_router(whatsapp.router)
app.include_router(flows.router) app.include_router(flows.router)
app.include_router(queues.router) app.include_router(queues.router)
app.include_router(supervisor.router) app.include_router(supervisor.router)
app.include_router(flow_templates.router)
@app.get("/health") @app.get("/health")

View File

@@ -4,6 +4,7 @@ from app.models.flow import Flow, FlowSession, TriggerType
from app.models.queue import Queue, QueueAgent, AssignmentMethod from app.models.queue import Queue, QueueAgent, AssignmentMethod
from app.models.quick_reply import QuickReply from app.models.quick_reply import QuickReply
from app.models.global_variable import GlobalVariable from app.models.global_variable import GlobalVariable
from app.models.flow_template import FlowTemplate
__all__ = [ __all__ = [
"User", "User",
@@ -19,4 +20,5 @@ __all__ = [
"AssignmentMethod", "AssignmentMethod",
"QuickReply", "QuickReply",
"GlobalVariable", "GlobalVariable",
"FlowTemplate",
] ]

View File

@@ -0,0 +1,21 @@
import uuid
from datetime import datetime
from sqlalchemy import Column, String, Text, DateTime, Boolean, ForeignKey
from sqlalchemy.dialects.postgresql import UUID, JSONB
from app.core.database import Base
class FlowTemplate(Base):
__tablename__ = "flow_templates"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String(100), nullable=False)
description = Column(Text, nullable=True)
category = Column(String(50), default="general", nullable=False)
nodes = Column(JSONB, default=list)
edges = Column(JSONB, default=list)
variables = Column(JSONB, default=dict)
preview_image = Column(String(500), nullable=True)
is_public = Column(Boolean, default=True, nullable=False)
created_by = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)

View File

@@ -0,0 +1,86 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List, Optional
from uuid import UUID
from app.core.database import get_db
from app.core.security import get_current_user
from app.models.user import User
from app.models.flow_template import FlowTemplate
from app.models.flow import Flow, TriggerType
from app.schemas.flow_template import FlowTemplateCreate, FlowTemplateResponse
router = APIRouter(prefix="/api/flow-templates", tags=["flow-templates"])
@router.get("", response_model=List[FlowTemplateResponse])
def list_templates(
category: Optional[str] = None,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
query = db.query(FlowTemplate).filter(FlowTemplate.is_public == True)
if category:
query = query.filter(FlowTemplate.category == category)
return query.all()
@router.post("", response_model=FlowTemplateResponse)
def create_template(
request: FlowTemplateCreate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
template = FlowTemplate(
**request.model_dump(),
created_by=current_user.id,
)
db.add(template)
db.commit()
db.refresh(template)
return template
@router.post("/{template_id}/use")
def use_template(
template_id: UUID,
flow_name: str,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
"""Create a new flow from template"""
template = db.query(FlowTemplate).filter(FlowTemplate.id == template_id).first()
if not template:
raise HTTPException(status_code=404, detail="Template not found")
flow = Flow(
name=flow_name,
description=f"Created from template: {template.name}",
trigger_type=TriggerType.KEYWORD,
nodes=template.nodes,
edges=template.edges,
variables=template.variables,
is_active=False,
)
db.add(flow)
db.commit()
db.refresh(flow)
return {"success": True, "flow_id": str(flow.id)}
@router.delete("/{template_id}")
def delete_template(
template_id: UUID,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
template = db.query(FlowTemplate).filter(FlowTemplate.id == template_id).first()
if not template:
raise HTTPException(status_code=404, detail="Template not found")
if template.created_by != current_user.id:
raise HTTPException(status_code=403, detail="Not authorized")
db.delete(template)
db.commit()
return {"success": True}

View File

@@ -0,0 +1,31 @@
from pydantic import BaseModel
from typing import Optional, List
from uuid import UUID
from datetime import datetime
class FlowTemplateCreate(BaseModel):
name: str
description: Optional[str] = None
category: str = "general"
nodes: List[dict] = []
edges: List[dict] = []
variables: dict = {}
is_public: bool = True
class FlowTemplateResponse(BaseModel):
id: UUID
name: str
description: Optional[str]
category: str
nodes: List[dict]
edges: List[dict]
variables: dict
preview_image: Optional[str]
is_public: bool
created_by: UUID
created_at: datetime
class Config:
from_attributes = True