-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor ParseIdentityResponse Workflow (#888)
- Loading branch information
1 parent
95a1dfe
commit 7dc11fd
Showing
10 changed files
with
502 additions
and
228 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { IdentityType } from './types.interfaces'; | ||
import { parseNumber } from './utils'; | ||
|
||
export interface IIdentitiesByType { | ||
[key: number]: string; | ||
} | ||
|
||
export function getNewIdentitiesByName(newIdentitiesByType: IIdentitiesByType) { | ||
const newIdentitiesByName = {}; | ||
|
||
for (var key in newIdentitiesByType) { | ||
const identityNameKey = getIdentityName(parseNumber(key)); | ||
newIdentitiesByName[identityNameKey] = newIdentitiesByType[key]; | ||
} | ||
|
||
return newIdentitiesByName; | ||
} | ||
|
||
export function getIdentityName(identityType: IdentityType): string | null { | ||
switch (identityType) { | ||
case IdentityType.Other: | ||
return 'other'; | ||
case IdentityType.CustomerId: | ||
return 'customerid'; | ||
case IdentityType.Facebook: | ||
return 'facebook'; | ||
case IdentityType.Twitter: | ||
return 'twitter'; | ||
case IdentityType.Google: | ||
return 'google'; | ||
case IdentityType.Microsoft: | ||
return 'microsoft'; | ||
case IdentityType.Yahoo: | ||
return 'yahoo'; | ||
case IdentityType.Email: | ||
return 'email'; | ||
case IdentityType.FacebookCustomAudienceId: | ||
return 'facebookcustomaudienceid'; | ||
case IdentityType.Other2: | ||
return 'other2'; | ||
case IdentityType.Other3: | ||
return 'other3'; | ||
case IdentityType.Other4: | ||
return 'other4'; | ||
case IdentityType.Other5: | ||
return 'other5'; | ||
case IdentityType.Other6: | ||
return 'other6'; | ||
case IdentityType.Other7: | ||
return 'other7'; | ||
case IdentityType.Other8: | ||
return 'other8'; | ||
case IdentityType.Other9: | ||
return 'other9'; | ||
case IdentityType.Other10: | ||
return 'other10'; | ||
case IdentityType.MobileNumber: | ||
return 'mobile_number'; | ||
case IdentityType.PhoneNumber2: | ||
return 'phone_number_2'; | ||
case IdentityType.PhoneNumber3: | ||
return 'phone_number_3'; | ||
default: | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { IMParticleUser, IdentityResultBody } from './identity-user-interfaces'; | ||
|
||
export function hasMPIDAndUserLoginChanged( | ||
previousUser: IMParticleUser, | ||
newUser: IMParticleUser | ||
): boolean { | ||
return ( | ||
!previousUser || | ||
newUser.getMPID() !== previousUser.getMPID() || | ||
previousUser.isLoggedIn() !== newUser.isLoggedIn() | ||
); | ||
} | ||
|
||
// https://go.mparticle.com/work/SQDSDKS-6504 | ||
export function hasMPIDChanged( | ||
prevUser: IMParticleUser, | ||
identityApiResult: IdentityResultBody | ||
): boolean { | ||
return ( | ||
!prevUser || | ||
(prevUser.getMPID() && | ||
identityApiResult.mpid && | ||
identityApiResult.mpid !== prevUser.getMPID()) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getNewIdentitiesByName } from '../../src/type-utils'; | ||
import { IdentityType } from '../../src/types.interfaces'; | ||
|
||
describe('getNewIdentitesByName', () => { | ||
it('returns an identity name when passing an identity type', () => { | ||
const { Email, CustomerId } = IdentityType; | ||
|
||
const newIdentitiesByType = { | ||
[CustomerId]: 'foo', | ||
[Email]: '[email protected]', | ||
}; | ||
|
||
expect(getNewIdentitiesByName(newIdentitiesByType)).toEqual({ | ||
customerid: 'foo', | ||
email: '[email protected]', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { | ||
IMParticleUser, | ||
IdentityModifyResultBody, | ||
IdentityResultBody, | ||
} from '../../src/identity-user-interfaces'; | ||
import { SDKIdentityTypeEnum } from '../../src/identity.interfaces'; | ||
import { | ||
hasMPIDAndUserLoginChanged, | ||
hasMPIDChanged, | ||
} from '../../src/user-utils'; | ||
|
||
describe('user-utils', () => { | ||
describe('#didUserChange', () => { | ||
it('returns true if previousUser is null', () => { | ||
expect(hasMPIDAndUserLoginChanged(null, null)).toBeTruthy(); | ||
}); | ||
|
||
it('returns true if previousUser and newUser have different mpids', () => { | ||
const previousUser = ({ | ||
getMPID: () => '123', | ||
} as unknown) as IMParticleUser; | ||
|
||
const newUser = ({ | ||
getMPID: () => '456', | ||
} as unknown) as IMParticleUser; | ||
|
||
expect( | ||
hasMPIDAndUserLoginChanged(previousUser, newUser) | ||
).toBeTruthy(); | ||
}); | ||
|
||
it('returns false if previousUser and newUser have the same MPID', () => { | ||
const previousUser = ({ | ||
getMPID: () => '123', | ||
isLoggedIn: () => false, | ||
} as unknown) as IMParticleUser; | ||
|
||
const newUser = ({ | ||
getMPID: () => '123', | ||
isLoggedIn: () => false, | ||
} as unknown) as IMParticleUser; | ||
|
||
expect( | ||
hasMPIDAndUserLoginChanged(previousUser, newUser) | ||
).toBeFalsy(); | ||
}); | ||
|
||
it('returns true if previousUser and newUser have the same MPID but different login states', () => { | ||
const previousUser = ({ | ||
getMPID: () => '123', | ||
isLoggedIn: () => true, | ||
} as unknown) as IMParticleUser; | ||
|
||
const newUser = ({ | ||
getMPID: () => '123', | ||
isLoggedIn: () => false, | ||
} as unknown) as IMParticleUser; | ||
|
||
expect( | ||
hasMPIDAndUserLoginChanged(previousUser, newUser) | ||
).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('#hasMPIDChanged', () => { | ||
const identityResultBody: IdentityResultBody = { | ||
context: null, | ||
is_ephemeral: false, | ||
is_logged_in: false, | ||
matched_identities: { | ||
device_application_stamp: 'test-das', | ||
}, | ||
mpid: '123', | ||
}; | ||
|
||
it('returns true if prevUser is null', () => { | ||
expect(hasMPIDChanged(null, identityResultBody)).toBeTruthy(); | ||
}); | ||
|
||
it('returns false if prevUser has an MPID and the new MPID is null or undefined', () => { | ||
const prevUser = ({ | ||
getMPID: () => '123', | ||
} as unknown) as IMParticleUser; | ||
|
||
expect( | ||
hasMPIDChanged(prevUser, { ...identityResultBody, mpid: null }) | ||
).toBeFalsy(); | ||
|
||
expect( | ||
hasMPIDChanged(prevUser, { | ||
...identityResultBody, | ||
mpid: undefined, | ||
}) | ||
).toBeFalsy(); | ||
}); | ||
|
||
it('returns false if prevUser has an MPID and the identity result is empty', () => { | ||
const prevUser = ({ | ||
getMPID: () => '123', | ||
} as unknown) as IMParticleUser; | ||
|
||
expect( | ||
hasMPIDChanged(prevUser, {} as IdentityResultBody) | ||
).toBeFalsy(); | ||
}); | ||
|
||
it('returns true if prevUser has an MPID and the new MPID is different', () => { | ||
const prevUser = ({ | ||
getMPID: () => '456', | ||
} as unknown) as IMParticleUser; | ||
|
||
expect(hasMPIDChanged(prevUser, identityResultBody)).toBeTruthy(); | ||
}); | ||
|
||
it('returns false if prevUser has an MPID and the new MPID is the same', () => { | ||
const prevUser = ({ | ||
getMPID: () => '123', | ||
} as unknown) as IMParticleUser; | ||
|
||
expect(hasMPIDChanged(prevUser, identityResultBody)).toBeFalsy(); | ||
}); | ||
|
||
it('returns false if prevUser has an MPID but the result is a modify result', () => { | ||
const prevUser = ({ | ||
getMPID: () => '123', | ||
} as unknown) as IMParticleUser; | ||
|
||
const modifyResults: IdentityModifyResultBody = { | ||
change_results: { | ||
identity_type: SDKIdentityTypeEnum.email, | ||
modified_mpid: '123', | ||
}, | ||
}; | ||
expect( | ||
hasMPIDChanged(prevUser, modifyResults as IdentityResultBody) | ||
).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.