Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
feat(functions): on user delete
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKanera committed May 12, 2021
1 parent 273ee4c commit a1a29b2
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,40 @@ exports.notifications = functions.pubsub.schedule('every 24 hours').timeZone('Eu
}
}
});

exports.deleteUserData = functions.auth.user().onDelete(async (user) => {
const userRef = db.collection('users').doc(user.uid);
const projectRef = db.collection('projects').where('studentId', '==', user.uid).limit(1);
const proposalRef = db.collection('proposals').where('studentId', '==', user.uid).limit(1);

try {
await db.runTransaction(async (transaction) => {
const userDoc = await transaction.get(userRef);
const projectDoc = (await transaction.get(projectRef)).docs[0];
const proposalDoc = (await transaction.get(proposalRef)).docs[0];
const projectFilesDoc = ((await transaction.get(db.collection('projectFiles').where('projectId', '==', projectDoc.id).limit(1))).docs[0]);

if (userDoc?.exists) transaction.delete(userRef);
if (projectDoc?.exists) transaction.delete(projectDoc.ref);
if (proposalDoc?.exists) transaction.delete(proposalDoc.ref);
if (projectFilesDoc?.exists) {
const storage = admin.storage();
const bucket = storage.bucket('ps-project-files');

const filesData = projectFilesDoc.data();
await Promise.all([
...filesData?.mandatory.map((file: any) => bucket.file(file.filePath).delete()),
...filesData?.optional.map((file: any) => bucket.file(file.filePath).delete())
]);

transaction.delete(projectFilesDoc.ref);
}

return transaction;
});
} catch(e) {
functions.logger.error(e);
}

return;
});

0 comments on commit a1a29b2

Please sign in to comment.