UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

132 lines (105 loc) 3.5 kB
import Apicart from '@apicart/core-sdk'; import { CustomerDaoInterface } from '../../Contract/Dao/Customer/CustomerDaoInterface'; import { DataSourceInterface } from '../../Contract/DataSource/DataSourceInterface'; import Customer from '../../Entity/Customer/Customer'; import CustomerParameter from '../../Entity/Customer/CustomerParameter'; import CustomerEventDispatcherEvents from '../../EventDispatcherEvents/CustomerEventDispatcherEvents'; export default class CustomerDao implements CustomerDaoInterface { private _hash: string | null; private _dataSource: DataSourceInterface; private _customer: Customer | null = null; private _customerPromise: Promise<Customer | null> | null = null; constructor(dataSource: DataSourceInterface, hash: string = null) { this._dataSource = dataSource; this._hash = hash; } /** @inheritdoc */ public async getEntity(clearCache = false): Promise<Customer> { if (!this._customerPromise && (this._customer === null || clearCache)) { let customer: any = this._customer; this._customerPromise = this.callDataSource((): Promise<Customer | null> => { return this._dataSource.findCustomer(this.getHash()); }); customer = await this._customerPromise; this._customerPromise = null; if (customer === null && this.getHash()) { Apicart.Utils.Console.error('Customer entity with hash "' + this.getHash() + '" not found.'); } else { this._customer = customer; this._hash = customer.getHash(); Apicart.Utils.EventDispatcher.dispatchEvent(CustomerEventDispatcherEvents.UPDATED, this); } } if (this._customerPromise) { await this._customerPromise; this._customerPromise = null; } return this._customer; } /** @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 addParameter(key: string, value: any): Promise<boolean> { const parameter = {}; parameter[key] = value; return this.addParameters(parameter); } /** @inheritdoc */ public async addParameters(parameters: Record<string, any>): Promise<boolean> { const response = this.callDataSource((): Promise<boolean> => { return this._dataSource.addCustomerParameters(this.getHash(), parameters); }, true); Apicart.Utils.EventDispatcher.dispatchEvent(CustomerEventDispatcherEvents, parameters); return response; } /** @inheritdoc */ public async removeParameter(key: string): Promise<boolean> { return this.removeParameters([key]); } /** @inheritdoc */ public async removeParameters(keys: string[]): Promise<boolean> { return this.callDataSource((): Promise<boolean> => { return this._dataSource.removeCustomerParameters(this.getHash(), keys); }, true); } /** @inheritdoc */ public async getParameters(): Promise<CustomerParameter[]> { return (await this.getEntity()).getParameters(); } /** @inheritdoc */ public async getParameterValue(key: string, defaultValue: any = null): Promise<any> { return (await this.getEntity()).getParameterValue(key, defaultValue); } private async callDataSource(callback: Function, updateEntity = false): Promise<any> { const response = await callback.call(this); if (updateEntity) { await this.getEntity(true); } return response; } }