UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

255 lines (206 loc) 5.38 kB
import Apicart from '@apicart/core-sdk'; import Cart from './../Cart/Cart'; import OrderState from './OrderState'; import OrderParameter from './OrderParameter'; import OrderItem from './OrderItem'; import Customer from '../Customer/Customer'; export default class Order { private _hash: string; private _variableSymbol: string; private _totalPrice: number; private _items: OrderItem[] = []; private _parameters: OrderParameter[] = []; private _createdAt: Date; private _updatedAt: Date; private _orderState: OrderState; private _cart: Cart private _customer: Customer constructor( createdAt: Date, updatedAt: Date, hash: string, variableSymbol: string, totalPrice: number, orderState: OrderState, cart: Cart, customer: Customer, items: OrderItem[] = [], parameters: OrderParameter[] = [] ) { this._createdAt = createdAt; this._updatedAt = updatedAt; this._hash = hash; this._variableSymbol = variableSymbol; this._totalPrice = totalPrice; this.setOrderState(orderState); this.setCart(cart); this.setCustomer(customer); Apicart.Utils.Loops.forEach(items, (orderItem: OrderItem): void => { this.addItem(orderItem); }); Apicart.Utils.Loops.forEach(parameters, (orderParameter: OrderParameter): void => { this.addParameter(orderParameter); }); } public getCreatedAt(): Date { return this._createdAt; } public getUpdatedAt(): Date { return this._updatedAt; } public getHash(): string { return this._hash; } public getVariableSymbol(): string { return this._variableSymbol; } public getTotalPrice(): number { return this._totalPrice; } public getOrderState(): OrderState { return this._orderState; } public setOrderState(orderState: OrderState): void { this._orderState = orderState; } public getCart(): Cart { return this._cart; } public setCart(cart: Cart): void { this._cart = cart; } public getCustomer(): Customer { return this._customer; } public setCustomer(customer: Customer): void { this._customer = customer; } public getItems(): OrderItem[] { return this._items; } public addItem(orderItem: OrderItem): void { this._items.push(orderItem); } public removeItemByExternalId(externalId: string | number): void { Apicart.Utils.Loops.forEach(this.getItems(), (item: OrderItem, key: string): void | boolean => { if (item.getExternalId() === externalId) { delete this._items[key]; return false; } }); } /** * @param number|externalId: string */ public findItemByExternalId(externalId: string | number): OrderItem | null { let selectedItem = null; Apicart.Utils.Loops.forEach(this.getItems(), (item: OrderItem): void | boolean => { if (item.getExternalId() === externalId) { selectedItem = item; return false; } }); return selectedItem; } public getParameters(): OrderParameter[] { return this._parameters; } public addParameter(orderParameter: OrderParameter): void { let parameterUpdated = false; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: OrderParameter): void | boolean => { if (parameter.getKey() === orderParameter.getKey()) { parameter.setValue(orderParameter.getValue()); parameterUpdated = true; return false; } }); if (!parameterUpdated) { this._parameters.push(orderParameter); } } public removeParameter(key: string): void { Apicart.Utils.Loops.forEach( this.getParameters(), (parameter: OrderParameter, index: number): void | boolean => { if (parameter.getKey() === key) { delete this._parameters[index]; return false; } } ); } public getParameterValue(key: string, defaultValue: any = null): any { let parameterValue: any = defaultValue; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: OrderParameter): void | boolean => { 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 static deserialize(data: any): Order { Apicart.Utils.Loops.forEach( [ 'createdAt', 'updatedAt', 'hash', 'variableSymbol', 'totalPrice', 'state.code', 'cart.hash', 'customer.hash', 'customer.state' ], (keyPath: string): void => { if ( ! Apicart.Utils.Objects.keyExists(data, keyPath)) { throw 'Order: Missing required parameter: "' + keyPath + '".'; } } ); const order = new Order( new Date(data.createdAt), new Date(data.updatedAt), data.hash, data.variableSymbol, data.totalPrice, new OrderState(data.state.code), Cart.deserialize(data.cart), Customer.deserialize(data.customer) ); if ('parameters' in data && Array.isArray(data.parameters)) { Apicart.Utils.Loops.forEach(data.parameters, (parameter: any): void => { if ('key' in parameter) { order.addParameter(new OrderParameter(parameter.key, parameter.value || null)); } }); } if ('items' in data && Array.isArray(data.items)) { Apicart.Utils.Loops.forEach(data.items, (item: OrderItem): void => { order.addItem(OrderItem.deserialize(item)); }); } return order; } }