From aebca9c5223eab6cfa9b05dba7b3485ac5af7927 Mon Sep 17 00:00:00 2001 From: Untone Date: Sun, 31 Aug 2025 22:29:40 +0300 Subject: [PATCH] author-stats-fix --- resolvers/author.py | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 5999c3df..8369913c 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -64,6 +64,7 @@ class AuthorsBy(TypedDict, total=False): order: str | None after: int | None stat: str | None + id: int | None # Добавляем поле id для фильтрации по ID # Вспомогательная функция для получения всех авторов без статистики @@ -753,21 +754,36 @@ async def get_author( author_dict["stat"] = cached_author["stat"] if not author_dict or not author_dict.get("stat"): - # update stat from db - author_query = select(Author).where(Author.id == author_id) - result = get_with_stat(author_query) - if result: - author_with_stat = result[0] - if isinstance(author_with_stat, Author): - # Кэшируем полные данные для админов - original_dict = author_with_stat.dict() - _t = asyncio.create_task(cache_author(original_dict)) + # update stat from db using new comprehensive stats + try: + # Используем новый резолвер для получения полной статистики + filter_by: AuthorsBy = {} + if slug: + filter_by["slug"] = slug + elif author_id: + filter_by["id"] = author_id # author_id уже int - # Возвращаем отфильтрованную версию - author_dict = author_with_stat.dict(is_admin) - # Добавляем статистику - if hasattr(author_with_stat, "stat"): - author_dict["stat"] = author_with_stat.stat + authors_with_stats = await get_authors_with_stats(limit=1, offset=0, by=filter_by) + if authors_with_stats and len(authors_with_stats) > 0: + author_dict = authors_with_stats[0] + # Фильтруем данные согласно правам доступа + if not is_admin: + # Убираем защищенные поля для не-админов + protected_fields = ["email", "phone"] + for field in protected_fields: + author_dict.pop(field, None) + # Кэшируем полные данные для админов + _t = asyncio.create_task(cache_author(author_dict)) + except Exception as e: + logger.error(f"Error getting author stats: {e}") + # Fallback to basic author data without stats + with local_session() as session: + if slug: + author = session.query(Author).filter_by(slug=slug).first() + else: + author = session.query(Author).filter_by(id=author_id).first() + if author: + author_dict = author.dict(is_admin) except ValueError: pass except Exception as exc: