File size: 1,621 Bytes
2fe81ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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' });
    }
  }
}