Skip to content

Commit

Permalink
Remove inactive user (more thant 1 year without connect) in cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Alza committed Jul 24, 2024
1 parent c6189fc commit 9b37b38
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
28 changes: 14 additions & 14 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ deploy-job:
environment:
name: open-source
script:
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/translation
- gcloud builds submit .
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/reporting
- gcloud builds submit .
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/token-broker
- gcloud builds submit .
# - cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/translation
# - gcloud builds submit .
# - cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/reporting
# - gcloud builds submit .
# - cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/token-broker
# - gcloud builds submit .
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/cleanup
- gcloud builds submit .
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/telemetry
- gcloud builds submit .
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/authentication
- gcloud builds submit .
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/detect-text
- gcloud builds submit .
- cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/pdf-to-image
- gcloud builds submit .
# - cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/telemetry
# - gcloud builds submit .
# - cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/authentication
# - gcloud builds submit .
# - cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/detect-text
# - gcloud builds submit .
# - cd /builds/petranslate/TradEmploi-backend/translation-app-assessment/backend/pdf-to-image
# - gcloud builds submit .
only:
refs:
- master
39 changes: 38 additions & 1 deletion translation-app-assessment/backend/cleanup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const corsOptions = {
origin: process.env.FRONTEND_URL,
credentials: true, // Permet les requêtes incluant les cookies
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'Origin'],
methods: ['GET'],
methods: ['GET', 'POST'],
};

app.use(cors(corsOptions));
Expand Down Expand Up @@ -93,6 +93,7 @@ app.post('/', async (req, res, next) => {
console.log(`deleted chats, size:${querySnapshot.size}`)

await createLanguagesFromRates();
await deleteInactiveUsers();
res.status(204).send()
} catch(e) {
next(e)
Expand Down Expand Up @@ -361,3 +362,39 @@ async function createLanguage(isoCode, occurrences, average) {
await firestore.collection("languages").doc(isoCode).set(data)
}

async function deleteInactiveUsers() {
const auth = firebaseAdmin.auth();
const oneYearAgo = Date.now() - 365 * 24 * 60 * 60 * 1000;
try {
let usersDeleted = 0;
let nextPageToken;

do {
const result = await auth.listUsers(1000, nextPageToken);
const inactiveUsers = result.users.filter(user => {
const lastSignInTime = user.metadata.lastSignInTime;
return lastSignInTime && new Date(lastSignInTime).getTime() < oneYearAgo;
});

const deletionPromises = inactiveUsers.map(user => {
return auth.deleteUser(user.uid)
.then(() => {
console.log(`Successfully deleted user: ${user.uid}`);
usersDeleted++;
})
.catch(error => {
console.error(`Error deleting user ${user.uid}:`, error);
});
});

await Promise.all(deletionPromises);

nextPageToken = result.pageToken;
} while (nextPageToken);

console.log(`Deleted ${usersDeleted} inactive users.`);
} catch (error) {
console.error('Error listing users:', error);
}
}

0 comments on commit 9b37b38

Please sign in to comment.