Skip to content

Commit

Permalink
fix: anonymisation not working with student API key
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Aug 16, 2024
1 parent 8e085bf commit e4b6a7a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/intra/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const anonymizeUsers = async function(api: Fast42): Promise<void> {
anonymize_date: {
lt: new Date(),
not: null,
gt: new Date('1970-01-01'), // timestamp > 0
},
login: {
not: {
Expand All @@ -62,18 +63,28 @@ const anonymizeUsers = async function(api: Fast42): Promise<void> {
// Request the anonymized data from the API and overwrite the local data
for (const user of users) {
try {
const anonymizedData = await fetchSingle42ApiPage(api, `/users/`, {
'filter[id]': user.id.toString(),
const cursusUser = await prisma.cursusUser.findFirst({
where: {
user_id: user.id,
},
});
if (anonymizedData.length == 0) {
console.error(`Anonymized data for user ${user.login} not found`);
if (!cursusUser) {
console.warn(`User ${user.login} has no cursus_users, cannot anonymize!`);
continue;
}
console.log(`Anonymizing user ${user.login} using cursus_user ${cursusUser.id}...`);
// Fetch user using cursus_user to circumvent the fact that the Intra API does not return anonymized users
// when using a regular student API key, even when requesting the user object with the specific user ID.
// The user data is still intact in the cursus_user object, so we can copy Intra's anonymized name from there.
const anonymizedData = await fetchSingle42ApiPage(api, `/cursus_users/${cursusUser.id}`, {});
if (!anonymizedData.user) {
console.warn(`User ${user.login} has no user data in their cursus_user ${cursusUser.id}, cannot anonymize!`);
continue;
}
console.log(`Anonymizing user ${user.login}...`);
await syncUser(anonymizedData[0]);
await syncUser(anonymizedData.user);
}
catch (err) {
console.error(`Error anonymizing user ${user.login}, deleting them (user ID ${user.id}): ${err}`);
console.error(`Error anonymizing user ${user.login}: ${err}`);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions src/intra/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { prisma, syncData } from './base';
import { monthToNumber } from '../utils';
import { CAMPUS_ID } from '../env';

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

0 comments on commit e4b6a7a

Please sign in to comment.