UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

341 lines (280 loc) 7.79 kB
import Apicart from '@apicart/core-sdk'; import CartState from './CartState'; import CartItem from './CartItem'; import CartParameter from './CartParameter'; import Customer from '../Customer/Customer'; import PaymentMethod from '../Payment/PaymentMethod'; import ShippingMethod from '../Shipping/ShippingMethod'; export interface CartSerializedStructureInterface { createdAt: Date; updatedAt: Date; hash: string; state: { code: string; }; customer: any; items: any[]; parameters: any; } export default class Cart { private _hash: string; private _totalPrice: number; private _items: CartItem[] = []; private _parameters: CartParameter[] = []; private _createdAt: Date; private _updatedAt: Date private _cartState: CartState private _customer: Customer; private _shippingMethod: ShippingMethod | null = null; private _paymentMethod: PaymentMethod | null = null; constructor( createdAt: Date, updatedAt: Date, hash: string, totalPrice: number, cartState: CartState, customer: Customer, paymentMethod: PaymentMethod = null, shippingMethod: ShippingMethod = null, items: CartItem[] = [], parameters: CartParameter[] = [] ) { this._createdAt = createdAt; this._updatedAt = updatedAt; this._hash = hash; this._totalPrice = totalPrice; this._paymentMethod = paymentMethod; this._shippingMethod = shippingMethod; this.setCartState(cartState); this.setCustomer(customer); Apicart.Utils.Loops.forEach(items, (cartItem: CartItem) => { this.addItem(cartItem); }); Apicart.Utils.Loops.forEach(parameters, (cartParameter: CartParameter): void => { this.addParameter(cartParameter); }); } public getCreatedAt(): Date { return this._createdAt; } public getUpdatedAt(): Date { return this._updatedAt; } public getHash(): string { return this._hash; } public getTotalPrice(): number { return this._totalPrice; } public getCartState(): CartState { return this._cartState; } public setCartState(cartState: CartState): void { this._cartState = cartState; } public getCustomer(): Customer { return this._customer; } public setCustomer(customer: Customer): void { this._customer = customer; } public getItems(): CartItem[] { return this._items; } public getTotalItemsQuantity(): number { let quantity = 0; Apicart.Utils.Loops.forEach(this.getItems(), (item: CartItem) => { quantity += item.getQuantity(); }); return quantity; } public addItem(cartItem: CartItem): void { this._items.push(cartItem); } public removeItemByExternalId(externalId: string|number): void { Apicart.Utils.Loops.forEach(this.getItems(), (item: CartItem, key: number): false | void => { if (item.getExternalId() === externalId) { delete this._items[key]; return false; } }); } public removeItemByDataUrl(dataUrl: string): void { Apicart.Utils.Loops.forEach(this.getItems(), (item: CartItem, key: number): false | void => { if (item.getItem().getDataUrl() === dataUrl) { delete this._items[key]; return false; } }); } public findItemByExternalId(externalId: string|number): CartItem | null { let selectedItem: CartItem | null = null; Apicart.Utils.Loops.forEach(this.getItems(), (item: CartItem): false | void => { if (item.getItem().getExternalId() === externalId) { selectedItem = item; return false; } }); return selectedItem; } public findItemByDataUrl(dataUrl: string): CartItem | null { let selectedItem: any | null = null; Apicart.Utils.Loops.forEach(this.getItems(), (item: CartItem): false | void => { if (item.getItem().getDataUrl() === dataUrl) { selectedItem = item; return false; } }); return selectedItem; } public getParameters(): CartParameter[] { return this._parameters; } public addParameter(cartParameter: CartParameter): void { let parameterUpdated = false; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CartParameter): false | void => { if (parameter.getKey() === cartParameter.getKey()) { parameter.setValue(cartParameter.getValue()); parameterUpdated = true; return false; } }); if (!parameterUpdated) { this._parameters.push(cartParameter); } } public removeParameter(key: string): void { Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CartParameter, index: number): false | void => { if (parameter.getKey() === key) { delete this._parameters[index]; return false; } }); } public getPaymentMethod(): PaymentMethod | null { return this._paymentMethod; } public getShippingMethod(): ShippingMethod | null { return this._shippingMethod; } public getParameterValue(key: string, defaultValue: any = null): any { let parameterValue: any = defaultValue; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CartParameter): false | void => { if (parameter.getKey() === key) { parameterValue = parameter.getValue(); return false; } }); return parameterValue; } public getExternalId(): string | number | null { let externalId = this.getParameterValue('externalId'); if (!['string', 'number'].includes(typeof externalId) && externalId !== null) { externalId = null; } return externalId; } public serialize(): CartSerializedStructureInterface { const structure = { createdAt: this.getCreatedAt(), updatedAt: this.getUpdatedAt(), hash: this.getHash(), totalPrice: this.getTotalPrice(), state: { code: this.getCartState().getCode() }, customer: this.getCustomer().serialize(), items: [], parameters: [], paymentMethod: null, shippingMethod: null }; const paymentMethod = this.getPaymentMethod(); const shippingMethod = this.getShippingMethod(); if (paymentMethod) { structure.paymentMethod = paymentMethod.serialize(); } if (shippingMethod) { structure.shippingMethod = shippingMethod.serialize(); } Apicart.Utils.Loops.forEach(this.getItems(), (cartItem: CartItem): void => { structure.items.push(cartItem.serialize()); }); Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CartParameter): false | void => { structure.parameters.push({ key: parameter.getKey(), value: parameter.getValue() }); }); return structure; } public serializeForGraphQL(): any { const serializedCart = this.serialize(); return { hash: serializedCart.hash, state: serializedCart.state.code, customerHash: serializedCart.customer.hash, items: serializedCart.items, parameters: serializedCart.parameters }; } public static deserialize(data: any): Cart { Apicart.Utils.Loops.forEach( ['createdAt', 'updatedAt', 'hash', 'totalPrice', 'state.code', 'customer.hash', 'customer.state'], (keyPath: string): false | void => { if (!Apicart.Utils.Objects.keyExists(data, keyPath)) { throw 'Cart: Missing required parameter: "' + keyPath + '".'; } } ); const cart = new Cart( new Date(data.createdAt), new Date(data.updatedAt), data.hash, data.totalPrice, new CartState(data.state.code), Customer.deserialize(data.customer), data.paymentMethod ? PaymentMethod.deserialize(data.paymentMethod) : null, data.shippingMethod ? ShippingMethod.deserialize(data.shippingMethod) : null ); if ('parameters' in data && Array.isArray(data.parameters)) { Apicart.Utils.Loops.forEach(data.parameters, (parameter: any): void => { if ('key' in parameter) { cart.addParameter(new CartParameter(parameter.key, parameter.value || null)); } }); } if ('items' in data && Array.isArray(data.items)) { Apicart.Utils.Loops.forEach(data.items, (item: CartItem): void => { cart.addItem(CartItem.deserialize(item)); }); } return cart; } }