File size: 3,377 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
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' });
}
}
}
|