Skip to content

Commit

Permalink
fix: Replace identify method strings with constants (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle authored Dec 20, 2023
1 parent 20a2e6a commit 86dd9f4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ const Constants = {
},
DefaultInstance: 'default_instance',
CCPAPurpose: 'data_sale_opt_out',
IdentityMethods: {
Modify: 'modify',
Logout: 'logout',
Login: 'login',
Identify: 'identify',
},
} as const;

export default Constants;
12 changes: 8 additions & 4 deletions src/forwarders.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import Types from './types';
import filteredMparticleUser from './filteredMparticleUser';
import { isEmpty } from './utils';
import KitFilterHelper from './kitFilterHelper';
import Constants from './constants';

const { Modify, Identify, Login, Logout } = Constants.IdentityMethods;

export default function Forwarders(mpInstance, kitBlocker) {
var self = this;
Expand Down Expand Up @@ -440,6 +443,7 @@ export default function Forwarders(mpInstance, kitBlocker) {
});
};

// TODO: https://go.mparticle.com/work/SQDSDKS-6036
this.setForwarderUserIdentities = function(userIdentities) {
mpInstance._Store.activeForwarders.forEach(function(forwarder) {
var filteredUserIdentities = mpInstance._Helpers.filterUserIdentities(
Expand Down Expand Up @@ -487,28 +491,28 @@ export default function Forwarders(mpInstance, kitBlocker) {
mpInstance,
kitBlocker
);
if (identityMethod === 'identify') {
if (identityMethod === Identify) {
if (forwarder.onIdentifyComplete) {
result = forwarder.onIdentifyComplete(filteredUser);
if (result) {
mpInstance.Logger.verbose(result);
}
}
} else if (identityMethod === 'login') {
} else if (identityMethod === Login) {
if (forwarder.onLoginComplete) {
result = forwarder.onLoginComplete(filteredUser);
if (result) {
mpInstance.Logger.verbose(result);
}
}
} else if (identityMethod === 'logout') {
} else if (identityMethod === Logout) {
if (forwarder.onLogoutComplete) {
result = forwarder.onLogoutComplete(filteredUser);
if (result) {
mpInstance.Logger.verbose(result);
}
}
} else if (identityMethod === 'modify') {
} else if (identityMethod === Modify) {
if (forwarder.onModifyComplete) {
result = forwarder.onModifyComplete(filteredUser);
if (result) {
Expand Down
13 changes: 8 additions & 5 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Types from './types';
var Messages = Constants.Messages,
HTTPCodes = Constants.HTTPCodes;

const { Identify, Modify } = Constants.IdentityMethods;

export default function Identity(mpInstance) {
var self = this;
this.checkIdentitySwap = function(
Expand Down Expand Up @@ -1476,7 +1478,7 @@ export default function Identity(mpInstance) {
}

if (xhr.status === 200) {
if (method === 'modify') {
if (method === Modify) {
newIdentitiesByType = mpInstance._Identity.IdentityRequest.combineUserIdentities(
previousUIByName,
identityApiData.userIdentities
Expand Down Expand Up @@ -1507,7 +1509,7 @@ export default function Identity(mpInstance) {
//will not have a value for "fst" until the current MPID changes, and in some cases,
//the current MPID will never change
if (
method === 'identify' &&
method === Identify &&
prevUser &&
identityApiResult.mpid === prevUser.getMPID()
) {
Expand Down Expand Up @@ -1620,6 +1622,8 @@ export default function Identity(mpInstance) {
mpInstance._APIClient.prepareForwardingStats
);
}

// TODO: https://go.mparticle.com/work/SQDSDKS-6036
mpInstance._Forwarders.setForwarderUserIdentities(
newUser.getUserIdentities().userIdentities
);
Expand All @@ -1628,8 +1632,7 @@ export default function Identity(mpInstance) {
method
);
mpInstance._Forwarders.setForwarderOnUserIdentified(
newUser,
method
newUser
);
}
var newIdentitiesByName = {};
Expand All @@ -1646,7 +1649,7 @@ export default function Identity(mpInstance) {
newIdentitiesByName,
method,
identityApiResult.mpid,
method === 'modify'
method === Modify
? previousUIByNameCopy
: incomingMpidUIByNameCopy
);
Expand Down
4 changes: 3 additions & 1 deletion src/identityApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Constants from './constants';
var HTTPCodes = Constants.HTTPCodes,
Messages = Constants.Messages;

const { Modify } = Constants.IdentityMethods;

export default function IdentityAPIClient(mpInstance) {
this.sendAliasRequest = function(aliasRequest, callback) {
var xhr,
Expand Down Expand Up @@ -107,7 +109,7 @@ export default function IdentityAPIClient(mpInstance) {
);
} else {
previousMPID = mpid || null;
if (method === 'modify') {
if (method === Modify) {
xhr.open(
'post',
mpInstance._Helpers.createServiceUrl(
Expand Down
12 changes: 10 additions & 2 deletions src/validators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Types from './types';
import { isFunction, isNumber, isObject, isStringOrNumber, valueof } from './utils';
import {
isFunction,
isNumber,
isObject,
isStringOrNumber,
valueof,
} from './utils';
import Constants from './constants';
import { IdentityApiData } from '@mparticle/web-sdk';

Expand All @@ -10,6 +16,8 @@ type ValidationIdentitiesReturn = {
error?: valueof<typeof Constants.Messages.ValidationMessages>;
};

const { Modify } = Constants.IdentityMethods;

const Validators = {
// From ./utils
// Utility Functions for backwards compatability
Expand Down Expand Up @@ -43,7 +51,7 @@ const Validators = {
copyUserAttributes: 1,
};
if (identityApiData) {
if (method === 'modify') {
if (method === Modify) {
if (
(isObject(identityApiData.userIdentities) &&
!Object.keys(identityApiData.userIdentities).length) ||
Expand Down

0 comments on commit 86dd9f4

Please sign in to comment.