Skip to content

Commit e4b6a7a

Browse files
committed
fix: anonymisation not working with student API key
1 parent 8e085bf commit e4b6a7a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/intra/cleanup.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

src/intra/users.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { prisma, syncData } from './base';
33
import { monthToNumber } from '../utils';
44
import { CAMPUS_ID } from '../env';
55

6+
// User object can be an object returned by /v2/users/:id or the user object in /v2/cursus_users/:id !
67
export const syncUser = async function(user: any): Promise<void> {
78
try {
89
await prisma.user.upsert({

0 commit comments

Comments
 (0)