crm-backend/apps/reports/services/data_sources.py

46 lines
1.1 KiB
Python

import logging
from utils.accounts_client import get_user_detailed_info
from utils.advertising_client import get_crm_application_tickets
logger = logging.getLogger(__name__)
class ReportDataSources:
def fetch_application_tickets(self):
tickets = []
offset = 0
limit = 1000
while True:
response = get_crm_application_tickets(limit=limit, offset=offset)
if not response or not response.results:
break
tickets.extend(response.results)
if response.next_ is None:
break
offset += limit
return tickets
def fetch_users_for_tickets(self, tickets):
users = []
seen = set()
for ticket in tickets:
user_id = str(ticket.user)
if user_id in seen:
continue
seen.add(user_id)
try:
user = get_user_detailed_info(user_id)
except Exception:
logger.exception("Failed to fetch user %s", user_id)
continue
if user is not None:
users.append(user)
return users