Skip to content

Commit

Permalink
refactor: Refactor ParseIdentityResponse Workflow (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle authored May 28, 2024
1 parent 95a1dfe commit 7dc11fd
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 228 deletions.
326 changes: 142 additions & 184 deletions src/identity.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
isEmpty,
isNumber,
isObject,
moveElementToEnd,
parseNumber,
returnConvertedBoolean,
} from './utils';
Expand Down Expand Up @@ -186,6 +187,7 @@ export interface IStore {
getLastSeenTime?(mpid: MPID): number;
setLastSeenTime?(mpid: MPID, time?: number): void;

addMpidToSessionHistory?(mpid: MPID, previousMpid?: MPID): void;
hasInvalidIdentifyRequest?: () => boolean;
nullifySession?: () => void;
processConfig(config: SDKInitConfig): void;
Expand Down Expand Up @@ -494,6 +496,22 @@ export default function Store(
mpInstance._Persistence.update();
};

this.addMpidToSessionHistory = (mpid: MPID, previousMPID?: MPID): void => {
const indexOfMPID = this.currentSessionMPIDs.indexOf(mpid);

if (mpid && previousMPID !== mpid && indexOfMPID < 0) {
this.currentSessionMPIDs.push(mpid);
return;
}

if (indexOfMPID >= 0) {
this.currentSessionMPIDs = moveElementToEnd(
this.currentSessionMPIDs,
indexOfMPID
);
}
};

this.getFirstSeenTime = (mpid: MPID) => {
if (!mpid) {
return null;
Expand Down
66 changes: 66 additions & 0 deletions src/type-utils.ts
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;
}
}
47 changes: 3 additions & 44 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getIdentityName } from './type-utils';

var MessageType = {
SessionStart: 1,
SessionEnd: 2,
Expand Down Expand Up @@ -209,50 +211,7 @@ IdentityType.getIdentityType = function(identityName) {
};

IdentityType.getIdentityName = function(identityType) {
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';
}
return getIdentityName(identityType);
};

var ProductActionType = {
Expand Down
25 changes: 25 additions & 0 deletions src/user-utils.ts
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())
);
}
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ const mergeObjects = <T extends object>(...objects: T[]): T => {
return Object.assign({}, ...objects);
};

const moveElementToEnd = <T>(array: T[], index: number): T[] =>
array.slice(0, index).concat(array.slice(index + 1), array[index]);

export {
createCookieString,
revertCookieString,
Expand Down Expand Up @@ -258,4 +261,5 @@ export {
isEmpty,
isValidCustomFlagProperty,
mergeObjects,
moveElementToEnd,
};
18 changes: 18 additions & 0 deletions test/jest/type-utils.spec.ts
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]',
});
});
});
139 changes: 139 additions & 0 deletions test/jest/user-utils.spec.ts
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();
});
});
});
Loading

0 comments on commit 7dc11fd

Please sign in to comment.