Files
2026-01-29 22:19:17 +00:00

93 lines
2.5 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from pydantic import BaseModel
from typing import Optional
import httpx
from app.core.database import get_db
from app.core.security import get_current_user
from app.core.config import get_settings
from app.models.user import User, UserRole
from app.models.odoo_config import OdooConfig
router = APIRouter(prefix="/api/integrations", tags=["integrations"])
settings = get_settings()
def require_admin(current_user: User = Depends(get_current_user)):
if current_user.role != UserRole.ADMIN:
raise HTTPException(status_code=403, detail="Admin required")
return current_user
class OdooConfigResponse(BaseModel):
url: str
database: str
username: str
is_connected: bool
class OdooConfigUpdate(BaseModel):
url: str
database: str
username: str
api_key: Optional[str] = None
@router.get("/odoo/config", response_model=OdooConfigResponse)
def get_odoo_config(
db: Session = Depends(get_db),
current_user: User = Depends(require_admin),
):
config = db.query(OdooConfig).filter(OdooConfig.is_active == True).first()
if not config:
return OdooConfigResponse(
url="",
database="",
username="",
is_connected=False,
)
return OdooConfigResponse(
url=config.url,
database=config.database,
username=config.username,
is_connected=config.api_key_encrypted is not None and config.api_key_encrypted != "",
)
@router.put("/odoo/config")
def update_odoo_config(
data: OdooConfigUpdate,
db: Session = Depends(get_db),
current_user: User = Depends(require_admin),
):
config = db.query(OdooConfig).filter(OdooConfig.is_active == True).first()
if not config:
config = OdooConfig()
db.add(config)
config.url = data.url
config.database = data.database
config.username = data.username
if data.api_key:
config.api_key_encrypted = data.api_key
db.commit()
return {"success": True}
@router.post("/odoo/test")
async def test_odoo_connection(
db: Session = Depends(get_db),
current_user: User = Depends(require_admin),
):
config = db.query(OdooConfig).filter(OdooConfig.is_active == True).first()
if not config or not config.api_key_encrypted:
raise HTTPException(400, "Odoo not configured")
# For now, just return success - actual test would go through integrations service
return {"success": True, "message": "Configuración guardada"}