@@ -7,7 +7,7 @@ import { ExpressIntraUser } from '../sync/oauth';
77import { getAPIClient } from '../utils' ;
88import { fetchSingle42ApiPage } from '../sync/base' ;
99import { syncCoalitionUser } from '../sync/coalitions_users' ;
10- import { CURSUS_ID } from '../env' ;
10+ import { ASSISTANTS_CAN_QUIZ , CURSUS_ID } from '../env' ;
1111
1212export interface QuizSessionQuestion {
1313 question : CodamCoalitionTestQuestion ;
@@ -59,11 +59,9 @@ export const isQuizAvailable = async function(user: IntraUser | ExpressIntraUser
5959 } ) ;
6060 const currentDate = new Date ( ) ;
6161 const availableDueToTime = ( currentDate . getTime ( ) >= settings . start_at . getTime ( ) && currentDate . getTime ( ) < settings . deadline_at . getTime ( ) ) ;
62- if ( availableDueToTime ) {
63- return true ; // Skip any further database queries, the questionnaire is available for everyone!
64- }
6562
6663 // If the user is not part of any coalition currently, taking the questionnaire is always allowed, as long as their cursus is ongoing
64+ // Also allow assistants to take the quiz if the relevant env var is set
6765 const userDetails = await prisma . intraUser . findFirst ( {
6866 where : {
6967 id : user . id ,
@@ -94,13 +92,31 @@ export const isQuizAvailable = async function(user: IntraUser | ExpressIntraUser
9492 end_at : true ,
9593 } ,
9694 } ,
95+ group_users : {
96+ select : {
97+ id : true ,
98+ } ,
99+ where : {
100+ group_id : parseInt ( process . env . INTRA_ASSISTANT_GROUP_ID || '0' ) ,
101+ } ,
102+ } ,
97103 } ,
98104 } ) ;
99105 if ( ! userDetails ) {
100106 console . warn ( `User ${ user . id } not found in database when checking quiz availability` ) ;
101107 return false ;
102108 }
103- return ( userDetails . coalition_users . length == 0 && userDetails . cursus_users . length > 0 && ! userDetails . cursus_users [ 0 ] . end_at ) ;
109+ if ( userDetails . coalition_users . length === 0 || availableDueToTime ) {
110+ if ( userDetails . cursus_users . length > 0 && ! userDetails . cursus_users [ 0 ] . end_at ) {
111+ console . log ( `User ${ user . id } has an ongoing cursus in cursus ${ CURSUS_ID } , allowing to take the questionnaire` ) ;
112+ return true ; // User has an ongoing cursus in the relevant cursus, allow taking the questionnaire
113+ }
114+ if ( userDetails . group_users . length > 0 && ASSISTANTS_CAN_QUIZ ) {
115+ console . log ( `User ${ user . id } is an assistant and assistants are allowed to take the quiz due to env var ASSISTANTS_CAN_QUIZ, allowing to take the questionnaire` ) ;
116+ return true ; // User is an assistant and assistants are allowed to take the quiz due to env var ASSISTANTS_CAN_QUIZ
117+ }
118+ }
119+ return false ;
104120}
105121
106122const resetQuizSession = async function ( req : Request , userSession : CustomSessionData ) : Promise < void > {
0 commit comments