UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

167 lines (132 loc) 4.06 kB
import Apicart from '@apicart/core-sdk'; import { OrderDaoInterface } from '../../Contract/Dao/Order/OrderDaoInterface'; import { DataSourceInterface } from '../../Contract/DataSource/DataSourceInterface'; import Order from '../../Entity/Order/Order'; import OrderState from '../../Entity/Order/OrderState'; import { CartDaoInterface } from '../../Contract/Dao/Cart/CartDaoInterface'; import { CustomerDaoInterface } from '../../Contract/Dao/Customer/CustomerDaoInterface'; import OrderItem from '../../Entity/Order/OrderItem'; import CustomerDao from '../Customer/CustomerDao'; import CartDao from '../Cart/CartDao'; import OrderParameter from '../../Entity/Order/OrderParameter'; export default class OrderDao implements OrderDaoInterface { private _hash: string private _dataSource: DataSourceInterface; private _order: Order | null = null; private _orderPromise: Promise<Order|null> | null = null; constructor(dataSource: DataSourceInterface, hash: string) { this._dataSource = dataSource; this._hash = hash; } /** @inheritdoc */ public async getEntity(): Promise<Order | null> { if (!this._orderPromise && this._order === null) { this._orderPromise = this._dataSource.findOrder(this.getHash()); const order = await this._orderPromise; if (order === null) { Apicart.Utils.Console.error('Order entity with hash "' + this.getHash() + '" not found.'); } else { this._order = order; } } if (this._orderPromise) { await this._orderPromise; this._orderPromise = null; } return this._order; } /** @inheritdoc */ public getHash(): string { return this._hash; } /** @inheritdoc */ public async getCreatedAt(): Promise<Date> { return (await this.getEntity()).getCreatedAt(); } /** @inheritdoc */ public async getUpdatedAt(): Promise<Date> { return (await this.getEntity()).getUpdatedAt(); } /** @inheritdoc */ public async getVariableSymbol(): Promise<string> { return (await this.getEntity()).getVariableSymbol(); } /** @inheritdoc */ public async getTotalPrice(): Promise<number> { return (await this.getEntity()).getTotalPrice(); } /** @inheritdoc */ public async getOrderState(): Promise<OrderState> { return (await this.getEntity()).getOrderState(); } /** @inheritdoc */ public async getCart(): Promise<CartDaoInterface> { const cart = (await this.getEntity()).getCart(); return new CartDao(this._dataSource, cart.getHash()); } /** @inheritdoc */ public async getCustomer(): Promise<CustomerDaoInterface> { const customer = (await this.getEntity()).getCustomer(); return new CustomerDao(this._dataSource, customer.getHash()); } /** @inheritdoc */ public async getItems(): Promise<OrderItem[]> { return (await this.getEntity()).getItems(); } /** @inheritdoc */ public async addParameter(key: string, value: any): Promise<boolean> { const parameter = {}; parameter[key] = value; return this.addParameters([parameter]); } /** @inheritdoc */ public async addParameters(parameters: any[]): Promise<boolean> { this.clearCachedEntity(); return this._dataSource.addOrderParameters(this.getHash(), parameters); } /** @inheritdoc */ public async removeParameter(key: string): Promise<boolean> { return this.removeParameters([key]); } /** @inheritdoc */ public async removeParameters(keys: string[]): Promise<boolean> { this.clearCachedEntity(); return this._dataSource.removeOrderParameters(this.getHash(), keys); } /** @inheritdoc */ public async getParameters(): Promise<OrderParameter[]> { return (await this.getEntity()).getParameters(); } /** @inheritdoc */ public async getParameterValue(key: string, defaultValue: any = null): Promise<any> { return (await this.getEntity()).getParameterValue(key, defaultValue); } /** @inheritdoc */ public async changeState(orderStateCode: string): Promise<boolean> { this.clearCachedEntity(); return this._dataSource.changeOrderState(this.getHash(), orderStateCode); } private clearCachedEntity(): void { this._order = null; } }