105 lines
4.6 KiB
Python
105 lines
4.6 KiB
Python
|
from datetime import datetime
|
|||
|
|
|||
|
import aiohttp # Для выполнения HTTP-запросов
|
|||
|
from loguru import logger
|
|||
|
|
|||
|
|
|||
|
async def get_user_teams(token):
|
|||
|
url = "https://api.otask.ru/api/v1/teams"
|
|||
|
headers = {
|
|||
|
"Authorization": f"Bearer {token}",
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Accept": "application/json"
|
|||
|
}
|
|||
|
|
|||
|
async with aiohttp.ClientSession() as session:
|
|||
|
async with session.get(url, headers=headers) as response:
|
|||
|
if response.status == 200:
|
|||
|
teams = await response.json()
|
|||
|
# logger.info(teams)
|
|||
|
slugs = []
|
|||
|
for group in teams['data']['teams']:
|
|||
|
# logger.info(group)
|
|||
|
# logger.info(group['slug'])
|
|||
|
slugs.append(group['slug'])
|
|||
|
logger.info(slugs)
|
|||
|
return slugs
|
|||
|
else:
|
|||
|
print(f"Failed to get teams. Status code: {response.status}")
|
|||
|
return None
|
|||
|
|
|||
|
|
|||
|
async def get_client_name(token, ws_slug, client_id):
|
|||
|
"""Получаем имя клиента по его идентификатору (client_id)."""
|
|||
|
|
|||
|
url = f"https://api.otask.ru/api/v1/ws/{ws_slug}/clients"
|
|||
|
headers = {
|
|||
|
"Authorization": f"Bearer {token}",
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Accept": "application/json",
|
|||
|
}
|
|||
|
|
|||
|
async with aiohttp.ClientSession() as session:
|
|||
|
async with session.get(url, headers=headers) as response:
|
|||
|
if response.status == 200:
|
|||
|
data = await response.json()
|
|||
|
clients = data.get('data', {}).get('clients', [])
|
|||
|
|
|||
|
# Ищем клиента по client_id в списке клиентов
|
|||
|
for client in clients:
|
|||
|
if client.get('id') == client_id:
|
|||
|
return client
|
|||
|
else:
|
|||
|
logger.error(f"Failed to get client info for workspace {ws_slug}. Status code: {response.status}")
|
|||
|
|
|||
|
return None
|
|||
|
|
|||
|
|
|||
|
async def get_regular_projects(token):
|
|||
|
slugs = await get_user_teams(token) # Получаем все слуги команд пользователя
|
|||
|
regular_projects = [] # Список для хранения регулярных проектов
|
|||
|
|
|||
|
for slug in slugs:
|
|||
|
url = f"https://api.otask.ru/api/v1/ws/{slug}/panel/projects"
|
|||
|
headers = {
|
|||
|
"Authorization": f"Bearer {token}",
|
|||
|
"Content-Type": "application/json",
|
|||
|
"Accept": "application/json",
|
|||
|
}
|
|||
|
|
|||
|
params = {
|
|||
|
"is_regular": "true"
|
|||
|
}
|
|||
|
|
|||
|
async with aiohttp.ClientSession() as session:
|
|||
|
async with session.get(url, headers=headers, params=params) as response:
|
|||
|
if response.status == 200:
|
|||
|
data = await response.json() # Получаем все проекты
|
|||
|
# Извлекаем проекты из ответа
|
|||
|
projects = data.get('data', {}).get('projects', []).get('data', [])
|
|||
|
for p in projects:
|
|||
|
if p['end_at'] is not None:
|
|||
|
end_at = datetime.fromisoformat(p['end_at'].replace("Z", "+00:00"))
|
|||
|
difference_in_days = end_at.date().day - datetime.utcnow().date().day
|
|||
|
if difference_in_days == 0: # Наступила дата платежа
|
|||
|
project_info = {
|
|||
|
'name': p.get('name', 'Неизвестный проект'),
|
|||
|
'amount': p.get('amount', 'Неизвестная сумма')}
|
|||
|
|
|||
|
client_id = p.get('client_id','')
|
|||
|
if client_id:
|
|||
|
client = await get_client_name(token, slug, client_id)
|
|||
|
|
|||
|
project_info['first_name'] = client.get('first_name', 'Неизвестный клиент')
|
|||
|
project_info["description"] = ''
|
|||
|
if client['description']:
|
|||
|
project_info["description"] = client["description"]
|
|||
|
regular_projects.append(project_info)
|
|||
|
else:
|
|||
|
pass # логика для регулярного проекта в котором нет даты платежа
|
|||
|
# Фильтруем регулярные проекты
|
|||
|
else:
|
|||
|
logger.error(f"Failed to get projects for workspace {slug}. Status code: {response.status}")
|
|||
|
|
|||
|
return regular_projects
|