|
| 1 | +import { Express } from 'express'; |
| 2 | +import passport from 'passport'; |
| 3 | +import { IntraUser, PrismaClient } from '@prisma/client'; |
| 4 | +import { ExpressIntraUser } from '../sync/oauth'; |
| 5 | +import { getCoalitionScore, getBlocAtDate, scoreSumsToRanking, getCoalitionTopContributors, SMALL_CONTRIBUTION_TYPES } from '../utils'; |
| 6 | +import { ASSISTANT_GROUP_ID, ASSISTANTS_CAN_QUIZ } from '../env'; |
| 7 | + |
| 8 | +export const setupCoalitionRoutes = function(app: Express, prisma: PrismaClient): void { |
| 9 | + app.get('/coalitions/:coalitionId', passport.authenticate('session', { |
| 10 | + keepSessionInfo: true, |
| 11 | + }), async (req, res) => { |
| 12 | + const coalitionId = parseInt(req.params.coalitionId); |
| 13 | + if (!coalitionId || isNaN(coalitionId) || coalitionId <= 0) { |
| 14 | + return res.status(400).send('Invalid coalition ID'); |
| 15 | + } |
| 16 | + const user = req.user as ExpressIntraUser; |
| 17 | + const now = new Date(); |
| 18 | + |
| 19 | + const coalition = await prisma.codamCoalition.findFirst({ |
| 20 | + where: { |
| 21 | + id: parseInt(req.params.coalitionId), |
| 22 | + }, |
| 23 | + include: { |
| 24 | + intra_coalition: true, |
| 25 | + }, |
| 26 | + }); |
| 27 | + if (!coalition) { |
| 28 | + return res.status(404).send('Coalition not found'); |
| 29 | + } |
| 30 | + |
| 31 | + const bloc = await getBlocAtDate(prisma, now); |
| 32 | + if (!bloc) { |
| 33 | + // No ongoing season, there's no point in viewing this page. Redirect to home. |
| 34 | + return res.redirect('/'); |
| 35 | + } |
| 36 | + |
| 37 | + // Get current coalition score |
| 38 | + const coalitionScore = await getCoalitionScore(prisma, coalition.id); |
| 39 | + |
| 40 | + // Get the top 25 contributors of this season |
| 41 | + const topContributors = await getCoalitionTopContributors(prisma, coalition.id, 'Top contributors of this season', now, 25); |
| 42 | + |
| 43 | + // Get the top contributors of the past 7 days |
| 44 | + const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); |
| 45 | + if (sevenDaysAgo < bloc.begin_at) { |
| 46 | + // If the season started less than 7 days ago, we can't get the top contributors of the past 7 days. |
| 47 | + // Get the top contributors since the season's start instead. |
| 48 | + sevenDaysAgo.setTime(bloc.begin_at.getTime()); |
| 49 | + console.log('Season started less than 7 days ago, getting top contributors since season start instead'); |
| 50 | + } |
| 51 | + const topScoresWeek = await prisma.codamCoalitionScore.groupBy({ |
| 52 | + by: ['user_id'], |
| 53 | + _sum: { |
| 54 | + amount: true, |
| 55 | + }, |
| 56 | + orderBy: { |
| 57 | + _sum: { |
| 58 | + amount: 'desc', |
| 59 | + }, |
| 60 | + }, |
| 61 | + where: { |
| 62 | + coalition_id: coalition.id, |
| 63 | + created_at: { |
| 64 | + gte: sevenDaysAgo, |
| 65 | + lte: now, |
| 66 | + }, |
| 67 | + }, |
| 68 | + take: 10, |
| 69 | + }); |
| 70 | + const topContributorsWeek = await scoreSumsToRanking(prisma, topScoresWeek, 'Top contributors of the past 7 days'); |
| 71 | + |
| 72 | + const latestScores = await prisma.codamCoalitionScore.findMany({ |
| 73 | + where: { |
| 74 | + coalition_id: coalition.id, |
| 75 | + }, |
| 76 | + orderBy: { |
| 77 | + created_at: 'desc', |
| 78 | + }, |
| 79 | + include: { |
| 80 | + user: { |
| 81 | + select: { |
| 82 | + intra_user: { |
| 83 | + select: { |
| 84 | + login: true, |
| 85 | + usual_full_name: true, |
| 86 | + image: true, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + take: 50, |
| 93 | + }); |
| 94 | + |
| 95 | + const latestBigScores = await prisma.codamCoalitionScore.findMany({ |
| 96 | + where: { |
| 97 | + coalition_id: coalition.id, |
| 98 | + OR: [ |
| 99 | + { |
| 100 | + NOT: { |
| 101 | + fixed_type_id: { |
| 102 | + in: SMALL_CONTRIBUTION_TYPES, // Exclude usually low individual scores |
| 103 | + } |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + fixed_type_id: null, // Do include scores that are not fixed types |
| 108 | + } |
| 109 | + ], |
| 110 | + amount: { |
| 111 | + gt: 0, |
| 112 | + }, |
| 113 | + created_at: { |
| 114 | + gte: sevenDaysAgo, |
| 115 | + lte: now, |
| 116 | + }, |
| 117 | + }, |
| 118 | + orderBy: { |
| 119 | + created_at: 'desc', |
| 120 | + }, |
| 121 | + include: { |
| 122 | + user: { |
| 123 | + select: { |
| 124 | + intra_user: { |
| 125 | + select: { |
| 126 | + login: true, |
| 127 | + usual_full_name: true, |
| 128 | + image: true, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + take: 25, |
| 135 | + }); |
| 136 | + |
| 137 | + // Get the staff part of this coalition |
| 138 | + const staff = await prisma.intraUser.findMany({ |
| 139 | + where: { |
| 140 | + kind: 'admin', |
| 141 | + coalition_users: { |
| 142 | + some: { |
| 143 | + coalition_id: coalition.id, |
| 144 | + }, |
| 145 | + }, |
| 146 | + cursus_users: { |
| 147 | + some: { |
| 148 | + end_at: null, // Make sure to only get active staff members |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + // Get the assistants part of this coalition |
| 155 | + let assistants: IntraUser[] = []; |
| 156 | + let assistantGroup = null; |
| 157 | + if (ASSISTANT_GROUP_ID && ASSISTANTS_CAN_QUIZ) { |
| 158 | + assistantGroup = await prisma.intraGroup.findUnique({ |
| 159 | + where: { |
| 160 | + id: ASSISTANT_GROUP_ID, |
| 161 | + }, |
| 162 | + }); |
| 163 | + assistants = await prisma.intraUser.findMany({ |
| 164 | + where: { |
| 165 | + group_users: { |
| 166 | + some: { |
| 167 | + group_id: ASSISTANT_GROUP_ID, |
| 168 | + }, |
| 169 | + }, |
| 170 | + coalition_users: { |
| 171 | + some: { |
| 172 | + coalition_id: coalition.id, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + return res.render('coalition.njk', { |
| 180 | + coalition, |
| 181 | + topContributors, |
| 182 | + topContributorsWeek, |
| 183 | + latestScores, |
| 184 | + latestBigScores, |
| 185 | + coalitionScore, |
| 186 | + staff, |
| 187 | + assistantGroup, |
| 188 | + assistantsCanQuiz: ASSISTANTS_CAN_QUIZ, |
| 189 | + assistants, |
| 190 | + }); |
| 191 | + }); |
| 192 | +}; |
0 commit comments