@apicart/store-sdk
Version:
Apicart SDK for integrating store into any web application
161 lines (128 loc) • 3.4 kB
text/typescript
import Apicart from '@apicart/core-sdk';
import ItemParameter from './ItemParameter';
export default class Item
{
private _externalId: string;
private _name: string
private _price: number
private _taxRate: number
private _dataUrl: string
private _parameters: ItemParameter[] = []
constructor(
externalId: string,
name: string,
price: number,
taxRate: number,
dataUrl: string,
parameters: ItemParameter[] = []
) {
this._externalId = externalId;
this._name = name;
this._price = price;
this._taxRate = taxRate;
this._dataUrl = dataUrl;
Apicart.Utils.Loops.forEach(parameters, (itemParameter: ItemParameter): void => {
this.addParameter(itemParameter);
});
}
public getExternalId(): string | number | null
{
return this._externalId;
}
public getName(): string
{
return this._name;
}
public getPrice(): number
{
return this._price;
}
public getTaxRate(): number
{
return this._taxRate;
}
public getDataUrl(): string
{
return this._dataUrl;
}
public getParameters(): ItemParameter[]
{
return this._parameters;
}
public addParameter(itemParameter: ItemParameter): void
{
let parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), (existingParameter: ItemParameter): void => {
if (existingParameter.getKey() === itemParameter.getKey()) {
existingParameter.setValue(itemParameter.getValue());
parameterUpdated = true;
return;
}
});
if ( ! parameterUpdated) {
this._parameters.push(itemParameter);
}
}
public removeParameter(key: string): void
{
Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: ItemParameter, index: number): void => {
if (parameter.getKey() === key) {
delete this._parameters[index];
}
});
}
public getParameterValue(key: string, defaultValue: any = null): any
{
let parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: ItemParameter): void => {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
}
});
return parameterValue;
}
public serialize(): Record<string, any>
{
const structure = {
externalId: this.getExternalId(),
dataUrl: this.getDataUrl(),
name: this.getName(),
parameters: [],
taxRate: this.getTaxRate(),
price: this.getPrice()
};
Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: ItemParameter) => {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
}
public serializeForGraphQL(): Record<string, any>
{
const serializedData = this.serialize();
delete serializedData['price'];
return serializedData;
}
public static deserialize(data: any): Item
{
Apicart.Utils.Loops.forEach(
['externalId', 'name', 'taxRate', 'dataUrl'],
function (keyPath: string) {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'Item: Missing required parameter: "' + keyPath + '".';
}
}
);
const item = new Item(data.externalId, data.name, data.price, data.taxRate, data.dataUrl);
if ('parameters' in data && Array.isArray(data.parameters)) {
Apicart.Utils.Loops.forEach(data.parameters, (parameter: any): void => {
if ('key' in parameter) {
item.addParameter(new ItemParameter(parameter.key, parameter.value || null));
}
});
}
return item;
}
}