@@ -442,80 +442,116 @@ export const setupQuizRoutes = function(app: Express, prisma: PrismaClient): voi
442442 } ) ;
443443
444444 const api = await getAPIClient ( ) ;
445- if ( intraCoalitionUser ) {
446- console . log ( `Found existing IntraCoalitionUser ${ intraCoalitionUser . id } for user ${ user . login } (currently in coalition ${ intraCoalitionUser . coalition_id } ), patching using Intra API...` ) ;
447- // Patch the user's coalition ID in Intra
448- const response = await api . patch ( `/coalitions_users/${ intraCoalitionUser . id } ` , {
449- coalitions_user : {
450- coalition_id : coalitionId ,
445+ let joined = false ;
446+
447+ // Make sure the cursus_user allows for a coalition
448+ const cursus_users = await fetchSingle42ApiPage ( api , `/cursus_users` , {
449+ 'filter[user_id]' : user . id . toString ( ) ,
450+ 'filter[cursus_id]' : CURSUS_ID . toString ( ) ,
451+ } ) ;
452+ if ( cursus_users . length === 0 ) {
453+ console . error ( `User ${ user . login } is not enrolled in the cursus with ID ${ CURSUS_ID } ` ) ;
454+ return res . status ( 412 ) . send ( { error : 'Failed to join coalition due to cursus enrollment error, try again later' } ) ;
455+ }
456+
457+ // Patch the cursus_user to allow for a coalition if needed
458+ if ( cursus_users [ 0 ] . has_coalition === false ) {
459+ console . log ( `Patching user ${ user . login } 's cursus_user to allow for a coalition in the cursus...` ) ;
460+ const response = await api . patch ( `/cursus_users/${ cursus_users [ 0 ] . id } ` , {
461+ cursus_user : {
462+ has_coalition : true ,
451463 }
452464 } ) ;
453- if ( response . status === 204 ) {
454- await prisma . intraCoalitionUser . update ( {
455- where : {
456- id : intraCoalitionUser . id
457- } ,
458- data : {
459- coalition_id : coalitionId
460- }
461- } ) ;
462- console . log ( `User ${ user . login } joined coalition ${ coalitionId } with an existing IntraCoalitionUser ${ intraCoalitionUser . id } ` ) ;
465+ console . log ( `${ user . login } 's cursus_user patch response: ${ response . status } ${ response . statusText } ` ) ;
466+ }
467+
468+ // Temporarily reopen the cursus if it has already ended
469+ const cursusEndAt = cursus_users [ 0 ] . end_at ? new Date ( cursus_users [ 0 ] . end_at ) : null ;
470+ const now = new Date ( ) ;
471+ if ( cursusEndAt && cursusEndAt < now ) {
472+ console . log ( `User ${ user . login } 's cursus has already ended at ${ cursusEndAt . toISOString ( ) } . Modifying cursus_user's end_at temporarily...` ) ;
473+ const endAtResponse = await api . patch ( `/cursus_users/${ cursus_users [ 0 ] . id } ` , {
474+ cursus_user : {
475+ end_at : null ,
476+ }
477+ } ) ;
478+ if ( endAtResponse . status !== 204 ) {
479+ console . error ( `Failed to patch cursus_user end_at for user ${ user . login } : ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
480+ return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to cursus end error, try again later' } ) ;
463481 }
464482 else {
465- console . error ( `Failed to patch coalition ID for user ${ user . login } : ${ response . status } ${ response . statusText } ` ) ;
466- return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to coalition patch error, try again later' } ) ;
483+ console . log ( `${ user . login } 's cursus_user end_at patch response: ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
467484 }
468485 }
469- else {
470- // Make sure the cursus_user allows for a coalition
471- const cursus_users = await fetchSingle42ApiPage ( api , `/cursus_users` , {
486+
487+ try {
488+ // Check for an existing coalitionUser on Intra
489+ const coalitionIds = await prisma . intraCoalition . findMany ( {
490+ select : {
491+ id : true ,
492+ } ,
493+ } ) ;
494+ const coalitionIdList = coalitionIds . map ( c => c . id ) ;
495+ const existingCoalitionUser = await fetchSingle42ApiPage ( api , `/coalitions_users` , {
472496 'filter[user_id]' : user . id . toString ( ) ,
473- 'filter[cursus_id ]' : CURSUS_ID . toString ( ) ,
497+ 'filter[coalition_id ]' : coalitionIdList . join ( ',' ) ,
474498 } ) ;
475- if ( cursus_users . length === 0 ) {
476- console . error ( `User ${ user . login } is not enrolled in the cursus with ID ${ CURSUS_ID } ` ) ;
477- return res . status ( 412 ) . send ( { error : 'Failed to join coalition due to cursus enrollment error, try again later' } ) ;
478- }
479499
480- // Patch the cursus_user to allow for a coalition if needed
481- if ( cursus_users [ 0 ] . has_coalition === false ) {
482- console . log ( `Patching user ${ user . login } 's cursus_user to allow for a coalition in the cursus...` ) ;
483- const response = await api . patch ( `/cursus_users /${ cursus_users [ 0 ] . id } ` , {
484- cursus_user : {
485- has_coalition : true ,
500+ if ( existingCoalitionUser . length > 0 ) {
501+ console . log ( `Found existing IntraCoalitionUser ${ existingCoalitionUser [ 0 ] . id } for user ${ user . login } (currently in coalition ${ existingCoalitionUser [ 0 ] . coalition_id } ), patching using Intra API...` ) ;
502+ // Patch the user's coalition ID in Intra
503+ const response = await api . patch ( `/coalitions_users /${ existingCoalitionUser [ 0 ] . id } ` , {
504+ coalitions_user : {
505+ coalition_id : coalitionId ,
486506 }
487507 } ) ;
488- console . log ( `${ user . login } 's cursus_user patch response: ${ response . status } ${ response . statusText } ` ) ;
508+ if ( response . status === 204 ) {
509+ existingCoalitionUser [ 0 ] . coalition_id = coalitionId ;
510+ await syncCoalitionUser ( existingCoalitionUser [ 0 ] ) ;
511+ console . log ( `User ${ user . login } joined coalition ${ coalitionId } with an existing IntraCoalitionUser ${ existingCoalitionUser [ 0 ] . id } ` ) ;
512+ joined = true ;
513+ }
514+ else {
515+ console . error ( `Failed to patch coalition ID for user ${ user . login } : ${ response . status } ${ response . statusText } ` ) ;
516+ throw new Error ( `Failed to patch coalitionuser ${ existingCoalitionUser [ 0 ] . id } for user ${ user . login } ` ) ;
517+ }
489518 }
490-
491- // Temporarily reopen the cursus if it has already ended
492- const cursusEndAt = cursus_users [ 0 ] . end_at ? new Date ( cursus_users [ 0 ] . end_at ) : null ;
493- const now = new Date ( ) ;
494- if ( cursusEndAt && cursusEndAt < now ) {
495- console . log ( `User ${ user . login } 's cursus has already ended at ${ cursusEndAt } . Modifying cursus_user's end_at temporarily...` ) ;
496- const endAtResponse = await api . patch ( `/cursus_users/${ cursus_users [ 0 ] . id } ` , {
497- cursus_user : {
498- end_at : null ,
519+ else {
520+ console . log ( `Creating a new IntraCoalitionUser for user ${ user . login } in coalition ${ coalitionId } ` ) ;
521+ const coalitionUserCreateResponse = await api . post ( '/coalitions_users' , {
522+ coalitions_user : {
523+ user_id : user . id ,
524+ coalition_id : coalitionId ,
525+ this_year_score : 0 ,
499526 }
500527 } ) ;
501- if ( endAtResponse . status !== 204 ) {
502- console . error ( `Failed to patch cursus_user end_at for user ${ user . login } : ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
503- return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to cursus end error, try again later' } ) ;
528+
529+ if ( coalitionUserCreateResponse . status === 201 ) {
530+ const responseBody = await coalitionUserCreateResponse . json ( ) ;
531+ if ( ! responseBody . id ) {
532+ console . error ( `Expected key 'id' in response data missing` , responseBody ) ;
533+ throw new Error ( 'Expected key id in coalition user creation response missing' ) ;
534+ }
535+ const coalitionUser = await fetchSingle42ApiPage ( api , `/coalitions_users/${ responseBody . id } ` ) ;
536+ if ( ! coalitionUser ) {
537+ console . error ( `Failed to fetch coalition user ${ responseBody . id } , was probably not created?` ) ;
538+ throw new Error ( `Failed to fetch coalition user ${ responseBody . id } after creation` ) ;
539+ }
540+ await syncCoalitionUser ( coalitionUser ) ;
541+ console . log ( `User ${ user . login } joined coalition ${ coalitionId } with a new IntraCoalitionUser ${ coalitionUser . id } ` ) ;
542+ joined = true ;
504543 }
505544 else {
506- console . log ( `${ user . login } 's cursus_user end_at patch response: ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
545+ const responseBody = await coalitionUserCreateResponse . text ( ) ;
546+ console . error ( `Failed to create coalition user for user ${ user . login } : ${ coalitionUserCreateResponse . status } ${ coalitionUserCreateResponse . statusText } - ${ responseBody } ` ) ;
547+ throw new Error ( `Failed to create coalition user for user ${ user . login } ` ) ;
507548 }
508549 }
509-
510- console . log ( `Creating a new IntraCoalitionUser for user ${ user . login } in coalition ${ coalitionId } ` ) ;
511- const coalitionUserCreateResponse = await api . post ( '/coalitions_users' , {
512- coalitions_user : {
513- user_id : user . id ,
514- coalition_id : coalitionId ,
515- this_year_score : 0 ,
516- }
517- } ) ;
518-
550+ }
551+ catch ( err ) {
552+ res . status ( 500 ) . send ( { error : err || 'Internal server error' } ) ;
553+ }
554+ finally {
519555 // Make sure to restore the cursus_user's end_at date if it was modified earlier
520556 if ( cursusEndAt && cursusEndAt < now ) {
521557 // Restore the original end_at date
@@ -532,28 +568,11 @@ export const setupQuizRoutes = function(app: Express, prisma: PrismaClient): voi
532568 console . log ( `${ user . login } 's cursus_user end_at restore patch response: ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
533569 }
534570 }
535-
536- if ( coalitionUserCreateResponse . status === 201 ) {
537- const responseBody = await coalitionUserCreateResponse . json ( ) ;
538- if ( ! responseBody . id ) {
539- console . error ( `Expected key 'id' in response data missing` , responseBody ) ;
540- return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to coalition user creation error, try again later' } ) ;
541- }
542- const coalitionUser = await fetchSingle42ApiPage ( api , `/coalitions_users/${ responseBody . id } ` ) ;
543- if ( ! coalitionUser ) {
544- console . error ( `Failed to fetch coalition user ${ responseBody . id } , was probably not created?` ) ;
545- return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to coalition user fetch error, try again later' } ) ;
546- }
547- await syncCoalitionUser ( coalitionUser ) ;
548- console . log ( `User ${ user . login } joined coalition ${ coalitionId } with a new IntraCoalitionUser ${ coalitionUser . id } ` ) ;
549- }
550- else {
551- console . error ( `Failed to create coalition user for user ${ user . login } : ${ coalitionUserCreateResponse . status } ${ coalitionUserCreateResponse . statusText } ` ) ;
552- return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to coalition user creation error, try again later' } ) ;
553- }
554571 }
555572
556- await resetQuizSession ( req , userSession ) ;
557- return res . redirect ( '/' ) ;
573+ if ( joined ) {
574+ await resetQuizSession ( req , userSession ) ;
575+ return res . redirect ( '/' ) ;
576+ }
558577 } ) ;
559578} ;
0 commit comments