diff --git a/docker-compose.yml b/docker-compose.yml index 03c67af..a8c8a87 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -81,6 +81,25 @@ services: networks: - wac_network + flow-engine: + build: + context: ./services/flow-engine + dockerfile: Dockerfile + container_name: wac_flow_engine + restart: unless-stopped + environment: + DATABASE_URL: postgresql://${DB_USER:-whatsapp_admin}:${DB_PASSWORD}@postgres:5432/${DB_NAME:-whatsapp_central} + REDIS_URL: redis://redis:6379 + API_GATEWAY_URL: http://api-gateway:8000 + WHATSAPP_CORE_URL: http://whatsapp-core:3001 + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + networks: + - wac_network + frontend: build: context: ./frontend diff --git a/services/api-gateway/app/models/__init__.py b/services/api-gateway/app/models/__init__.py index c8224b7..a8e5850 100644 --- a/services/api-gateway/app/models/__init__.py +++ b/services/api-gateway/app/models/__init__.py @@ -1,4 +1,5 @@ from app.models.user import User from app.models.whatsapp import WhatsAppAccount, Contact, Conversation, Message +from app.models.flow import Flow, FlowSession, TriggerType -__all__ = ["User", "WhatsAppAccount", "Contact", "Conversation", "Message"] +__all__ = ["User", "WhatsAppAccount", "Contact", "Conversation", "Message", "Flow", "FlowSession", "TriggerType"] diff --git a/services/api-gateway/app/models/flow.py b/services/api-gateway/app/models/flow.py new file mode 100644 index 0000000..35e9055 --- /dev/null +++ b/services/api-gateway/app/models/flow.py @@ -0,0 +1,44 @@ +import uuid +from datetime import datetime +from sqlalchemy import Column, String, Boolean, DateTime, Text, Integer, Enum as SQLEnum +from sqlalchemy.dialects.postgresql import UUID, JSONB +import enum +from app.core.database import Base + + +class TriggerType(str, enum.Enum): + WELCOME = "welcome" + KEYWORD = "keyword" + FALLBACK = "fallback" + EVENT = "event" + MANUAL = "manual" + + +class Flow(Base): + __tablename__ = "flows" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + name = Column(String(100), nullable=False) + description = Column(Text, nullable=True) + trigger_type = Column(SQLEnum(TriggerType), nullable=False) + trigger_value = Column(String(255), nullable=True) + nodes = Column(JSONB, default=list) + edges = Column(JSONB, default=list) + variables = Column(JSONB, default=dict) + is_active = Column(Boolean, default=False, nullable=False) + version = Column(Integer, default=1, nullable=False) + created_at = Column(DateTime, default=datetime.utcnow, nullable=False) + updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) + + +class FlowSession(Base): + __tablename__ = "flow_sessions" + + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + conversation_id = Column(UUID(as_uuid=True), nullable=False, index=True) + flow_id = Column(UUID(as_uuid=True), nullable=False) + current_node_id = Column(String(100), nullable=True) + variables = Column(JSONB, default=dict) + waiting_for_input = Column(Boolean, default=False) + created_at = Column(DateTime, default=datetime.utcnow, nullable=False) + updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) diff --git a/services/flow-engine/Dockerfile b/services/flow-engine/Dockerfile new file mode 100644 index 0000000..216f786 --- /dev/null +++ b/services/flow-engine/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.11-slim + +WORKDIR /app + +RUN apt-get update && apt-get install -y --no-install-recommends \ + gcc \ + libpq-dev \ + && rm -rf /var/lib/apt/lists/* + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY app ./app + +EXPOSE 8001 + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001"] diff --git a/services/flow-engine/app/__init__.py b/services/flow-engine/app/__init__.py new file mode 100644 index 0000000..d9dc1e8 --- /dev/null +++ b/services/flow-engine/app/__init__.py @@ -0,0 +1 @@ +# WhatsApp Centralizado - Flow Engine diff --git a/services/flow-engine/app/config.py b/services/flow-engine/app/config.py new file mode 100644 index 0000000..deb0f28 --- /dev/null +++ b/services/flow-engine/app/config.py @@ -0,0 +1,17 @@ +from pydantic_settings import BaseSettings +from functools import lru_cache + + +class Settings(BaseSettings): + DATABASE_URL: str = "postgresql://whatsapp_admin:password@localhost:5432/whatsapp_central" + REDIS_URL: str = "redis://localhost:6379" + API_GATEWAY_URL: str = "http://localhost:8000" + WHATSAPP_CORE_URL: str = "http://localhost:3001" + + class Config: + env_file = ".env" + + +@lru_cache +def get_settings() -> Settings: + return Settings() diff --git a/services/flow-engine/requirements.txt b/services/flow-engine/requirements.txt new file mode 100644 index 0000000..f0a5bb5 --- /dev/null +++ b/services/flow-engine/requirements.txt @@ -0,0 +1,8 @@ +fastapi==0.115.6 +uvicorn[standard]==0.34.0 +sqlalchemy==2.0.36 +psycopg2-binary==2.9.10 +redis==5.2.1 +httpx==0.28.1 +pydantic==2.10.4 +pydantic-settings==2.7.1