UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

206 lines (162 loc) 5.44 kB
import Apicart from '@apicart/core-sdk'; import Storage from './Storage/Storage'; import Store from './Store'; import { CartDaoInterface } from './Contract/Dao/Cart/CartDaoInterface'; import { CustomerDaoInterface } from './Contract/Dao/Customer/CustomerDaoInterface'; import { StoreInterface } from './Contract/StoreInterface'; import ShippingMethod from './Entity/Shipping/ShippingMethod'; import Customer from './Entity/Customer/Customer'; import Cart from './Entity/Cart/Cart'; import { ShippingMethodDaoInterface } from './Contract/Dao/Shipping/ShippingMethodDaoInterface'; import PaymentMethod from './Entity/Payment/PaymentMethod'; import { PaymentMethodDaoInterface } from './Contract/Dao/Payment/PaymentMethodDaoInterface'; import { OrderDaoInterface } from './Contract/Dao/Order/OrderDaoInterface'; class StoreBrowser implements StoreInterface { private _config: any = { token: '', cart: { hash: null }, customer: { hash: null } }; private _store: Store = null; private _cart: CartDaoInterface = null; private _cartPromise: Promise<CartDaoInterface> = null; private _customer: CustomerDaoInterface = null; private _customerPromise: Promise<CustomerDaoInterface> = null; constructor(config: Record<any, any>) { this._store = new Store({ token: config.token }); this._config = Apicart.Utils.Objects.merge(this._config, config); } public async createCart(customerHash: string = null): Promise<CartDaoInterface> { return this._store.createCart(customerHash); } public async createCustomer(): Promise<CustomerDaoInterface> { return this._store.createCustomer(); } public hasCustomerHash(): boolean { return Storage.getCustomerHash() !== null; } public hasCartHash(): boolean { return Storage.getCartHash() !== null; } public getOrder(hash: string): OrderDaoInterface { return this._store.getOrder(hash); } public getPaymentMethod(id: number): PaymentMethodDaoInterface { return this._store.getPaymentMethod(id); } public async getPaymentMethods(onlyEnabled = true, clearCache = false): Promise<PaymentMethod[] | null> { let serializedPaymentMethods: PaymentMethod[] = []; const paymentMethods = clearCache ? null : Storage.getPaymentMethods(); if (paymentMethods) { Apicart.Utils.Loops.forEach(paymentMethods, (paymentMethod: any) => { serializedPaymentMethods.push(PaymentMethod.deserialize(paymentMethod)); }); } else { serializedPaymentMethods = await this._store.getPaymentMethods(onlyEnabled); Storage.savePaymentMethods(serializedPaymentMethods); } return serializedPaymentMethods; } public getShippingMethod(id: number): ShippingMethodDaoInterface { return this._store.getShippingMethod(id); } public async getShippingMethods(onlyEnabled = true, clearCache = false): Promise<ShippingMethod[] | null> { let serializedShippingMethods = []; const shippingMethods = clearCache ? null : Storage.getShippingMethods(); if (shippingMethods) { Apicart.Utils.Loops.forEach(shippingMethods, (shippingMethod: any) => { serializedShippingMethods.push(ShippingMethod.deserialize(shippingMethod)); }); } else { serializedShippingMethods = await this._store.getShippingMethods(onlyEnabled); Storage.saveShippingMethods(serializedShippingMethods); } return serializedShippingMethods; } public async saveCart(cartEntity: Cart): Promise<boolean> { return this._store.saveCart(cartEntity); } public async saveCustomer(customerEntity: Customer): Promise<boolean> { return this._store.saveCustomer(customerEntity); } public getToken(): string { return this._store.getToken(); } public async getCart(): Promise<CartDaoInterface> { if (!this._cart) { if (!this._cartPromise) { this._cartPromise = new Promise(async (resolve) => { const cartHash = this._config.cart.hash || Storage.getCartHash(); let cart = null; if (cartHash) { cart = await this._store.getCart(cartHash); const cartEntity = await cart.getEntity(); const cartState = cartEntity ? await cart.getCartState() : null; if (!cartState || cartState.getCode() === 'finished') { cart = null; } } if (!cart) { const customer = await this.getCustomer(); cart = await this.createCart(customer.getHash()); } Storage.saveCartHash(cart.getHash()); resolve(cart); }); } if (this._cartPromise) { this._cart = await this._cartPromise; this._cartPromise = null; } } return this._cart; } public async getCustomer(): Promise<CustomerDaoInterface> { if (!this._customer) { if (!this._customerPromise) { this._customerPromise = new Promise(async (resolve) => { const customerHash = this._config.customer.hash || Storage.getCustomerHash(); let customer: CustomerDaoInterface = null; if (customerHash) { customer = await this._store.getCustomer(customerHash); const customerEntity = await customer.getEntity(); if (!customerEntity) { customer = null; } } if (!customer) { customer = await this.createCustomer(); } Storage.saveCustomerHash(customer.getHash()); resolve(customer); }); } if (this._customerPromise) { this._customer = await this._customerPromise; this._customerPromise = null; } } return this._customer; } } Apicart.Store = StoreBrowser; export default StoreBrowser;