@@ -5,6 +5,7 @@ import { IntraUser } from "./intra/oauth";
55import NodeCache from "node-cache" ;
66const cursusCache = new NodeCache ( ) ;
77const PISCINE_MIN_USER_COUNT = 40 ;
8+ const DISCO_PISCINE_MIN_USER_COUNT = 10 ;
89const prisma = new PrismaClient ( ) ;
910
1011const 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
218231export const getLatestCohort = async function ( prisma : PrismaClient ) : Promise < Cohort | null > {
0 commit comments