Updated "deleteUser" Firebase cloud function to perform updates in an atomic way, tweaked UI call and error handling

This commit is contained in:
gianantoniopini
2021-04-23 15:16:37 +02:00
parent d4e7914a27
commit 54ddfe30d7
2 changed files with 15 additions and 17 deletions

View File

@ -19,8 +19,9 @@ const deleteUserFunctionHandler = async (_, { auth }) => {
}
try {
// 1. Delete user resumes
const userId = auth.uid;
const updates = {};
const userResumesDataSnapshot = await admin
.database()
.ref('resumes')
@ -30,18 +31,21 @@ const deleteUserFunctionHandler = async (_, { auth }) => {
const userResumes = userResumesDataSnapshot.val();
if (userResumes) {
Object.keys(userResumes).forEach(async (resumeId) => {
await admin.database().ref(`resumes/${resumeId}`).remove();
updates[`resumes/${resumeId}`] = null;
});
}
// 2. Delete user
const userDataSnapshot = await admin
.database()
.ref(`users/${userId}`)
.once('value');
const user = userDataSnapshot.val();
if (user) {
await admin.database().ref(`users/${userId}`).remove();
updates[`users/${userId}`] = null;
}
if (Object.keys(updates).length > 0) {
await admin.database().ref().update(updates);
}
return true;