| import { Request, Response } from 'express'; |
| import { AppDataSource } from '../config/database'; |
| import { User } from '../entities/User'; |
| import { Agent, AgentStatus } from '../entities/Agent'; |
| import { ChatMessage } from '../entities/ChatMessage'; |
| import { Transaction } from '../entities/Transaction'; |
|
|
| const userRepository = AppDataSource.getRepository(User); |
| const agentRepository = AppDataSource.getRepository(Agent); |
| const messageRepository = AppDataSource.getRepository(ChatMessage); |
| const txRepository = AppDataSource.getRepository(Transaction); |
|
|
| export class AdminController { |
| async overview(req: Request, res: Response) { |
| try { |
| const { from, to } = req.query as any; |
|
|
| const [users, agents, messages] = await Promise.all([ |
| userRepository.count(), |
| agentRepository.count(), |
| messageRepository.count(), |
| ]); |
|
|
| const pendingAgents = await agentRepository.count({ where: { status: AgentStatus.PENDING } }); |
|
|
| let pvQb = txRepository |
| .createQueryBuilder('t') |
| .select('SUM(ABS(t.amount))', 'sum'); |
|
|
| if (from) pvQb = pvQb.where('t.createdAt >= :from', { from }); |
| if (to) pvQb = pvQb.andWhere('t.createdAt <= :to', { to }); |
|
|
| const totalPointsVolumeRow = await pvQb.getRawOne(); |
|
|
| res.json({ |
| totals: { |
| users, |
| agents, |
| messages, |
| pendingAgents, |
| pointsVolume: Number(totalPointsVolumeRow?.sum || 0), |
| }, |
| }); |
| } catch (error) { |
| console.error('Admin overview error:', error); |
| res.status(500).json({ error: 'Failed to get overview' }); |
| } |
| } |
| } |
|
|