Skip to content

Commit e61d2c3

Browse files
committed
allowing user deletion even when they have preferences or empty tokens-wallet
1 parent 46145b0 commit e61d2c3

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

cloud/functions/src/functions/firestore/services/user-utils.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {durationOf} from "../../http/utils";
88
import DocumentData = firestore.DocumentData;
99
import QuerySnapshot = firestore.QuerySnapshot;
1010
import {User} from "../../../../../../shared/user.firestore";
11+
import {UserTokensWallet} from "../../../../../../shared/user-tokens-wallet.localstorage";
12+
import DocumentSnapshot = firestore.DocumentSnapshot;
1113

1214

1315
async function getRawUsersMatching(collectionFilter: (collection: CollectionReference) => Query) {
@@ -113,23 +115,25 @@ export async function windowedProcessUsers(
113115
}
114116

115117
async function deleteUserRefIncludingChildren(userRef: DocumentReference<DocumentData>, force = false) {
116-
const [preferencesDoc, tokensWalletDoc] = await Promise.all([
117-
db.doc(`/users/${userRef.id}/preferences/self`).get(),
118-
db.doc(`/users/${userRef.id}/tokens-wallet/self`).get(),
119-
])
120-
121-
// let's not delete
122-
const collectionsPreventingDelete = ([] as string[])
123-
.concat(preferencesDoc.exists ? ["preferences"]:[])
124-
.concat(tokensWalletDoc.exists ? ["tokens-wallet"]:[])
125-
if(collectionsPreventingDelete.length && !force) {
126-
console.info(`Not deleting user ${userRef.id} because he has ${collectionsPreventingDelete.join("/")} non-empty collection`)
118+
const tokensWalletDoc = await db.doc(`${userRef.path}/tokens-wallet/self`).get() as DocumentSnapshot<UserTokensWallet>
119+
const walletSecrets = tokensWalletDoc.data()?.secretTokens;
120+
121+
if(!force && tokensWalletDoc.exists && walletSecrets && (
122+
(walletSecrets.eventOrganizerTokens && walletSecrets.eventOrganizerTokens.length)
123+
|| (walletSecrets.privateSpaceTokens && walletSecrets.privateSpaceTokens.length)
124+
|| (walletSecrets.talkFeedbacksViewerTokens && walletSecrets.talkFeedbacksViewerTokens.length)
125+
)) {
126+
console.info(`Not deleting user ${userRef.id} because he has tokens-wallet non-empty collection`)
127127
return;
128128
}
129129

130+
const preferencesDoc = await db.doc(`${userRef.path}/preferences/self`).get()
131+
130132
await Promise.all([
131133
deleteUserSpaces(userRef),
132134
deleteUserEventsFromNode(userRef),
135+
...(preferencesDoc.exists ? [preferencesDoc.ref.delete()]:[]),
136+
...(tokensWalletDoc.exists ? [tokensWalletDoc.ref.delete()]:[]),
133137
])
134138

135139
console.info(`Deleting user entry ${userRef.path}`)

0 commit comments

Comments
 (0)