UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

173 lines (141 loc) 5.33 kB
import Api from './DataSource/Api'; import Apicart from '@apicart/core-sdk'; import { CartDaoInterface } from './Contract/Dao/Cart/CartDaoInterface'; import CustomerState from './Entity/Customer/CustomerState'; import Customer from './Entity/Customer/Customer'; import CartState from './Entity/Cart/CartState'; import Cart from './Entity/Cart/Cart'; import { CustomerDaoInterface } from './Contract/Dao/Customer/CustomerDaoInterface'; import { OrderDaoInterface } from './Contract/Dao/Order/OrderDaoInterface'; import CustomerDao from './Dao/Customer/CustomerDao'; import CartDao from './Dao/Cart/CartDao'; import OrderDao from './Dao/Order/OrderDao'; import PaymentMethodDao from './Dao/Payment/PaymentMethodDao'; import ShippingMethodDao from './Dao/Shipping/ShippingMethodDao'; import { PaymentMethodDaoInterface } from './Contract/Dao/Payment/PaymentMethodDaoInterface'; import { ShippingMethodDaoInterface } from './Contract/Dao/Shipping/ShippingMethodDaoInterface'; import CartEventDispatcherEvents from './EventDispatcherEvents/CartEventDispatcherEvents'; import CustomerEventDispatcherEvents from './EventDispatcherEvents/CustomerEventDispatcherEvents'; import PaymentMethod from './Entity/Payment/PaymentMethod'; import ShippingMethod from './Entity/Shipping/ShippingMethod'; import { DataSourceInterface } from './Contract/DataSource/DataSourceInterface'; import { StoreInterface } from './Contract/StoreInterface'; export default class Store implements StoreInterface { private _config: any = { token: '', dataSource: { class: Api, parameters: { connectionTimeout: 5000, url: 'https://store-api.apicart.' + (Apicart.Configurator.isDevEnv() ? 'dev' : 'net'), withCredentials: false } } } private _dataSource: DataSourceInterface = null; public readonly eventDispatcherEvents = { cart: CartEventDispatcherEvents, customer: CustomerEventDispatcherEvents } constructor(config: Record<string, any>) { this._config = this.parseConfig(config); } public async createCart(customerHash: string = null): Promise<CartDaoInterface> { const cartHash = Apicart.Utils.Strings.generateHash(32); const now = new Date; const customer = customerHash ? await this.getCustomer(customerHash) : await this.createCustomer(); const cartEntity = new Cart( now, now, cartHash, 0.0, new CartState(CartState.CODE_ACTIVE), await customer.getEntity() ); const status = await this.saveCart(cartEntity); if (!status) { throw 'Unable to create cart'; } return this.getCart(cartHash); } public async createCustomer(): Promise<CustomerDaoInterface> { const customerHash = Apicart.Utils.Strings.generateHash(32); const now = new Date; const customerEntity = new Customer(now, now, customerHash, new CustomerState(CustomerState.CODE_ACTIVE)); const status = await this.saveCustomer(customerEntity); if (!status) { throw 'Unable to create customer'; } return this.getCustomer(customerHash); } public getCart(hash: string): Promise<CartDaoInterface> { return Promise.resolve(new CartDao(this.getDataSource(), hash)); } public getCustomer(hash: string): Promise<CustomerDaoInterface> { return Promise.resolve(new CustomerDao(this.getDataSource(), hash)); } public getOrder(hash: string): OrderDaoInterface { return new OrderDao(this.getDataSource(), hash); } public getPaymentMethod(id: number): PaymentMethodDaoInterface { return new PaymentMethodDao(this.getDataSource(), id); } public async getPaymentMethods(onlyEnabled = true): Promise<PaymentMethod[] | null> { return this.getDataSource().findPaymentMethods(onlyEnabled); } public getShippingMethod(id: number): ShippingMethodDaoInterface { return new ShippingMethodDao(this.getDataSource(), id); } public async getShippingMethods(onlyEnabled = true): Promise<ShippingMethod[] | null> { return this.getDataSource().findShippingMethods(onlyEnabled); } public async saveCart(cartEntity: Cart): Promise<boolean> { return this.getDataSource().fullCartSynchronization(cartEntity); } public async saveCustomer(customerEntity: Customer): Promise<boolean> { return this.getDataSource().fullCustomerSynchronization(customerEntity); } public getToken(): string { return this._config.token; } private getDataSource(): DataSourceInterface { if (this._dataSource === null) { const DataSourceClass = this._config.dataSource.class; this._dataSource = new DataSourceClass( this._config.token, this._config.dataSource.parameters ); } return this._dataSource; } private parseConfig(config: Record<string, any>): Record<string, any> { const newConfig = Apicart.Utils.Objects.merge(this._config, config); if (!Apicart.Utils.Objects.keyExists(newConfig, 'token') || Apicart.Utils.Validators.isEmpty(newConfig.token) ) { throw 'Missing token'; } if (!Apicart.Utils.Objects.keyExists(newConfig, 'dataSource') || !Apicart.Utils.Objects.keyExists(newConfig, 'dataSource.class') || Apicart.Utils.Validators.isEmpty(newConfig.dataSource.class) || Apicart.Utils.Validators.isEmpty(newConfig.dataSource.class) ) { throw 'Parameter "dataSource" is not valid.'; } if (! (/^[0-9a-zA-Z_!?]{64}$/i).test(newConfig.token)) { throw 'Parameter "token" must contains 64 characters.'; } return newConfig; } }