Telezab/app/services/users_event_service.py
UdoChudo 52e31864b3 feat: Develop web interface
- Implemented the initial version of the web interface.
refactor: Begin Telegram bot refactoring
- Started restructuring the bot’s code for better maintainability.
chore: Migrate to Flask project structure
- Reorganized the application to follow Flask's project structure.
cleanup: Extensive code cleanup
- Removed redundant code and improved readability.

Signed-off-by: UdoChudo <stream@udochudo.ru>
2025-06-10 14:39:11 +05:00

37 lines
1.8 KiB
Python
Raw Permalink 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.

# app/services/user_event_service.py
from typing import Dict, Any, Tuple
from app.extensions.db import db
from app.models import UserEvents, Users # Импортируем модель Users для получения telegram_id
def log_user_action(chat_id: int, action: str) -> Tuple[Dict[str, str], int]:
try:
# Получаем telegram_id пользователя по chat_id
user = Users.query.filter_by(chat_id=chat_id).first()
if not user:
return {'error': f'Пользователь с chat_id {chat_id} не найден для логирования действия.'}, 404
new_event = UserEvents(
chat_id=chat_id,
telegram_id=user.telegram_id, # Добавляем telegram_id из найденного пользователя
action=action,
# timestamp генерируется автоматически по default=db.func.current_timestamp()
)
db.session.add(new_event)
db.session.commit()
return {'message': 'Действие сохранено'}, 200
except Exception as e:
db.session.rollback()
return {'error': str(e)}, 500
def get_user_events(chat_id: int) -> tuple[list[dict[str, Any | None]], int]:
# Предполагаем, что у UserEvents есть поля 'id' и 'timestamp'
events = UserEvents.query.filter_by(chat_id=chat_id).order_by(UserEvents.timestamp.desc()).all()
events_list = [{
'event_id': e.id,
'chat_id': e.chat_id,
'event_type': e.action, # Используем 'action', как в модели UserEvents
'timestamp': e.timestamp.isoformat() if e.timestamp else None # Форматируем дату
} for e in events]
return events_list, 200