/
fegerV
/
Stogram
Обзор
Документация
Войти
/
fegerV
/
Stogram
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
server/src/controllers/analyticsController.ts
115 строк
3 KB
engine-labs-app[bot]
feat(core): implement E2E encryption, extended media, bots, analytics, UI/UX, performance, and security for v2.0.0
01 ноя 2025, 19:09
01 ноя 2025, 19:09
d2da506
Код
Авторство
О чём код?
import { Request, Response } from 'express'; import { AnalyticsService } from '../services/analyticsService'; export class AnalyticsController { // Get user analytics static async getUserAnalytics(req: Request, res: Response): Promise<void> { try { const userId = (req as any).user.id; const days = parseInt(req.query.days as string) || 30; const analytics = await AnalyticsService.getUserAnalytics(userId, days); res.json({ analytics }); } catch (error) { console.error('Get user analytics error:', error); res.status(500).json({ error: 'Failed to get user analytics' }); } } // Get bot analytics static async getBotAnalytics(req: Request, res: Response): Promise<void> { try { const { botId } = req.params; const days = parseInt(req.query.days as string) || 30; // Verify bot ownership const prisma = (req as any).prisma; const userId = (req as any).user.id; const bot = await prisma.bot.findUnique({ where: { id: botId }, select: { ownerId: true }, }); if (!bot || bot.ownerId !== userId) { res.status(403).json({ error: 'Access denied' }); return; } const analytics = await AnalyticsService.getBotAnalytics(botId, days); res.json({ analytics }); } catch (error) { console.error('Get bot analytics error:', error); res.status(500).json({ error: 'Failed to get bot analytics' }); } } // Get system analytics (admin only) static async getSystemAnalytics(req: Request, res: Response): Promise<void> { try { // TODO: Add admin check const days = parseInt(req.query.days as string) || 30; const analytics = await AnalyticsService.getSystemAnalytics(days); res.json({ analytics }); } catch (error) { console.error('Get system analytics error:', error); res.status(500).json({ error: 'Failed to get system analytics' }); } } // Get dashboard statistics static async getDashboardStats(req: Request, res: Response): Promise<void> { try { const stats = await AnalyticsService.getDashboardStats(); res.json({ stats }); } catch (error) { console.error('Get dashboard stats error:', error); res.status(500).json({ error: 'Failed to get dashboard stats' }); } } // Get bot summary static async getBotSummary(req: Request, res: Response): Promise<void> { try { const { botId } = req.params; const prisma = (req as any).prisma; const userId = (req as any).user.id; const bot = await prisma.bot.findUnique({ where: { id: botId }, select: { id: true, username: true, displayName: true, messagesSent: true, messagesReceived: true, uniqueUsers: true, isActive: true, createdAt: true, ownerId: true, }, }); if (!bot || bot.ownerId !== userId) { res.status(403).json({ error: 'Access denied' }); return; } // Get recent analytics const recentAnalytics = await AnalyticsService.getBotAnalytics(botId, 7); res.json({ bot, recentAnalytics, }); } catch (error) { console.error('Get bot summary error:', error); res.status(500).json({ error: 'Failed to get bot summary' }); } } }