From 1439fd1861fcc939ddc57e7b2cc99fc60cc461b0 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Thu, 7 Nov 2024 12:29:09 -0300 Subject: [PATCH] Fix: duplicated users after merge/delete profile --- src/Root.tsx | 20 +++++++++++++++++++- src/constants/device-emitter-events.ts | 1 + src/lib/Braze/index.ts | 20 ++++++++++++++++++-- src/store/bitpay-id/bitpay-id.effects.ts | 13 ++++++++++--- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/Root.tsx b/src/Root.tsx index d56a2cffb..85cfe25d0 100644 --- a/src/Root.tsx +++ b/src/Root.tsx @@ -143,6 +143,7 @@ import { setAccountEVMCreationMigrationComplete, successAddWallet, } from './store/wallet/wallet.actions'; +import {BrazeWrapper} from './lib/Braze'; const {Timer, SilentPushEvent, InAppMessageModule} = NativeModules; @@ -276,7 +277,6 @@ export default () => { const lockAuthorizedUntil = useAppSelector( ({APP}) => APP.lockAuthorizedUntil, ); - const inAppMessageData = useAppSelector(({APP}) => APP.inAppMessageData); const keys = useAppSelector(({WALLET}) => WALLET.keys); const accountEvmCreationMigrationComplete = useAppSelector( ({WALLET}) => WALLET.accountEvmCreationMigrationComplete, @@ -471,6 +471,24 @@ export default () => { return () => subscriptionAppStateChange.remove(); }, [pinLockActive, biometricLockActive]); + useEffect(() => { + const eventBrazeListener = DeviceEventEmitter.addListener( + DeviceEmitterEvents.SHOULD_DELETE_BRAZE_USER, + async eid => { + // Wait for a few seconds to ensure the user is deleted + await sleep(20000); + LogActions.info('Deleting old user EID: ', eid); + await BrazeWrapper.delete(eid); + await sleep(3000); + BrazeWrapper.endMergingUser(); + }, + ); + + return () => { + eventBrazeListener.remove(); + }; + }, []); + // THEME useEffect(() => { function onAppStateChange(status: AppStateStatus) { diff --git a/src/constants/device-emitter-events.ts b/src/constants/device-emitter-events.ts index 19808ab9c..ee4c73a09 100644 --- a/src/constants/device-emitter-events.ts +++ b/src/constants/device-emitter-events.ts @@ -38,4 +38,5 @@ export enum DeviceEmitterEvents { PUSH_NOTIFICATIONS = 'PUSH_NOTIFICATIONS', FIX_WALLET_ADDRESS = 'FIX_WALLET_ADDRESS', APP_TOKENS_DATA_LOADED = 'APP_TOKENS_DATA_LOADED', + SHOULD_DELETE_BRAZE_USER = 'SHOULD_DELETE_BRAZE_USER', } diff --git a/src/lib/Braze/index.ts b/src/lib/Braze/index.ts index be3bcf74d..2ae7e1d87 100644 --- a/src/lib/Braze/index.ts +++ b/src/lib/Braze/index.ts @@ -138,6 +138,7 @@ export const BrazeWrapper = (() => { userId?: string; attributes?: BrazeUserAttributes; } = {}; + let mergingUser = false; return { init() { @@ -148,6 +149,9 @@ export const BrazeWrapper = (() => { userId: string | undefined, attributes?: BrazeUserAttributes | undefined, ) { + if (mergingUser) { + return; + } if (!lastSeenIdentity) { lastSeenIdentity = {}; } @@ -175,6 +179,14 @@ export const BrazeWrapper = (() => { }; }, + startMergingUser() { + mergingUser = true; + }, + + endMergingUser() { + mergingUser = false; + }, + merge(userToMerge: string, userToKeep: string) { return mergeUsers(userToMerge, userToKeep); }, @@ -186,11 +198,15 @@ export const BrazeWrapper = (() => { screen(name: string, properties: Record = {}) { const screenName = `Viewed ${name} Screen`; - Braze.logCustomEvent(screenName, properties); + if (!mergingUser) { + Braze.logCustomEvent(screenName, properties); + } }, track(eventName: string, properties: Record = {}) { - Braze.logCustomEvent(eventName, properties); + if (!mergingUser) { + Braze.logCustomEvent(eventName, properties); + } }, }; })(); diff --git a/src/store/bitpay-id/bitpay-id.effects.ts b/src/store/bitpay-id/bitpay-id.effects.ts index fcb19b18b..139812308 100644 --- a/src/store/bitpay-id/bitpay-id.effects.ts +++ b/src/store/bitpay-id/bitpay-id.effects.ts @@ -28,6 +28,8 @@ import axios from 'axios'; import {BASE_BITPAY_URLS} from '../../constants/config'; import Braze from '@braze/react-native-sdk'; import {dismissOnGoingProcessModal, setBrazeEid} from '../app/app.actions'; +import {DeviceEmitterEvents} from '../../constants/device-emitter-events'; +import {DeviceEventEmitter} from 'react-native'; interface StartLoginParams { email: string; @@ -59,10 +61,15 @@ export const startBitPayIdAnalyticsInit = // Check if Braze EID exists and different if (APP.brazeEid && APP.brazeEid !== eid) { + BrazeWrapper.startMergingUser(); // Should migrate the user to the new EID + LogActions.info('Merging current user to new EID: ', eid); await BrazeWrapper.merge(APP.brazeEid, eid); - // Delete old user - await BrazeWrapper.delete(APP.brazeEid); + // Emit an event that delete the user + DeviceEventEmitter.emit( + DeviceEmitterEvents.SHOULD_DELETE_BRAZE_USER, + APP.brazeEid, + ); } dispatch(setBrazeEid(eid)); @@ -456,7 +463,7 @@ const startPairAndLoadUser = ); } - dispatch(startBitPayIdStoreInit(data.user)); + await dispatch(startBitPayIdStoreInit(data.user)); dispatch(CardEffects.startCardStoreInit(data.user)); dispatch(ShopEffects.startFetchCatalog()); dispatch(ShopEffects.startSyncGiftCards()).then(() =>