Refactor: Improve error handling in authentication flow

This commit is contained in:
Your Name
2026-04-17 19:55:35 +00:00
parent 7c5da339cb
commit 61bbca2e5d
33 changed files with 2834 additions and 4185 deletions
+8
View File
@@ -3,6 +3,7 @@ import { authenticate, AuthenticatedRequest, requireAdmin } from '../middleware/
import { prisma } from '../utils/prisma';
import { asyncHandler } from '../middleware/errorHandler';
import { mintTokens } from '../services/solana';
import { io } from '../index';
const router = Router();
@@ -227,6 +228,13 @@ router.post('/users/:id/bonus',
}
});
// Emit real-time update via WebSocket
io.to(`user:${user.id}`).emit('bonus_received', {
amount,
reason: reason || 'Admin Bonus',
transaction
});
res.json({
success: true,
amount,
+16 -33
View File
@@ -165,45 +165,28 @@ router.get('/admin/all',
})
);
// Admin: Get system-wide stats
// Get system-wide stats
router.get('/admin/stats',
// ... (keeping existing code)
);
// Get recent global activity
router.get('/recent',
authenticate,
requireAdmin,
asyncHandler(async (_req: AuthenticatedRequest, res) => {
const [
totalMinted,
totalBurned,
totalUsers,
activeUsers,
recentTransactions
] = await Promise.all([
prisma.transaction.aggregate({
where: { type: 'EARN' },
_sum: { amount: true }
}),
prisma.transaction.aggregate({
where: { type: 'SPEND' },
_sum: { amount: true }
}),
prisma.user.count(),
prisma.user.count({ where: { walletAddress: { not: null } } }),
prisma.transaction.count({
where: {
createdAt: {
gte: new Date(Date.now() - 24 * 60 * 60 * 1000)
const transactions = await prisma.transaction.findMany({
orderBy: { createdAt: 'desc' },
take: 10,
include: {
user: {
select: {
plexUsername: true
}
}
})
]);
res.json({
totalMinted: totalMinted._sum.amount || 0,
totalBurned: totalBurned._sum.amount || 0,
netSupply: (totalMinted._sum.amount || 0) - (totalBurned._sum.amount || 0),
totalUsers,
activeUsers,
recentTransactions
}
});
res.json({ transactions });
})
);
+30
View File
@@ -132,4 +132,34 @@ router.get('/me/requests',
})
);
// Get leaderboard
router.get('/leaderboard',
authenticate,
asyncHandler(async (_req: AuthenticatedRequest, res) => {
const topEarners = await prisma.user.findMany({
orderBy: { totalEarned: 'desc' },
take: 10,
select: {
id: true,
plexUsername: true,
totalEarned: true,
watchTimeMinutes: true
}
});
const topWatchers = await prisma.user.findMany({
orderBy: { watchTimeMinutes: 'desc' },
take: 10,
select: {
id: true,
plexUsername: true,
totalEarned: true,
watchTimeMinutes: true
}
});
res.json({ topEarners, topWatchers });
})
);
export { router as userRouter };