Files
crm.clientright.ru/ticket_form/backend/app/services/policy_service.py
Fedor de011efba9 fix: исправлен конфликт имён переменных в loadDraft (claimId -> finalClaimId)
- Исправлена ошибка ReferenceError при загрузке черновиков
- Переименована локальная переменная claimId в finalClaimId для избежания конфликта с параметром функции
- Обновлена логика извлечения claim_id из разных источников (claim.claim_id, payload.claim_id, body.claim_id, claim.id)
- Добавлен fallback на параметр claimId функции для надёжности
2025-11-19 23:33:52 +03:00

81 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Policy Service - Проверка полисов в MySQL БД
"""
import aiomysql
from typing import Optional, Dict, Any
from ..config import settings
import logging
logger = logging.getLogger(__name__)
class PolicyService:
"""Сервис для проверки полисов ERV"""
def __init__(self):
self.pool: Optional[aiomysql.Pool] = None
async def connect(self):
"""Подключение к MySQL БД с полисами"""
try:
# Используем credentials из .env через settings
self.pool = await aiomysql.create_pool(
host=settings.mysql_host,
port=settings.mysql_port,
user=settings.mysql_user,
password=settings.mysql_password,
db=settings.mysql_db,
autocommit=True,
minsize=1,
maxsize=5
)
logger.info(f"✅ MySQL Policy DB connected: {settings.mysql_host}/{settings.mysql_db}")
except Exception as e:
logger.error(f"❌ MySQL Policy DB connection error: {e}")
raise
async def check_policy(self, voucher: str) -> Optional[Dict[str, Any]]:
"""
Проверить полис в БД
Args:
voucher: Номер полиса вида E1000-302538524
Returns:
Dict с данными полиса или None если не найден
"""
if not self.pool:
await self.connect()
try:
async with self.pool.acquire() as conn:
async with conn.cursor(aiomysql.DictCursor) as cursor:
# Запрос поиска по номеру полиса в таблице lexrpiority
query = "SELECT * FROM lexrpiority WHERE voucher = %s LIMIT 1"
await cursor.execute(query, [voucher])
result = await cursor.fetchone()
if result:
logger.info(f"✅ Policy found: {voucher}")
return dict(result)
else:
logger.warning(f"⚠️ Policy not found: {voucher}")
return None
except Exception as e:
logger.error(f"Error checking policy: {e}")
return None
async def close(self):
"""Закрыть пул подключений"""
if self.pool:
self.pool.close()
await self.pool.wait_closed()
logger.info("MySQL Policy DB pool closed")
# Глобальный экземпляр
policy_service = PolicyService()