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 e0302e8 commit 561d39c
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions translation-app-assessment/backend/cleanup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ app.use(cookieParser())
app.disable('x-powered-by')
require("dotenv");
const cors = require('cors');
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.applicationDefault()
});

const corsOptions = {
origin: process.env.FRONTEND_URL,
Expand All @@ -32,7 +29,9 @@ const corsOptions = {
app.use(cors(corsOptions));
// Init project and services
const projectId = process.env.GCP_PROJECT
firebaseAdmin.initializeApp()
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.applicationDefault()
});
const firestore = firebaseAdmin.firestore()
// Function to write monitoring "heartbeat" that cleanup has run
const writeMonitoring = async () => {
Expand Down Expand Up @@ -81,23 +80,23 @@ app.get('/', async (req, res) => {

app.post('/', async (req, res, next) => {
try {
await kpi()
// Delete chat that have been expired for an hour or longer
const deletionPromises = []
const collection = firestore.collection('chats')
const deletionTimeThreshold = parseInt(Moment().subtract(1, 'hour').format('x'))
const querySnapshot = await collection.where('expiryDate', '<', deletionTimeThreshold).get()
for (const docSnapshot of querySnapshot.docs) {
deletionPromises.push(collection.doc(docSnapshot.id).delete())
}
await Promise.all(deletionPromises)
if(process.env.ID_BOT !== undefined) await cleanRatesFromBot();
await writeMonitoring()
console.log(`deleted chats, size:${querySnapshot.size}`)

await createLanguagesFromRates();
// await kpi()
// // Delete chat that have been expired for an hour or longer
// const deletionPromises = []
// const collection = firestore.collection('chats')
// const deletionTimeThreshold = parseInt(Moment().subtract(1, 'hour').format('x'))
// const querySnapshot = await collection.where('expiryDate', '<', deletionTimeThreshold).get()
// for (const docSnapshot of querySnapshot.docs) {
// deletionPromises.push(collection.doc(docSnapshot.id).delete())
// }
// await Promise.all(deletionPromises)
// if(process.env.ID_BOT !== undefined) await cleanRatesFromBot();
// await writeMonitoring()
// console.log(`deleted chats, size:${querySnapshot.size}`)
//
// await createLanguagesFromRates();
await deleteInactiveUsers();
res.status(204).send()
// res.status(204).send()
} catch(e) {
next(e)
}
Expand Down

0 comments on commit 561d39c

Please sign in to comment.