| import { Request, Response } from 'express'; | |
| import { AppDataSource } from '../config/database'; | |
| import { Agent } from '../entities/Agent'; | |
| import { Transaction, TransactionType } from '../entities/Transaction'; | |
| const agentRepository = AppDataSource.getRepository(Agent); | |
| const txRepository = AppDataSource.getRepository(Transaction); | |
| export class CreatorController { | |
| async overview(req: Request, res: Response) { | |
| try { | |
| const userId = (req as any).user.id; | |
| const { agentId, from, to } = req.query as any; | |
| const agents = await agentRepository.find({ where: { creatorId: userId } }); | |
| const agentIds = agents.map((a) => a.id); | |
| let countQb = txRepository | |
| .createQueryBuilder('t') | |
| .where('t.type = :type', { type: TransactionType.CHARGE }) | |
| .andWhere('t.agentId IN (:...agentIds)', { agentIds: agentIds.length ? agentIds : [''] }); | |
| if (agentId) countQb = countQb.andWhere('t.agentId = :agentId', { agentId }); | |
| if (from) countQb = countQb.andWhere('t.createdAt >= :from', { from }); | |
| if (to) countQb = countQb.andWhere('t.createdAt <= :to', { to }); | |
| const totalTasks = await countQb.getCount(); | |
| let sumQb = txRepository | |
| .createQueryBuilder('t') | |
| .where('t.type = :type', { type: TransactionType.CHARGE }) | |
| .andWhere('t.agentId IN (:...agentIds)', { agentIds: agentIds.length ? agentIds : [''] }) | |
| .select('SUM(ABS(t.amount))', 'sum'); | |
| if (agentId) sumQb = sumQb.andWhere('t.agentId = :agentId', { agentId }); | |
| if (from) sumQb = sumQb.andWhere('t.createdAt >= :from', { from }); | |
| if (to) sumQb = sumQb.andWhere('t.createdAt <= :to', { to }); | |
| const totalPoints = await sumQb.getRawOne(); | |
| res.json({ | |
| agents: agents.map((a) => ({ id: a.id, name: a.name, pointsPerTask: Number(a.pointsPerTask || 0) })), | |
| totals: { | |
| agents: agents.length, | |
| tasks: totalTasks, | |
| pointsEarned: Number(totalPoints?.sum || 0), | |
| }, | |
| }); | |
| } catch (error) { | |
| console.error('Creator overview error:', error); | |
| res.status(500).json({ error: 'Failed to get overview' }); | |
| } | |
| } | |
| async earnings(req: Request, res: Response) { | |
| try { | |
| const userId = (req as any).user.id; | |
| const { from, to, agentId } = req.query as any; | |
| const agents = await agentRepository.find({ where: { creatorId: userId } }); | |
| const agentIds = agents.map((a) => a.id); | |
| let qb = txRepository | |
| .createQueryBuilder('t') | |
| .where('t.agentId IN (:...agentIds)', { agentIds: agentIds.length ? agentIds : [''] }) | |
| .andWhere('t.type = :type', { type: TransactionType.CHARGE }) | |
| .select("DATE_TRUNC('day', t.createdAt)", 'day') | |
| .addSelect('SUM(ABS(t.amount))', 'points') | |
| .groupBy("DATE_TRUNC('day', t.createdAt)") | |
| .orderBy('day', 'ASC'); | |
| if (agentId) qb = qb.andWhere('t.agentId = :agentId', { agentId }); | |
| if (from) qb = qb.andWhere('t.createdAt >= :from', { from }); | |
| if (to) qb = qb.andWhere('t.createdAt <= :to', { to }); | |
| const rows = await qb.getRawMany(); | |
| res.json({ | |
| series: rows.map((r) => ({ day: r.day, points: Number(r.points) })), | |
| }); | |
| } catch (error) { | |
| console.error('Creator earnings error:', error); | |
| res.status(500).json({ error: 'Failed to get earnings' }); | |
| } | |
| } | |
| } | |