Skip to content

Commit

Permalink
Merge pull request #10 from Ecwid/checkout-info-storage
Browse files Browse the repository at this point in the history
get cart info from checkout info storage instead of legacy cart record
  • Loading branch information
Alexis2004 authored Oct 2, 2023
2 parents be5c718 + d3b9b93 commit a95677d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ecwid/sdk",
"version": "0.6.0",
"version": "0.7.0",
"description": "Lightweight SDK library to access Ecwid API functions.",
"author": "Ecwid",
"license": "Apache-2.0",
Expand Down
12 changes: 12 additions & 0 deletions src/converter/checkout-converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LocalStorageCheckout } from '../type/local-storage';
import { Cart } from '../type/cart';

export default class CheckoutConverter {
toCart(localStorageCart: LocalStorageCheckout): Cart {
return {
cartId: '',
items: [],
productsQuantity: localStorageCart.itemsCount,
};
}
}
18 changes: 18 additions & 0 deletions src/service/cart-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cart } from '../type/cart';
import CartConverter from '../converter/cart-converter';
import CheckoutConverter from '../converter/checkout-converter';

/**
* Ecommerce SDK cart service. Contains cart operations.
Expand All @@ -10,16 +11,22 @@ class CartService {

private readonly cartConverter: CartConverter;

private readonly checkoutConverter: CheckoutConverter;

private readonly localStorageCartKey: string;

private readonly localStorageCheckoutKey: string;

private readonly defaultStoreLocationPath: string;

constructor(storeId: number, storeLocationPath?: string) {
this.localStorageCartKey = this.getCartKey(storeId);
this.localStorageCheckoutKey = this.getCheckoutKey(storeId);
this.defaultStoreLocationPath = storeLocationPath
? this.unifyStoreLocationPath(storeLocationPath)
: this.DEFAULT_STORE_LOCATION_PATH;
this.cartConverter = new CartConverter();
this.checkoutConverter = new CheckoutConverter();
}

/**
Expand All @@ -30,6 +37,13 @@ class CartService {
if (typeof window === 'undefined' || !window || !window.localStorage) {
return this.createEmptyCartPromise();
}
const checkoutRecord = window.localStorage.getItem(this.localStorageCheckoutKey);
if (checkoutRecord != null) {
const localStorageCheckout = JSON.parse(checkoutRecord);
const cart = this.checkoutConverter.toCart(localStorageCheckout);
return this.createCartPromise(cart);
}

const cartRecord = window.localStorage.getItem(this.localStorageCartKey);
if (cartRecord == null) {
return this.createEmptyCartPromise();
Expand Down Expand Up @@ -70,6 +84,10 @@ class CartService {
return `PSecwid__${storeId}PScart`;
}

private getCheckoutKey(storeId: number) {
return `ec-${storeId}-checkout`;
}

private createCartPromise(cart: Cart): Promise<Cart> {
return Promise.resolve(cart);
}
Expand Down
5 changes: 5 additions & 0 deletions src/type/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export interface LocalStorageCart {
readonly cartId: string;
readonly order: LocalStorageOrder;
}

export interface LocalStorageCheckout {
readonly id?: string;
readonly itemsCount: number;
}
17 changes: 17 additions & 0 deletions test/converter/checkout-converter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import CheckoutConverter from '../../src/converter/checkout-converter';
import { LocalStorageCheckout } from '../../src/type/local-storage';

test('LocalStorageCheckout to Cart conversion', () => {
const cartConverter = new CheckoutConverter();
const localStorageCheckout: LocalStorageCheckout = {
id: 'GkpzhWnTX2gBvgBQ',
itemsCount: 4,
};
const expectedResult = {
cartId: '',
items: [],
productsQuantity: 4,
};
const actualResult = cartConverter.toCart(localStorageCheckout);
expect(actualResult).toEqual(expectedResult);
});
17 changes: 17 additions & 0 deletions test/service/cart-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,21 @@ describe('Get cart tests', () => {
expect(cart).toBe(expectedCart);
});
});

test('Get cart from checkout info storage', () => {
const localStorageCheckout = {
id: 'GkpzhWnTX2gBvgBQ',
itemsCount: 4,
};
global.localStorage.setItem('ec-1002-checkout', JSON.stringify(localStorageCheckout));

const cartService = new CartService(1002);
cartService.get().then((cart) => {
expect(cart).toEqual({
cartId: '',
items: [],
productsQuantity: 4,
});
});
});
});

0 comments on commit a95677d

Please sign in to comment.