Skip to content

Commit 81fa5c5

Browse files
committed
fix: make it possible to view coalition pages without an ongoing season
1 parent 25886c3 commit 81fa5c5

File tree

2 files changed

+261
-255
lines changed

2 files changed

+261
-255
lines changed

src/routes/coalitions.ts

Lines changed: 114 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -28,112 +28,6 @@ export const setupCoalitionRoutes = function(app: Express, prisma: PrismaClient)
2828
return res.status(404).send('Coalition not found');
2929
}
3030

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-
13731
// Get the staff part of this coalition
13832
const staff = await prisma.intraUser.findMany({
13933
where: {
@@ -176,17 +70,124 @@ export const setupCoalitionRoutes = function(app: Express, prisma: PrismaClient)
17670
});
17771
}
17872

179-
return res.render('coalition.njk', {
73+
const currentBloc = await getBlocAtDate(prisma, now);
74+
const viewOptions: any = {
18075
coalition,
181-
topContributors,
182-
topContributorsWeek,
183-
latestScores,
184-
latestBigScores,
185-
coalitionScore,
76+
currentBloc,
18677
staff,
18778
assistantGroup,
18879
assistantsCanQuiz: ASSISTANTS_CAN_QUIZ,
18980
assistants,
190-
});
81+
};
82+
83+
if (currentBloc) {
84+
// Get current coalition score
85+
const coalitionScore = await getCoalitionScore(prisma, coalition.id);
86+
87+
// Get the top 25 contributors of this season
88+
const topContributors = await getCoalitionTopContributors(prisma, coalition.id, 'Top contributors of this season', now, 25);
89+
90+
// Get the top contributors of the past 7 days
91+
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
92+
if (sevenDaysAgo < currentBloc.begin_at) {
93+
// If the season started less than 7 days ago, we can't get the top contributors of the past 7 days.
94+
// Get the top contributors since the season's start instead.
95+
sevenDaysAgo.setTime(currentBloc.begin_at.getTime());
96+
console.log('Season started less than 7 days ago, getting top contributors since season start instead');
97+
}
98+
const topScoresWeek = await prisma.codamCoalitionScore.groupBy({
99+
by: ['user_id'],
100+
_sum: {
101+
amount: true,
102+
},
103+
orderBy: {
104+
_sum: {
105+
amount: 'desc',
106+
},
107+
},
108+
where: {
109+
coalition_id: coalition.id,
110+
created_at: {
111+
gte: sevenDaysAgo,
112+
lte: now,
113+
},
114+
},
115+
take: 10,
116+
});
117+
const topContributorsWeek = await scoreSumsToRanking(prisma, topScoresWeek, 'Top contributors of the past 7 days');
118+
119+
const latestScores = await prisma.codamCoalitionScore.findMany({
120+
where: {
121+
coalition_id: coalition.id,
122+
},
123+
orderBy: {
124+
created_at: 'desc',
125+
},
126+
include: {
127+
user: {
128+
select: {
129+
intra_user: {
130+
select: {
131+
login: true,
132+
usual_full_name: true,
133+
image: true,
134+
},
135+
},
136+
},
137+
},
138+
},
139+
take: 50,
140+
});
141+
142+
const latestBigScores = await prisma.codamCoalitionScore.findMany({
143+
where: {
144+
coalition_id: coalition.id,
145+
OR: [
146+
{
147+
NOT: {
148+
fixed_type_id: {
149+
in: SMALL_CONTRIBUTION_TYPES, // Exclude usually low individual scores
150+
}
151+
},
152+
},
153+
{
154+
fixed_type_id: null, // Do include scores that are not fixed types
155+
}
156+
],
157+
amount: {
158+
gt: 0,
159+
},
160+
created_at: {
161+
gte: sevenDaysAgo,
162+
lte: now,
163+
},
164+
},
165+
orderBy: {
166+
created_at: 'desc',
167+
},
168+
include: {
169+
user: {
170+
select: {
171+
intra_user: {
172+
select: {
173+
login: true,
174+
usual_full_name: true,
175+
image: true,
176+
},
177+
},
178+
},
179+
},
180+
},
181+
take: 25,
182+
});
183+
184+
viewOptions['topContributors'] = topContributors;
185+
viewOptions['topContributorsWeek'] = topContributorsWeek;
186+
viewOptions['latestScores'] = latestScores;
187+
viewOptions['latestBigScores'] = latestBigScores;
188+
viewOptions['coalitionScore'] = coalitionScore;
189+
}
190+
191+
return res.render('coalition.njk', viewOptions);
191192
});
192193
};

0 commit comments

Comments
 (0)