Skip to content

Commit 4a25339

Browse files
authored
refactor: SDKE 283 remove cart related function from persistence (#1073)
1 parent 495c955 commit 4a25339

22 files changed

+86
-1013
lines changed

src/constants.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const Constants = {
66
platform: 'web',
77
Messages: {
88
DeprecationMessages: {
9-
MethodIsDeprecatedPostfix:
10-
'is a deprecated method and will be removed in future releases',
9+
MethodHasBeenDeprecated: 'has been deprecated.',
10+
MethodMarkedForDeprecationPostfix:
11+
'is a deprecated method and will be removed in future releases.',
1112
AlternativeMethodPrefix: 'Please use the alternate method:',
1213
},
1314
ErrorMessages: {
@@ -232,11 +233,10 @@ export const HTTP_SERVER_ERROR = 500 as const;
232233

233234
export type PrivacyControl = 'functional' | 'targeting';
234235

235-
export type StorageTypes = 'SDKState' | 'Products' | 'OfflineEvents' | 'IdentityCache' | 'TimeOnSite';
236+
export type StorageTypes = 'SDKState' | 'OfflineEvents' | 'IdentityCache' | 'TimeOnSite';
236237

237238
export const StoragePrivacyMap: Record<StorageTypes, PrivacyControl> = {
238239
SDKState: 'functional',
239-
Products: 'targeting',
240240
OfflineEvents: 'functional',
241241
IdentityCache: 'functional',
242242
TimeOnSite: 'targeting',

src/helpers.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,6 @@ export default function Helpers(mpInstance) {
376376
}
377377
};
378378

379-
this.createProductStorageName = function(workspaceToken) {
380-
if (workspaceToken) {
381-
return (
382-
StorageNames.currentStorageProductsName + '_' + workspaceToken
383-
);
384-
} else {
385-
return StorageNames.currentStorageProductsName;
386-
}
387-
};
388-
389379
// TODO: Refactor SDK to directly use these methods
390380
// https://go.mparticle.com/work/SQDSDKS-5239
391381
// Utility Functions

src/identity-user-interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ export interface IdentityModifyResultBody {
115115
}
116116

117117
export interface mParticleUserCart {
118-
add(product: SDKProduct | SDKProduct[], logEvent: boolean): void;
119-
remove(product: SDKProduct | SDKProduct[], logEvent: boolean): void;
118+
add(): void;
119+
remove(): void;
120120
clear(): void;
121121
getCartProducts(): SDKProduct[];
122122
}

src/identity.interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,5 @@ export interface IIdentity {
206206
/**
207207
* @deprecated
208208
*/
209-
mParticleUserCart(mpid: MPID): mParticleUserCart;
209+
mParticleUserCart(): mParticleUserCart;
210210
}

src/identity.js

Lines changed: 29 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ export default function Identity(mpInstance) {
12151215
mpInstance.Logger.warning(
12161216
'Deprecated function Identity.getCurrentUser().getCart() will be removed in future releases'
12171217
);
1218-
return self.mParticleUserCart(mpid);
1218+
return self.mParticleUserCart();
12191219
},
12201220

12211221
/**
@@ -1286,133 +1286,37 @@ export default function Identity(mpInstance) {
12861286
* @class mParticle.Identity.getCurrentUser().getCart()
12871287
* @deprecated
12881288
*/
1289-
this.mParticleUserCart = function(mpid) {
1289+
this.mParticleUserCart = function() {
12901290
return {
12911291
/**
12921292
* Adds a cart product to the user cart
12931293
* @method add
1294-
* @param {Object} product the product
1295-
* @param {Boolean} [logEvent] a boolean to log adding of the cart object. If blank, no logging occurs.
12961294
* @deprecated
12971295
*/
1298-
add: function(product, logEvent) {
1296+
add: function() {
12991297
mpInstance.Logger.warning(
1300-
'Deprecated function Identity.getCurrentUser().getCart().add() will be removed in future releases'
1298+
generateDeprecationMessage(
1299+
'Identity.getCurrentUser().getCart().add()',
1300+
true,
1301+
'eCommerce.logProductAction()',
1302+
'https://docs.mparticle.com/developers/sdk/web/commerce-tracking'
1303+
)
13011304
);
1302-
var allProducts, userProducts, arrayCopy;
1303-
1304-
arrayCopy = Array.isArray(product)
1305-
? product.slice()
1306-
: [product];
1307-
arrayCopy.forEach(function(product) {
1308-
product.Attributes = mpInstance._Helpers.sanitizeAttributes(
1309-
product.Attributes
1310-
);
1311-
});
1312-
1313-
if (mpInstance._Store.webviewBridgeEnabled) {
1314-
mpInstance._NativeSdkHelpers.sendToNative(
1315-
Constants.NativeSdkPaths.AddToCart,
1316-
JSON.stringify(arrayCopy)
1317-
);
1318-
} else {
1319-
mpInstance._SessionManager.resetSessionTimer();
1320-
1321-
userProducts = mpInstance._Persistence.getUserProductsFromLS(
1322-
mpid
1323-
);
1324-
1325-
userProducts = userProducts.concat(arrayCopy);
1326-
1327-
if (logEvent === true) {
1328-
mpInstance._Events.logProductActionEvent(
1329-
Types.ProductActionType.AddToCart,
1330-
arrayCopy
1331-
);
1332-
}
1333-
1334-
var productsForMemory = {};
1335-
productsForMemory[mpid] = { cp: userProducts };
1336-
1337-
if (
1338-
userProducts.length >
1339-
mpInstance._Store.SDKConfig.maxProducts
1340-
) {
1341-
mpInstance.Logger.verbose(
1342-
'The cart contains ' +
1343-
userProducts.length +
1344-
' items. Only ' +
1345-
mpInstance._Store.SDKConfig.maxProducts +
1346-
' can currently be saved in cookies.'
1347-
);
1348-
userProducts = userProducts.slice(
1349-
-mpInstance._Store.SDKConfig.maxProducts
1350-
);
1351-
}
1352-
1353-
allProducts = mpInstance._Persistence.getAllUserProductsFromLS();
1354-
allProducts[mpid].cp = userProducts;
1355-
1356-
mpInstance._Persistence.setCartProducts(allProducts);
1357-
}
13581305
},
13591306
/**
13601307
* Removes a cart product from the current user cart
13611308
* @method remove
1362-
* @param {Object} product the product
1363-
* @param {Boolean} [logEvent] a boolean to log adding of the cart object. If blank, no logging occurs.
13641309
* @deprecated
13651310
*/
1366-
remove: function(product, logEvent) {
1311+
remove: function() {
13671312
mpInstance.Logger.warning(
1368-
'Deprecated function Identity.getCurrentUser().getCart().remove() will be removed in future releases'
1313+
generateDeprecationMessage(
1314+
'Identity.getCurrentUser().getCart().remove()',
1315+
true,
1316+
'eCommerce.logProductAction()',
1317+
'https://docs.mparticle.com/developers/sdk/web/commerce-tracking'
1318+
)
13691319
);
1370-
var allProducts,
1371-
userProducts,
1372-
cartIndex = -1,
1373-
cartItem = null;
1374-
1375-
if (mpInstance._Store.webviewBridgeEnabled) {
1376-
mpInstance._NativeSdkHelpers.sendToNative(
1377-
Constants.NativeSdkPaths.RemoveFromCart,
1378-
JSON.stringify(product)
1379-
);
1380-
} else {
1381-
mpInstance._SessionManager.resetSessionTimer();
1382-
1383-
userProducts = mpInstance._Persistence.getUserProductsFromLS(
1384-
mpid
1385-
);
1386-
1387-
if (userProducts) {
1388-
userProducts.forEach(function(cartProduct, i) {
1389-
if (cartProduct.Sku === product.Sku) {
1390-
cartIndex = i;
1391-
cartItem = cartProduct;
1392-
}
1393-
});
1394-
1395-
if (cartIndex > -1) {
1396-
userProducts.splice(cartIndex, 1);
1397-
1398-
if (logEvent === true) {
1399-
mpInstance._Events.logProductActionEvent(
1400-
Types.ProductActionType.RemoveFromCart,
1401-
cartItem
1402-
);
1403-
}
1404-
}
1405-
}
1406-
1407-
var productsForMemory = {};
1408-
productsForMemory[mpid] = { cp: userProducts };
1409-
1410-
allProducts = mpInstance._Persistence.getAllUserProductsFromLS();
1411-
1412-
allProducts[mpid].cp = userProducts;
1413-
1414-
mpInstance._Persistence.setCartProducts(allProducts);
1415-
}
14161320
},
14171321
/**
14181322
* Clears the user's cart
@@ -1421,31 +1325,13 @@ export default function Identity(mpInstance) {
14211325
*/
14221326
clear: function() {
14231327
mpInstance.Logger.warning(
1424-
'Deprecated function Identity.getCurrentUser().getCart().clear() will be removed in future releases'
1328+
generateDeprecationMessage(
1329+
'Identity.getCurrentUser().getCart().clear()',
1330+
true,
1331+
'',
1332+
'https://docs.mparticle.com/developers/sdk/web/commerce-tracking'
1333+
)
14251334
);
1426-
1427-
var allProducts;
1428-
1429-
if (mpInstance._Store.webviewBridgeEnabled) {
1430-
mpInstance._NativeSdkHelpers.sendToNative(
1431-
Constants.NativeSdkPaths.ClearCart
1432-
);
1433-
} else {
1434-
mpInstance._SessionManager.resetSessionTimer();
1435-
allProducts = mpInstance._Persistence.getAllUserProductsFromLS();
1436-
1437-
if (
1438-
allProducts &&
1439-
allProducts[mpid] &&
1440-
allProducts[mpid].cp
1441-
) {
1442-
allProducts[mpid].cp = [];
1443-
1444-
allProducts[mpid].cp = [];
1445-
1446-
mpInstance._Persistence.setCartProducts(allProducts);
1447-
}
1448-
}
14491335
},
14501336
/**
14511337
* Returns all cart products
@@ -1455,9 +1341,14 @@ export default function Identity(mpInstance) {
14551341
*/
14561342
getCartProducts: function() {
14571343
mpInstance.Logger.warning(
1458-
'Deprecated function Identity.getCurrentUser().getCart().getCartProducts() will be removed in future releases'
1344+
generateDeprecationMessage(
1345+
'Identity.getCurrentUser().getCart().getCartProducts()',
1346+
true,
1347+
'eCommerce.logProductAction()',
1348+
'https://docs.mparticle.com/developers/sdk/web/commerce-tracking'
1349+
)
14591350
);
1460-
return mpInstance._Persistence.getCartProducts(mpid);
1351+
return [];
14611352
},
14621353
};
14631354
};

src/mp-instance.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Consent, { IConsent } from './consent';
3636
import KitBlocker from './kitBlocking';
3737
import ConfigAPIClient, { IKitConfigs } from './configAPIClient';
3838
import IdentityAPIClient from './identityApiClient';
39-
import { isFunction, parseConfig, valueof } from './utils';
39+
import { isFunction, parseConfig, valueof, generateDeprecationMessage } from './utils';
4040
import { DisabledVault, LocalStorageVault } from './vault';
4141
import { removeExpiredIdentityCacheDates } from './identity-utils';
4242
import IntegrationCapture from './integrationCapture';
@@ -706,16 +706,13 @@ export default function mParticleInstance(this: IMParticleWebSDKInstance, instan
706706
*/
707707
add: function(product, logEventBoolean) {
708708
self.Logger.warning(
709-
'Deprecated function eCommerce.Cart.add() will be removed in future releases'
709+
generateDeprecationMessage(
710+
'eCommerce.Cart.add()',
711+
true,
712+
'eCommerce.logProductAction()',
713+
'https://docs.mparticle.com/developers/sdk/web/commerce-tracking'
714+
)
710715
);
711-
let mpid;
712-
const currentUser = self.Identity.getCurrentUser();
713-
if (currentUser) {
714-
mpid = currentUser.getMPID();
715-
}
716-
self._Identity
717-
.mParticleUserCart(mpid)
718-
.add(product, logEventBoolean);
719716
},
720717
/**
721718
* Removes a product from the cart
@@ -726,16 +723,13 @@ export default function mParticleInstance(this: IMParticleWebSDKInstance, instan
726723
*/
727724
remove: function(product, logEventBoolean) {
728725
self.Logger.warning(
729-
'Deprecated function eCommerce.Cart.remove() will be removed in future releases'
726+
generateDeprecationMessage(
727+
'eCommerce.Cart.remove()',
728+
true,
729+
'eCommerce.logProductAction()',
730+
'https://docs.mparticle.com/developers/sdk/web/commerce-tracking'
731+
)
730732
);
731-
let mpid;
732-
const currentUser = self.Identity.getCurrentUser();
733-
if (currentUser) {
734-
mpid = currentUser.getMPID();
735-
}
736-
self._Identity
737-
.mParticleUserCart(mpid)
738-
.remove(product, logEventBoolean);
739733
},
740734
/**
741735
* Clears the cart
@@ -744,14 +738,13 @@ export default function mParticleInstance(this: IMParticleWebSDKInstance, instan
744738
*/
745739
clear: function() {
746740
self.Logger.warning(
747-
'Deprecated function eCommerce.Cart.clear() will be removed in future releases'
741+
generateDeprecationMessage(
742+
'eCommerce.Cart.clear()',
743+
true,
744+
'',
745+
'https://docs.mparticle.com/developers/sdk/web/commerce-tracking'
746+
)
748747
);
749-
let mpid;
750-
const currentUser = self.Identity.getCurrentUser();
751-
if (currentUser) {
752-
mpid = currentUser.getMPID();
753-
}
754-
self._Identity.mParticleUserCart(mpid).clear();
755748
},
756749
},
757750
/**

src/persistence.interfaces.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,8 @@ export interface IPersistence {
9999
useLocalStorage(): boolean;
100100
initializeStorage(): void;
101101
update(): void;
102-
storeProductsInMemory(products: Product[], mpid: MPID): void;
103102
storeDataInMemory(obj: IPersistenceMinified, currentMPID: MPID): void;
104103
determineLocalStorageAvailability(storage: Storage): boolean;
105-
getUserProductsFromLS(mpid: MPID): Product[];
106-
getAllUserProductsFromLS(): Product[];
107104
setLocalStorage(): void;
108105
getLocalStorage(): IPersistenceMinified | null;
109106
expireCookies(cookieName: string): void;
@@ -120,8 +117,6 @@ export interface IPersistence {
120117
decodePersistence(persistenceString: string): string;
121118
getCookieDomain(): string;
122119
getDomain(doc: string, locationHostname: string): string;
123-
getCartProducts(mpid: MPID): Product[];
124-
setCartProducts(allProducts: Product[]): void;
125120
saveUserCookieSyncDatesToPersistence(mpid: MPID, csd: CookieSyncDates): void;
126121
savePersistence(persistance: IPersistenceMinified): void;
127122
getPersistence(): IPersistenceMinified;

0 commit comments

Comments
 (0)