""" API Routes for Odoo Integration. """ from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app.core.database import get_db from app.services.odoo_service import odoo_service router = APIRouter() @router.get("/status") async def get_odoo_status(db: Session = Depends(get_db)): """ Get Odoo connection status. """ status = await odoo_service.test_connection() return status @router.post("/sync/products") async def sync_products( limit: int = 100, db: Session = Depends(get_db) ): """ Sync products from Odoo to local database. - **limit**: Maximum number of products to sync """ result = await odoo_service.sync_products(limit=limit) if not result.get("success"): raise HTTPException( status_code=500, detail=result.get("error", "Sync failed") ) return { "message": "Products synced successfully", **result } @router.post("/sync/services") async def sync_services( limit: int = 100, db: Session = Depends(get_db) ): """ Sync services from Odoo to local database. - **limit**: Maximum number of services to sync """ result = await odoo_service.sync_services(limit=limit) if not result.get("success"): raise HTTPException( status_code=500, detail=result.get("error", "Sync failed") ) return { "message": "Services synced successfully", **result } @router.post("/sync/leads") async def export_leads(db: Session = Depends(get_db)): """ Export unsynced leads to Odoo CRM. """ result = await odoo_service.export_leads_to_odoo() if not result.get("success"): raise HTTPException( status_code=500, detail=result.get("error", "Export failed") ) return { "message": "Leads exported successfully", **result } @router.get("/sync/logs") async def get_sync_logs( limit: int = 20, db: Session = Depends(get_db) ): """ Get Odoo sync history. - **limit**: Maximum number of logs to return """ logs = await odoo_service.get_sync_logs(limit=limit) return {"logs": logs, "count": len(logs)} @router.get("/sales") async def get_sales_summary( days: int = 30, db: Session = Depends(get_db) ): """ Get sales summary from Odoo. - **days**: Number of days to look back """ result = await odoo_service.get_sales_summary(days=days) if not result.get("success"): raise HTTPException( status_code=500, detail=result.get("error", "Failed to get sales data") ) return result