@@ -50,6 +50,7 @@ const anonymizeUsers = async function(api: Fast42): Promise<void> {
5050 anonymize_date : {
5151 lt : new Date ( ) ,
5252 not : null ,
53+ gt : new Date ( '1970-01-01' ) , // timestamp > 0
5354 } ,
5455 login : {
5556 not : {
@@ -62,18 +63,28 @@ const anonymizeUsers = async function(api: Fast42): Promise<void> {
6263 // Request the anonymized data from the API and overwrite the local data
6364 for ( const user of users ) {
6465 try {
65- const anonymizedData = await fetchSingle42ApiPage ( api , `/users/` , {
66- 'filter[id]' : user . id . toString ( ) ,
66+ const cursusUser = await prisma . cursusUser . findFirst ( {
67+ where : {
68+ user_id : user . id ,
69+ } ,
6770 } ) ;
68- if ( anonymizedData . length == 0 ) {
69- console . error ( `Anonymized data for user ${ user . login } not found` ) ;
71+ if ( ! cursusUser ) {
72+ console . warn ( `User ${ user . login } has no cursus_users, cannot anonymize!` ) ;
73+ continue ;
74+ }
75+ console . log ( `Anonymizing user ${ user . login } using cursus_user ${ cursusUser . id } ...` ) ;
76+ // Fetch user using cursus_user to circumvent the fact that the Intra API does not return anonymized users
77+ // when using a regular student API key, even when requesting the user object with the specific user ID.
78+ // The user data is still intact in the cursus_user object, so we can copy Intra's anonymized name from there.
79+ const anonymizedData = await fetchSingle42ApiPage ( api , `/cursus_users/${ cursusUser . id } ` , { } ) ;
80+ if ( ! anonymizedData . user ) {
81+ console . warn ( `User ${ user . login } has no user data in their cursus_user ${ cursusUser . id } , cannot anonymize!` ) ;
7082 continue ;
7183 }
72- console . log ( `Anonymizing user ${ user . login } ...` ) ;
73- await syncUser ( anonymizedData [ 0 ] ) ;
84+ await syncUser ( anonymizedData . user ) ;
7485 }
7586 catch ( err ) {
76- console . error ( `Error anonymizing user ${ user . login } , deleting them (user ID ${ user . id } ) : ${ err } ` ) ;
87+ console . error ( `Error anonymizing user ${ user . login } : ${ err } ` ) ;
7788 }
7889 }
7990} ;
0 commit comments