UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

186 lines (149 loc) 4.16 kB
import Apicart from '@apicart/core-sdk'; import CustomerState from './CustomerState'; import CustomerParameter from './CustomerParameter'; export interface CustomerSerializedStructureInterface { createdAt: Date; updatedAt: Date; hash: string; state: Record<string, any>; parameters: any; } export default class Customer { private _hash: string; private _parameters: CustomerParameter[] = []; private _createdAt: Date; private _updatedAt: Date; private _customerState: CustomerState; constructor( createdAt: Date, updatedAt: Date, hash: string, customerState: CustomerState, parameters: CustomerParameter[] = [] ) { this._createdAt = createdAt; this._updatedAt = updatedAt; this._hash = hash; this.setCustomerState(customerState); Apicart.Utils.Loops.forEach(parameters, (customerParameter: CustomerParameter): void => { this.addParameter(customerParameter); }); } public getCreatedAt(): Date { return this._createdAt; } public getUpdatedAt(): Date { return this._updatedAt; } public getHash(): string { return this._hash; } public getCustomerState(): CustomerState { return this._customerState; } public setCustomerState(customerState: CustomerState): void { this._customerState = customerState; } public getParameters(): CustomerParameter[] { return this._parameters; } public addParameter(customerParameter: CustomerParameter): void { let parameterUpdated = false; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CustomerParameter): void => { if (parameter.getKey() === customerParameter.getKey()) { parameter.setValue(customerParameter.getValue()); parameterUpdated = true; } }); if (!parameterUpdated) { this._parameters.push(customerParameter); } } public removeParameter(key: string): void { Apicart.Utils.Loops.forEach( this.getParameters(), (parameter: CustomerParameter, 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: CustomerParameter): 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 serialize(): CustomerSerializedStructureInterface { const structure = { createdAt: this.getCreatedAt(), updatedAt: this.getUpdatedAt(), hash: this.getHash(), state: { code: this.getCustomerState().getCode() }, parameters: [] }; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CustomerParameter): void | boolean => { structure.parameters.push({ key: parameter.getKey(), value: parameter.getValue() }); }); return structure; } public serializeForGraphQL(): any { const serializedCustomer = this.serialize(); return { hash: serializedCustomer.hash, state: serializedCustomer.state.code, parameters: serializedCustomer.parameters }; } public static deserialize(data: any): Customer { Apicart.Utils.Loops.forEach( ['createdAt', 'updatedAt', 'hash', 'state'], function (keyPath: string) { if (!Apicart.Utils.Objects.keyExists(data, keyPath)) { throw 'Customer: Missing required parameter: "' + keyPath + '".'; } } ); const customer = new Customer( new Date(data.createdAt), new Date(data.updatedAt), data.hash, new CustomerState(data.state.code) ); if ('parameters' in data && Array.isArray(data.parameters)) { Apicart.Utils.Loops.forEach(data.parameters, (parameter: any): void => { if ('key' in parameter) { customer.addParameter(new CustomerParameter(parameter.key, parameter.value || null)); } }); } return customer; } }