feat: Проксирование CreateClaim через backend
- ✅ Новый endpoint: POST /api/n8n/claim/create - ✅ Проксирует запросы к n8n webhook создания заявки - ✅ Frontend теперь использует /api/n8n/claim/create вместо прямого URL - ✅ Решает проблему CORS и скрывает webhook URL - ✅ Логирование запросов и ошибок
This commit is contained in:
@@ -18,6 +18,7 @@ router = APIRouter(prefix="/api/n8n", tags=["n8n-proxy"])
|
||||
# URL webhooks из .env (будут добавлены)
|
||||
N8N_POLICY_CHECK_WEBHOOK = getattr(settings, 'n8n_policy_check_webhook', None)
|
||||
N8N_FILE_UPLOAD_WEBHOOK = getattr(settings, 'n8n_file_upload_webhook', None)
|
||||
N8N_CREATE_CLAIM_WEBHOOK = getattr(settings, 'n8n_create_claim_webhook', 'https://n8n.clientright.pro/webhook/d5bf4ca6-9e44-44b9-9714-3186ea703e7d')
|
||||
|
||||
|
||||
@router.post("/policy/check")
|
||||
@@ -132,3 +133,46 @@ async def proxy_file_upload(
|
||||
logger.error(f"❌ Error proxying file to n8n: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"Ошибка загрузки файла: {str(e)}")
|
||||
|
||||
|
||||
@router.post("/claim/create")
|
||||
async def proxy_create_claim(request: Request):
|
||||
"""
|
||||
Проксирует создание черновика заявки к n8n webhook
|
||||
|
||||
Frontend отправляет: POST /api/n8n/claim/create
|
||||
Backend проксирует к: https://n8n.clientright.pro/webhook/d5bf4ca6-9e44-44b9-9714-3186ea703e7d
|
||||
"""
|
||||
if not N8N_CREATE_CLAIM_WEBHOOK:
|
||||
raise HTTPException(status_code=500, detail="N8N claim webhook не настроен")
|
||||
|
||||
try:
|
||||
# Получаем JSON body от фронтенда
|
||||
body = await request.json()
|
||||
|
||||
logger.info(f"🔄 Proxy create claim: event_type={body.get('event_type', 'unknown')}, claim_id={body.get('claim_id', 'unknown')}")
|
||||
|
||||
# Проксируем запрос к n8n
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
response = await client.post(
|
||||
N8N_CREATE_CLAIM_WEBHOOK,
|
||||
json=body,
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
logger.info(f"✅ Claim created successfully")
|
||||
return response.json()
|
||||
else:
|
||||
logger.error(f"❌ N8N returned {response.status_code}: {response.text}")
|
||||
raise HTTPException(
|
||||
status_code=response.status_code,
|
||||
detail=f"N8N error: {response.text}"
|
||||
)
|
||||
|
||||
except httpx.TimeoutException:
|
||||
logger.error("⏱️ N8N webhook timeout")
|
||||
raise HTTPException(status_code=504, detail="Таймаут подключения к n8n")
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Error proxying to n8n: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"Ошибка создания заявки: {str(e)}")
|
||||
|
||||
|
||||
@@ -39,8 +39,8 @@ const Step2EventType: React.FC<Props> = ({ formData, updateFormData, onNext, onP
|
||||
const eventLabel = EVENT_TYPES.find(e => e.value === values.eventType)?.label || values.eventType;
|
||||
const title = `${eventLabel} - ${formData.voucher || 'полис не указан'}`;
|
||||
|
||||
// Вызываем n8n webhook для создания черновика заявки
|
||||
const response = await fetch('https://n8n.clientright.pro/webhook/d5bf4ca6-9e44-44b9-9714-3186ea703e7d', {
|
||||
// Вызываем n8n webhook для создания черновика заявки (через backend proxy)
|
||||
const response = await fetch('/api/n8n/claim/create', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user