From c9a304a17051bfec056653a72c31e2ce2778e951 Mon Sep 17 00:00:00 2001 From: darshan-iterable Date: Tue, 17 Sep 2024 18:13:51 +0530 Subject: [PATCH] [MOB-9505] rename merge parameter (#450) * [MOB-9505] rename merge parameter * fix ci failure and resolved comment * fix: ci failure and merged AUT_main changes --- .../tests/userMergeScenarios.test.ts | 62 +++++++-------- src/authorization/authorization.ts | 76 ++++++++----------- 2 files changed, 65 insertions(+), 73 deletions(-) diff --git a/src/anonymousUserTracking/tests/userMergeScenarios.test.ts b/src/anonymousUserTracking/tests/userMergeScenarios.test.ts index 19b733a1..5451b761 100644 --- a/src/anonymousUserTracking/tests/userMergeScenarios.test.ts +++ b/src/anonymousUserTracking/tests/userMergeScenarios.test.ts @@ -127,7 +127,7 @@ describe('UserMergeScenariosTests', () => { }); describe('UserMergeScenariosTests with setUserID', () => { - it('criteria not met with merge false with setUserId', async () => { + it('criteria not met with disableEventReplay true with setUserId', async () => { const { setUserID, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -142,7 +142,7 @@ describe('UserMergeScenariosTests', () => { SHARED_PREFS_EVENT_LIST_KEY, expect.any(String) ); - await setUserID('testuser123', false); + await setUserID('testuser123', true); const response = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -160,7 +160,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('criteria not met with merge true with setUserId', async () => { + it('criteria not met with disableEventReplay false with setUserId', async () => { const { setUserID, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -175,7 +175,7 @@ describe('UserMergeScenariosTests', () => { SHARED_PREFS_EVENT_LIST_KEY, expect.any(String) ); - await setUserID('testuser123', true); + await setUserID('testuser123', false); const response = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -193,7 +193,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('criteria not met with merge default value with setUserId', async () => { + it('criteria not met with disableEventReplay default value with setUserId', async () => { const { setUserID, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -226,7 +226,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('criteria is met with merge false with setUserId', async () => { + it('criteria is met with disableEventReplay true with setUserId', async () => { (localStorage.getItem as jest.Mock).mockImplementation((key) => { if (key === SHARED_PREFS_EVENT_LIST_KEY) { return JSON.stringify([eventDataMatched]); @@ -247,7 +247,7 @@ describe('UserMergeScenariosTests', () => { } catch (e) { console.log(''); } - await setUserID('testuser123', false); + await setUserID('testuser123', true); expect(localStorageMock.removeItem).toHaveBeenCalledWith( SHARED_PREF_ANON_USER_ID ); @@ -262,7 +262,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('criteria is met with merge true with setUserId', async () => { + it('criteria is met with disableEventReplay false with setUserId', async () => { (localStorage.getItem as jest.Mock).mockImplementation((key) => { if (key === SHARED_PREFS_EVENT_LIST_KEY) { return JSON.stringify([eventDataMatched]); @@ -288,7 +288,7 @@ describe('UserMergeScenariosTests', () => { count: 10, packageName: 'my-lil-website' }); - await setUserID('testuser123', true); + await setUserID('testuser123', false); expect(localStorageMock.removeItem).toHaveBeenCalledWith( SHARED_PREF_ANON_USER_ID ); @@ -302,7 +302,7 @@ describe('UserMergeScenariosTests', () => { jest.runAllTimers(); }); - it('criteria is met with merge default with setUserId', async () => { + it('criteria is met with disableEventReplay default with setUserId', async () => { (localStorage.getItem as jest.Mock).mockImplementation((key) => { if (key === SHARED_PREFS_EVENT_LIST_KEY) { return JSON.stringify([eventDataMatched]); @@ -343,7 +343,7 @@ describe('UserMergeScenariosTests', () => { jest.runAllTimers(); }); - it('current user identified with setUserId merge false', async () => { + it('current user identified with setUserId disableEventReplay true', async () => { const { setUserID, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -363,7 +363,7 @@ describe('UserMergeScenariosTests', () => { expect(localStorageMock.setItem).not.toHaveBeenCalledWith( SHARED_PREF_ANON_USER_ID ); - await setUserID('testuseranotheruser', false); + await setUserID('testuseranotheruser', true); const secondResponse = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -374,7 +374,8 @@ describe('UserMergeScenariosTests', () => { ); expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('current user identified with setUserId merge true', async () => { + + it('current user identified with setUserId disableEventReplay false', async () => { const { setUserID, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -389,7 +390,7 @@ describe('UserMergeScenariosTests', () => { expect(localStorageMock.setItem).not.toHaveBeenCalledWith( SHARED_PREF_ANON_USER_ID ); - await setUserID('testuseranotheruser', true); + await setUserID('testuseranotheruser', false); const secondResponse = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -400,7 +401,8 @@ describe('UserMergeScenariosTests', () => { ); expect(mergePostRequestData).toBeDefined(); // ensure that merge API gets called }); - it('current user identified with setUserId merge default', async () => { + + it('current user identified with setUserId disableEventReplay default', async () => { const { setUserID, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -424,12 +426,12 @@ describe('UserMergeScenariosTests', () => { const mergePostRequestData = mockRequest.history.post.find( (req) => req.url === ENDPOINT_MERGE_USER ); - expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called + expect(mergePostRequestData).toBeDefined(); // ensure that merge API gets called }); }); describe('UserMergeScenariosTests with setEmail', () => { - it('criteria not met with merge false with setEmail', async () => { + it('criteria not met with disableEventReplay true with setEmail', async () => { const { setEmail, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -444,7 +446,7 @@ describe('UserMergeScenariosTests', () => { SHARED_PREFS_EVENT_LIST_KEY, expect.any(String) ); - await setEmail('testuser123@test.com', false); + await setEmail('testuser123@test.com', true); const response = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -462,7 +464,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('criteria not met with merge true with setEmail', async () => { + it('criteria not met with disableEventReplay false with setEmail', async () => { const { setEmail, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -477,7 +479,7 @@ describe('UserMergeScenariosTests', () => { SHARED_PREFS_EVENT_LIST_KEY, expect.any(String) ); - await setEmail('testuser123@test.com', true); + await setEmail('testuser123@test.com', false); const response = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -495,7 +497,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('criteria not met with merge default value with setEmail', async () => { + it('criteria not met with disableEventReplay default value with setEmail', async () => { const { setEmail, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -528,7 +530,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('criteria is met with merge true with setEmail', async () => { + it('criteria is met with disableEventReplay false with setEmail', async () => { (localStorage.getItem as jest.Mock).mockImplementation((key) => { if (key === SHARED_PREFS_EVENT_LIST_KEY) { return JSON.stringify([eventDataMatched]); @@ -553,7 +555,7 @@ describe('UserMergeScenariosTests', () => { count: 10, packageName: 'my-lil-website' }); - await setEmail('testuser123@test.com', true); + await setEmail('testuser123@test.com', false); expect(localStorageMock.removeItem).toHaveBeenCalledWith( SHARED_PREF_ANON_USER_ID ); @@ -567,7 +569,7 @@ describe('UserMergeScenariosTests', () => { jest.runAllTimers(); }); - it('criteria is met with merge default with setEmail', async () => { + it('criteria is met with disableEventReplay default with setEmail', async () => { (localStorage.getItem as jest.Mock).mockImplementation((key) => { if (key === SHARED_PREFS_EVENT_LIST_KEY) { return JSON.stringify([eventDataMatched]); @@ -608,7 +610,7 @@ describe('UserMergeScenariosTests', () => { jest.runAllTimers(); }); - it('current user identified with setEmail with merge false', async () => { + it('current user identified with setEmail with disableEventReplay true', async () => { const { setEmail, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -628,7 +630,7 @@ describe('UserMergeScenariosTests', () => { expect(localStorageMock.setItem).not.toHaveBeenCalledWith( SHARED_PREF_ANON_USER_ID ); - await setEmail('testuseranotheruser@test.com', false); + await setEmail('testuseranotheruser@test.com', true); const secondResponse = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -642,7 +644,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called }); - it('current user identified with setEmail merge true', async () => { + it('current user identified with setEmail disableEventReplay false', async () => { const { setEmail, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -662,7 +664,7 @@ describe('UserMergeScenariosTests', () => { expect(localStorageMock.setItem).not.toHaveBeenCalledWith( SHARED_PREF_ANON_USER_ID ); - await setEmail('testuseranotheruser@test.com', true); + await setEmail('testuseranotheruser@test.com', false); const secondResponse = await getInAppMessages({ count: 10, packageName: 'my-lil-website' @@ -676,7 +678,7 @@ describe('UserMergeScenariosTests', () => { expect(mergePostRequestData).toBeDefined(); // ensure that merge API gets called }); - it('current user identified with setEmail merge default', async () => { + it('current user identified with setEmail disableEventReplay default', async () => { const { setEmail, logout } = initializeWithConfig({ authToken: '123', configOptions: { enableAnonTracking: true } @@ -707,7 +709,7 @@ describe('UserMergeScenariosTests', () => { const mergePostRequestData = mockRequest.history.post.find( (req) => req.url === ENDPOINT_MERGE_USER ); - expect(mergePostRequestData).toBeUndefined(); // ensure that merge API Do NOT get called + expect(mergePostRequestData).toBeDefined(); // ensure that merge API gets called }); }); }); diff --git a/src/authorization/authorization.ts b/src/authorization/authorization.ts index 3fd16897..e7ca34a6 100644 --- a/src/authorization/authorization.ts +++ b/src/authorization/authorization.ts @@ -62,8 +62,8 @@ const doesRequestUrlContain = (routeConfig: RouteConfig) => ); export interface WithJWT { clearRefresh: () => void; - setEmail: (email: string, merge?: boolean) => Promise; - setUserID: (userId: string, merge?: boolean) => Promise; + setEmail: (email: string, disableEventReplay?: boolean) => Promise; + setUserID: (userId: string, disableEventReplay?: boolean) => Promise; logout: () => void; refreshJwtToken: (authTypes: string) => Promise; } @@ -71,8 +71,8 @@ export interface WithJWT { export interface WithoutJWT { setNewAuthToken: (newToken?: string) => void; clearAuthToken: () => void; - setEmail: (email: string, merge?: boolean) => Promise; - setUserID: (userId: string, merge?: boolean) => Promise; + setEmail: (email: string, disableEventReplay?: boolean) => Promise; + setUserID: (userId: string, disableEventReplay?: boolean) => Promise; logout: () => void; } @@ -108,10 +108,13 @@ const getAnonUserId = () => { } }; -const initializeUserIdAndSync = (userId: string, merge: boolean) => { +const initializeUserIdAndSync = ( + userId: string, + disableEventReplay?: boolean +) => { addUserIdToRequest(userId); clearAnonymousUser(); - if (merge) { + if (!disableEventReplay) { syncEvents(); } }; @@ -211,10 +214,13 @@ const addUserIdToRequest = (userId: string) => { }); }; -const initializeEmailUserAndSync = (email: string, merge: boolean) => { +const initializeEmailUserAndSync = ( + email: string, + disableEventReplay?: boolean +) => { addEmailToRequest(email); clearAnonymousUser(); - if (merge) { + if (!disableEventReplay) { syncEvents(); } }; @@ -417,28 +423,12 @@ export function initialize( } }; - const getMergeDefaultValue = (merge?: boolean) => { - const doesAnonUserExist = getAnonUserId() === null; - if (merge === undefined) { - if ( - (authIdentifier === null && typeOfAuth === null && doesAnonUserExist) || // Criteria is not yet met (default merge is true) - (authIdentifier !== null && typeOfAuth !== null && !doesAnonUserExist) - ) { - // // Criteria is met (Iterable profile created with an autogenerated identity)(default merge is true) - return true; - } else { - return false; // Current logged in user is identified (default merge is false) - } - } else { - return merge; - } - }; - const tryMergeUser = async ( emailOrUserId: string, isEmail: boolean, - merge: boolean + disableEventReplay?: boolean ): Promise => { + const enableAnonTracking = config.getConfig('enableAnonTracking'); const sourceUserIdOrEmail = authIdentifier === null ? getAnonUserId() : authIdentifier; const sourceUserId = typeOfAuth === 'email' ? null : sourceUserIdOrEmail; @@ -446,7 +436,11 @@ export function initialize( const destinationUserId = isEmail ? null : emailOrUserId; const destinationEmail = isEmail ? emailOrUserId : null; // This function will try to merge if anon user exists - if ((getAnonUserId() !== null || authIdentifier !== null) && merge) { + if ( + (getAnonUserId() !== null || authIdentifier !== null) && + !disableEventReplay && + enableAnonTracking + ) { const anonymousUserMerge = new AnonymousUserMerge(); try { await anonymousUserMerge.mergeUser( @@ -485,13 +479,12 @@ export function initialize( baseAxiosRequest.interceptors.request.eject(authInterceptor); } }, - setEmail: async (email: string, merge?: boolean) => { + setEmail: async (email: string, disableEventReplay?: boolean) => { clearMessages(); try { - merge = getMergeDefaultValue(merge); - const result = await tryMergeUser(email, true, merge); + const result = await tryMergeUser(email, true, disableEventReplay); if (result) { - initializeEmailUserAndSync(email, merge); + initializeEmailUserAndSync(email, disableEventReplay); return Promise.resolve(); } } catch (error) { @@ -499,13 +492,12 @@ export function initialize( return Promise.reject(`merging failed: ${error}`); } }, - setUserID: async (userId: string, merge?: boolean) => { + setUserID: async (userId: string, disableEventReplay?: boolean) => { clearMessages(); try { - merge = getMergeDefaultValue(merge); - const result = await tryMergeUser(userId, false, merge); + const result = await tryMergeUser(userId, false, disableEventReplay); if (result) { - initializeUserIdAndSync(userId, merge); + initializeUserIdAndSync(userId, disableEventReplay); return Promise.resolve(); } } catch (error) { @@ -791,14 +783,13 @@ export function initialize( /* this will just clear the existing timeout */ handleTokenExpiration(''); }, - setEmail: async (email: string, merge?: boolean) => { + setEmail: async (email: string, disableEventReplay?: boolean) => { /* clear previous user */ clearMessages(); try { - merge = getMergeDefaultValue(merge); - const result = await tryMergeUser(email, true, merge); + const result = await tryMergeUser(email, true, disableEventReplay); if (result) { - initializeEmailUserAndSync(email, merge); + initializeEmailUserAndSync(email, disableEventReplay); try { return doRequest({ email }).catch((e) => { if (logLevel === 'verbose') { @@ -818,13 +809,12 @@ export function initialize( return Promise.reject(`merging failed: ${error}`); } }, - setUserID: async (userId: string, merge?: boolean) => { + setUserID: async (userId: string, disableEventReplay?: boolean) => { clearMessages(); try { - merge = getMergeDefaultValue(merge); - const result = await tryMergeUser(userId, false, merge); + const result = await tryMergeUser(userId, false, disableEventReplay); if (result) { - initializeUserIdAndSync(userId, merge); + initializeUserIdAndSync(userId, disableEventReplay); try { return doRequest({ userID: userId }) .then(async (token) => {