Skip to content

Commit 0630b8b

Browse files
committed
fix: combine discovery piscines of the same week
1 parent 071c504 commit 0630b8b

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/routes/users.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ export const setupUsersRoutes = function(app: Express, prisma: PrismaClient): vo
216216
cursus_id: {
217217
in: DISCO_PISCINE_CURSUS_IDS,
218218
},
219-
end_at: discopiscine.end_at,
219+
end_at: {
220+
in: discopiscine.end_ats,
221+
},
220222
},
221223
},
222224
},
@@ -226,7 +228,9 @@ export const setupUsersRoutes = function(app: Express, prisma: PrismaClient): vo
226228
cursus_id: {
227229
in: DISCO_PISCINE_CURSUS_IDS,
228230
},
229-
end_at: discopiscine.end_at,
231+
end_at: {
232+
in: discopiscine.end_ats,
233+
},
230234
},
231235
},
232236
},

src/utils.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IntraUser } from "./intra/oauth";
55
import NodeCache from "node-cache";
66
const cursusCache = new NodeCache();
77
const PISCINE_MIN_USER_COUNT = 40;
8+
const DISCO_PISCINE_MIN_USER_COUNT = 10;
89
const prisma = new PrismaClient();
910

1011
const months = [
@@ -64,7 +65,7 @@ export interface DiscoPiscine {
6465
year_num: number;
6566
week: string;
6667
week_num: number;
67-
end_at: Date;
68+
end_ats: Date[];
6869
user_count: number;
6970
};
7071

@@ -164,7 +165,7 @@ export const getAllDiscoPiscines = async function(prisma: PrismaClient, limitToC
164165
return cachedData as DiscoPiscine[];
165166
}
166167

167-
// Find all possible discovery piscines from the database, no matter the amount of users.
168+
// Find all possible discovery piscines with over DISCO_PISCINE_MIN_USER_COUNT users
168169
// Assume all discovery piscines end at the exact same time.
169170
// We look at end_at instead of begin_at as some latecomers might have a different begin_at.
170171
const disco_piscines_cursus_users = await prisma.cursusUser.groupBy({
@@ -186,7 +187,8 @@ export const getAllDiscoPiscines = async function(prisma: PrismaClient, limitToC
186187
});
187188

188189
// Create disco piscines array from the grouped data
189-
const discoPiscines: DiscoPiscine[] = disco_piscines_cursus_users.flatMap((p) => {
190+
const discoPiscines: DiscoPiscine[] = [];
191+
for (const p of disco_piscines_cursus_users) {
190192
// Do not include empty end_at
191193
if (!p.end_at) {
192194
return [];
@@ -196,23 +198,34 @@ export const getAllDiscoPiscines = async function(prisma: PrismaClient, limitToC
196198
const beginDate = new Date(endDate.getTime() - (7 * 24 * 60 * 60 * 1000)); // 1 week before end_at
197199
const year = beginDate.getFullYear();
198200
const weekNumber = getISOWeekNumber(beginDate);
199-
return {
201+
// If a disco piscine for this year and week already exists, just add the user count and end_at
202+
const existingPiscine = discoPiscines.find((dp) => dp.year_num === year && dp.week_num === weekNumber);
203+
if (existingPiscine) {
204+
existingPiscine.user_count += p._count.id;
205+
existingPiscine.end_ats.push(endDate);
206+
continue;
207+
}
208+
209+
discoPiscines.push({
200210
year: year.toString(),
201211
year_num: year,
202212
week: weekNumber.toString().padStart(2, '0'), // Ensure week is two digits
203213
week_num: weekNumber,
204-
end_at: endDate,
214+
end_ats: [endDate],
205215
user_count: p._count.id,
206-
};
207-
});
216+
});
217+
}
218+
219+
// Remove disco piscines with less than DISCO_PISCINE_MIN_USER_COUNT users
220+
const filteredDiscoPiscines = discoPiscines.filter((dp) => dp.user_count >= DISCO_PISCINE_MIN_USER_COUNT);
208221

209222
// Cache the result for 5 minutes
210-
cursusCache.set('allDiscoPiscines', discoPiscines, 300);
223+
cursusCache.set('allDiscoPiscines', filteredDiscoPiscines, 300);
211224
if (limitToCurrentYear) {
212225
const currentYear = new Date().getFullYear();
213-
return (discoPiscines as DiscoPiscine[]).filter((p) => p.year_num === currentYear);
226+
return (filteredDiscoPiscines as DiscoPiscine[]).filter((p) => p.year_num === currentYear);
214227
}
215-
return discoPiscines;
228+
return filteredDiscoPiscines;
216229
};
217230

218231
export const getLatestCohort = async function(prisma: PrismaClient): Promise<Cohort | null> {

templates/users.njk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
window.location.href = '/users/disco/' + this.value.replace('-', '/');
3939
});
4040
</script>
41-
<i><small>If a week appears multiple times here, ask the pedago team to make sure the cursus_user end_at is the same for all discovery pisciners of one discovery piscine.</small></i>
4241
{% endif %}
4342
{# if cohorts are defined in the data given to the template, display a cohort year selection #}
4443
{% if cohorts %}

0 commit comments

Comments
 (0)