|
1 | 1 | import { PrismaClient } from '@prisma/client'; |
2 | 2 | import { Express } from 'express'; |
3 | | -import { getEndedSeasons, getSeasonResults } from '../utils'; |
| 3 | +import { getEndedSeasons, getSeasonResults, RANKING_MAX } from '../utils'; |
4 | 4 |
|
5 | 5 | export const setupResultsRoutes = function(app: Express, prisma: PrismaClient): void { |
6 | 6 | app.get('/results', async (req, res) => { |
@@ -29,4 +29,55 @@ export const setupResultsRoutes = function(app: Express, prisma: PrismaClient): |
29 | 29 | rankings, |
30 | 30 | }); |
31 | 31 | }); |
| 32 | + |
| 33 | + app.get('/results/:bloc_deadline_id/coalitions/:coalition_slug.csv', async (req, res) => { |
| 34 | + const endedSeasons = await getEndedSeasons(prisma); |
| 35 | + const season = endedSeasons.find(season => season.id === parseInt(req.params.bloc_deadline_id, 10)); |
| 36 | + if (!season) { |
| 37 | + return res.status(404).send('Season not found or has not ended yet.'); |
| 38 | + } |
| 39 | + |
| 40 | + const { seasonResults } = await getSeasonResults(prisma, season.id, RANKING_MAX); |
| 41 | + const seasonResult = seasonResults.find(result => result.coalition.intra_coalition.slug === req.params.coalition_slug); |
| 42 | + if (!seasonResult) { |
| 43 | + return res.status(404).send('Coalition not found for this season.'); |
| 44 | + } |
| 45 | + |
| 46 | + // Generate CSV |
| 47 | + let csv = 'Coalition,Rank,User,Score\n'; |
| 48 | + seasonResult.scores.forEach((entry, index) => { |
| 49 | + csv += `"${seasonResult.coalition.intra_coalition.name}",${entry.coalition_rank},"${entry.user.intra_user.login}",${entry.score}\n`; |
| 50 | + }); |
| 51 | + |
| 52 | + res.setHeader('Content-Disposition', `attachment; filename="${seasonResult.coalition.intra_coalition.slug}-leaderboard-season-${season.id}.csv"`); |
| 53 | + res.setHeader('Content-Type', 'text/csv'); |
| 54 | + return res.send(csv); |
| 55 | + }); |
| 56 | + |
| 57 | + app.get('/results/:bloc_deadline_id/rankings/:ranking_type.csv', async (req, res) => { |
| 58 | + const endedSeasons = await getEndedSeasons(prisma); |
| 59 | + const season = endedSeasons.find(season => season.id === parseInt(req.params.bloc_deadline_id, 10)); |
| 60 | + if (!season) { |
| 61 | + return res.status(404).send('Season not found or has not ended yet.'); |
| 62 | + } |
| 63 | + |
| 64 | + const { seasonResults, rankings } = await getSeasonResults(prisma, season.id, RANKING_MAX); |
| 65 | + const ranking = rankings.find(r => r.type === req.params.ranking_type); |
| 66 | + if (!ranking) { |
| 67 | + return res.status(404).send('Ranking not found for this season.'); |
| 68 | + } |
| 69 | + |
| 70 | + // Generate CSV |
| 71 | + let csv = 'Ranking,Coalition,Rank,User,Score\n'; |
| 72 | + ranking.results.forEach((entry) => { |
| 73 | + const userCoalition = seasonResults.find(sr => sr.coalition.id === entry.coalition_id); |
| 74 | + const coalitionName = userCoalition ? userCoalition.coalition.intra_coalition.name : 'N/A'; |
| 75 | + csv += `"${ranking.name}","${coalitionName}",${entry.rank},"${entry.user.intra_user.login}",${entry.score}\n`; |
| 76 | + }); |
| 77 | + |
| 78 | + |
| 79 | + res.setHeader('Content-Disposition', `attachment; filename="ranking-${ranking.type}-season-${season.id}.csv"`); |
| 80 | + res.setHeader('Content-Type', 'text/csv'); |
| 81 | + return res.send(csv); |
| 82 | + }); |
32 | 83 | }; |
0 commit comments