|
1 | 1 | import { Express } from 'express'; |
2 | 2 | import passport from 'passport'; |
3 | 3 | import { IntraUser, PrismaClient } from '@prisma/client'; |
4 | | -import { getCoalitionScore, getBlocAtDate, scoreSumsToRanking, getCoalitionTopContributors, SMALL_CONTRIBUTION_TYPES } from '../utils'; |
| 4 | +import { getCoalitionScore, getBlocAtDate, scoreSumsToRanking, getCoalitionTopContributors, SMALL_CONTRIBUTION_TYPES, getPageNav, getPageNumber } from '../utils'; |
5 | 5 | import { ASSISTANT_GROUP_ID, ASSISTANTS_CAN_QUIZ } from '../env'; |
6 | 6 |
|
7 | 7 | export const setupCoalitionRoutes = function(app: Express, prisma: PrismaClient): void { |
@@ -117,29 +117,6 @@ export const setupCoalitionRoutes = function(app: Express, prisma: PrismaClient) |
117 | 117 | }); |
118 | 118 | const topContributorsWeek = await scoreSumsToRanking(prisma, topScoresWeek, 'Top contributors of the past 7 days'); |
119 | 119 |
|
120 | | - const latestScores = await prisma.codamCoalitionScore.findMany({ |
121 | | - where: { |
122 | | - coalition_id: coalition.id, |
123 | | - }, |
124 | | - orderBy: { |
125 | | - created_at: 'desc', |
126 | | - }, |
127 | | - include: { |
128 | | - user: { |
129 | | - select: { |
130 | | - intra_user: { |
131 | | - select: { |
132 | | - login: true, |
133 | | - usual_full_name: true, |
134 | | - image: true, |
135 | | - }, |
136 | | - }, |
137 | | - }, |
138 | | - }, |
139 | | - }, |
140 | | - take: 50, |
141 | | - }); |
142 | | - |
143 | 120 | const latestBigScores = await prisma.codamCoalitionScore.findMany({ |
144 | 121 | where: { |
145 | 122 | coalition_id: coalition.id, |
@@ -184,7 +161,6 @@ export const setupCoalitionRoutes = function(app: Express, prisma: PrismaClient) |
184 | 161 |
|
185 | 162 | viewOptions['topContributors'] = topContributors; |
186 | 163 | viewOptions['topContributorsWeek'] = topContributorsWeek; |
187 | | - viewOptions['latestScores'] = latestScores; |
188 | 164 | viewOptions['latestBigScores'] = latestBigScores; |
189 | 165 | viewOptions['coalitionScore'] = coalitionScore; |
190 | 166 | } |
@@ -226,4 +202,80 @@ export const setupCoalitionRoutes = function(app: Express, prisma: PrismaClient) |
226 | 202 | coalitionColored: false, |
227 | 203 | }); |
228 | 204 | }); |
| 205 | + |
| 206 | + app.get('/coalitions/:coalitionId/scores', passport.authenticate('session', { |
| 207 | + keepSessionInfo: true, |
| 208 | + }), async (req, res) => { |
| 209 | + const coalitionId = parseInt(req.params.coalitionId); |
| 210 | + if (!coalitionId || isNaN(coalitionId) || coalitionId <= 0) { |
| 211 | + return res.status(400).send('Invalid coalition ID'); |
| 212 | + } |
| 213 | + |
| 214 | + const coalition = await prisma.codamCoalition.findFirst({ |
| 215 | + where: { |
| 216 | + id: parseInt(req.params.coalitionId), |
| 217 | + }, |
| 218 | + include: { |
| 219 | + intra_coalition: true, |
| 220 | + }, |
| 221 | + }); |
| 222 | + if (!coalition) { |
| 223 | + return res.status(404).send('Coalition not found'); |
| 224 | + } |
| 225 | + |
| 226 | + const currentBloc = await getBlocAtDate(prisma); |
| 227 | + if (!currentBloc) { |
| 228 | + return res.status(400).send('No season currently ongoing'); |
| 229 | + } |
| 230 | + const itemsPerPage = 100; |
| 231 | + const totalPages = await prisma.codamCoalitionScore.count({ |
| 232 | + where: { |
| 233 | + coalition_id: coalition.id, |
| 234 | + created_at: { |
| 235 | + gte: currentBloc.begin_at, |
| 236 | + lt: currentBloc.end_at, |
| 237 | + }, |
| 238 | + }, |
| 239 | + }).then(count => Math.ceil(count / itemsPerPage)); |
| 240 | + const pageNum = getPageNumber(req, totalPages); |
| 241 | + const scores = await prisma.codamCoalitionScore.findMany({ |
| 242 | + where: { |
| 243 | + coalition_id: coalition.id, |
| 244 | + created_at: { |
| 245 | + gte: currentBloc.begin_at, |
| 246 | + lt: currentBloc.end_at, |
| 247 | + }, |
| 248 | + }, |
| 249 | + orderBy: { |
| 250 | + created_at: 'desc', |
| 251 | + }, |
| 252 | + include: { |
| 253 | + coalition: { |
| 254 | + select: { |
| 255 | + intra_coalition: { |
| 256 | + select: { |
| 257 | + name: true, |
| 258 | + color: true, |
| 259 | + }, |
| 260 | + } |
| 261 | + } |
| 262 | + }, |
| 263 | + }, |
| 264 | + take: itemsPerPage, |
| 265 | + skip: (pageNum - 1) * itemsPerPage, |
| 266 | + }); |
| 267 | + |
| 268 | + // Create a list of pages for the pagination nav |
| 269 | + const pageNav = getPageNav(pageNum, totalPages); |
| 270 | + |
| 271 | + return res.render('history.njk', { |
| 272 | + historyTitle: `Score History for ${coalition.intra_coalition.name} coalition in the current season`, |
| 273 | + scores, |
| 274 | + pageNum, |
| 275 | + totalPages, |
| 276 | + pageNav, |
| 277 | + coalitionColored: false, |
| 278 | + multipleUsers: true, |
| 279 | + }); |
| 280 | + }); |
229 | 281 | }; |
0 commit comments