From 86dd9f47a2239b647eb22371849b90aa396aba9f Mon Sep 17 00:00:00 2001 From: Alex S <49695018+alexs-mparticle@users.noreply.github.com> Date: Wed, 20 Dec 2023 15:34:02 -0500 Subject: [PATCH] fix: Replace identify method strings with constants (#817) --- src/constants.ts | 6 ++++++ src/forwarders.js | 12 ++++++++---- src/identity.js | 13 ++++++++----- src/identityApiClient.js | 4 +++- src/validators.ts | 12 ++++++++++-- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index e49d5e2c..94f451db 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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; diff --git a/src/forwarders.js b/src/forwarders.js index 03831068..3a7362fb 100644 --- a/src/forwarders.js +++ b/src/forwarders.js @@ -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; @@ -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( @@ -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) { diff --git a/src/identity.js b/src/identity.js index 8d684523..e53569a0 100644 --- a/src/identity.js +++ b/src/identity.js @@ -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( @@ -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 @@ -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() ) { @@ -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 ); @@ -1628,8 +1632,7 @@ export default function Identity(mpInstance) { method ); mpInstance._Forwarders.setForwarderOnUserIdentified( - newUser, - method + newUser ); } var newIdentitiesByName = {}; @@ -1646,7 +1649,7 @@ export default function Identity(mpInstance) { newIdentitiesByName, method, identityApiResult.mpid, - method === 'modify' + method === Modify ? previousUIByNameCopy : incomingMpidUIByNameCopy ); diff --git a/src/identityApiClient.js b/src/identityApiClient.js index 1eed6490..8d07b3fd 100644 --- a/src/identityApiClient.js +++ b/src/identityApiClient.js @@ -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, @@ -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( diff --git a/src/validators.ts b/src/validators.ts index 28e5055b..e0fccf7a 100644 --- a/src/validators.ts +++ b/src/validators.ts @@ -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'; @@ -10,6 +16,8 @@ type ValidationIdentitiesReturn = { error?: valueof; }; +const { Modify } = Constants.IdentityMethods; + const Validators = { // From ./utils // Utility Functions for backwards compatability @@ -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) ||