@@ -463,21 +463,23 @@ export const setupQuizRoutes = function(app: Express, prisma: PrismaClient): voi
463463 }
464464 else {
465465 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, try again later' } ) ;
466+ return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to coalition patch error , try again later' } ) ;
467467 }
468468 }
469469 else {
470- // Make sure the 42cursus allows for a coalition
470+ // Make sure the cursus_user allows for a coalition
471471 const cursus_users = await fetchSingle42ApiPage ( api , `/cursus_users` , {
472472 'filter[user_id]' : user . id . toString ( ) ,
473473 'filter[cursus_id]' : CURSUS_ID . toString ( ) ,
474474 } ) ;
475475 if ( cursus_users . length === 0 ) {
476- console . error ( `User ${ user . login } is not enrolled in the 42cursus with ID ${ CURSUS_ID } ` ) ;
477- return res . status ( 412 ) . send ( { error : 'Failed to join coalition, try again later' } ) ;
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' } ) ;
478478 }
479+
480+ // Patch the cursus_user to allow for a coalition if needed
479481 if ( cursus_users [ 0 ] . has_coalition === false ) {
480- console . log ( `Patching user ${ user . login } 's cursus_user to allow for a coalition in the 42cursus ...` ) ;
482+ console . log ( `Patching user ${ user . login } 's cursus_user to allow for a coalition in the cursus ...` ) ;
481483 const response = await api . patch ( `/cursus_users/${ cursus_users [ 0 ] . id } ` , {
482484 cursus_user : {
483485 has_coalition : true ,
@@ -486,31 +488,68 @@ export const setupQuizRoutes = function(app: Express, prisma: PrismaClient): voi
486488 console . log ( `${ user . login } 's cursus_user patch response: ${ response . status } ${ response . statusText } ` ) ;
487489 }
488490
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 ,
499+ }
500+ } ) ;
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' } ) ;
504+ }
505+ else {
506+ console . log ( `${ user . login } 's cursus_user end_at patch response: ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
507+ }
508+ }
509+
489510 console . log ( `Creating a new IntraCoalitionUser for user ${ user . login } in coalition ${ coalitionId } ` ) ;
490- const response = await api . post ( '/coalitions_users' , {
511+ const coalitionUserCreateResponse = await api . post ( '/coalitions_users' , {
491512 coalitions_user : {
492513 user_id : user . id ,
493514 coalition_id : coalitionId ,
494515 this_year_score : 0 ,
495516 }
496517 } ) ;
497- if ( response . status === 201 ) {
498- const responseBody = await response . json ( ) ;
518+
519+ // Make sure to restore the cursus_user's end_at date if it was modified earlier
520+ if ( cursusEndAt && cursusEndAt < now ) {
521+ // Restore the original end_at date
522+ console . log ( `Restoring user ${ user . login } 's cursus_user end_at to ${ cursusEndAt . toISOString ( ) } ...` ) ;
523+ const endAtResponse = await api . patch ( `/cursus_users/${ cursus_users [ 0 ] . id } ` , {
524+ cursus_user : {
525+ end_at : cursusEndAt . toISOString ( ) ,
526+ }
527+ } ) ;
528+ if ( endAtResponse . status !== 204 ) {
529+ console . warn ( `Failed to restore cursus_user end_at for user ${ user . login } : ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
530+ }
531+ else {
532+ console . log ( `${ user . login } 's cursus_user end_at restore patch response: ${ endAtResponse . status } ${ endAtResponse . statusText } ` ) ;
533+ }
534+ }
535+
536+ if ( coalitionUserCreateResponse . status === 201 ) {
537+ const responseBody = await coalitionUserCreateResponse . json ( ) ;
499538 if ( ! responseBody . id ) {
500539 console . error ( `Expected key 'id' in response data missing` , responseBody ) ;
501- return res . status ( 500 ) . send ( { error : 'Failed to join coalition, try again later' } ) ;
540+ return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to coalition user creation error , try again later' } ) ;
502541 }
503542 const coalitionUser = await fetchSingle42ApiPage ( api , `/coalitions_users/${ responseBody . id } ` ) ;
504543 if ( ! coalitionUser ) {
505544 console . error ( `Failed to fetch coalition user ${ responseBody . id } , was probably not created?` ) ;
506- return res . status ( 500 ) . send ( { error : 'Failed to join coalition, try again later' } ) ;
545+ return res . status ( 500 ) . send ( { error : 'Failed to join coalition due to coalition user fetch error , try again later' } ) ;
507546 }
508547 await syncCoalitionUser ( coalitionUser ) ;
509548 console . log ( `User ${ user . login } joined coalition ${ coalitionId } with a new IntraCoalitionUser ${ coalitionUser . id } ` ) ;
510549 }
511550 else {
512- console . error ( `Failed to create coalition user for user ${ user . login } : ${ response . status } ${ response . statusText } ` ) ;
513- return res . status ( 500 ) . send ( { error : 'Failed to join coalition, try again later' } ) ;
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' } ) ;
514553 }
515554 }
516555
0 commit comments