diff --git a/functions/index.js b/functions/index.js index e8369f7..bece0c2 100644 --- a/functions/index.js +++ b/functions/index.js @@ -926,7 +926,7 @@ async function updateUserGroupRole(snap, context) { groupData.users = {}; } - if (typeof snap !== "undefined" && typeof snap.data() !== "undefined" && typeof snap.data().role !== "undefined") { + if (typeof snap === "object" && snap !== null && snap.data !== undefined && typeof snap.data === "function" && typeof snap.data() === "object" && snap.data() !== null && snap.data().role !== undefined) { groupData.users[context.params.userId] = snap.data().role; } else { delete groupData.users[context.params.userId]; @@ -963,6 +963,18 @@ exports.userGroupRoleUpdated = functions.firestore.document("users/{userId}/grou return updateUserGroupRole(change.after, context); }); +/** + * Removes an existing user from a group in the groups collection + * in Firestore, after the group has been removed from their + * document in the users collection. + * NOTE: Can't be unit tested. + * @return {boolean} Returns true on completion. +*/ +exports.userGroupRoleDeleted = functions.firestore.document("users/{userId}/groups/{groupId}") + .onDelete(async (snap, context) => { + return updateUserGroupRole(null, context); + }); + /** * Generates a random, unused group join code. * @return {string} The join code.