@apicart/store-sdk
Version:
Apicart SDK for integrating store into any web application
204 lines (165 loc) • 4.29 kB
text/typescript
import Apicart from '@apicart/core-sdk';
import ShippingMethodParameter from './ShippingMethodParameter';
export interface ShippingMethodSerializedStructureInterface
{
id: number;
uid: string | null;
name: string;
description: string;
image: string | null;
price: number;
sort: number;
enabled: boolean;
parameters: any;
}
export default class ShippingMethod
{
private _id: number;
private _name: string;
private _description: string | null
private _image: string | null;
private _price: number;
private _sort: number | null;
private _uid: string | null = null;
private _enabled = false;
private _parameters: ShippingMethodParameter[] = [];
constructor(
id: number,
name: string,
description: string | null,
image: string | null,
price: number,
sort: number | null,
enabled: boolean,
uid: string | null = null,
parameters: ShippingMethodParameter[] = []
) {
this._id = id;
this._uid = uid;
this._name = name;
this._description = description;
this._image = image;
this._price = price;
this._sort = sort;
this._enabled = enabled;
Apicart.Utils.Loops.forEach(parameters, (shippingMethodParameter: ShippingMethodParameter): void => {
this.addParameter(shippingMethodParameter);
});
}
public getId(): number
{
return this._id;
}
public getUid(): string | null
{
return this._uid;
}
public getName(): string
{
return this._name;
}
public getDescription(): string | null
{
return this._description;
}
public getImage(): string | null
{
return this._image;
}
public getPrice(): number
{
return this._price;
}
public getSort(): number | null
{
return this._sort;
}
public isEnabled(): boolean
{
return this._enabled;
}
public getParameters(): ShippingMethodParameter[]
{
return this._parameters;
}
public addParameter(shippingMethodParameter: ShippingMethodParameter): void
{
let parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), (existingParameter): void | boolean => {
if (existingParameter.getKey() === shippingMethodParameter.getKey()) {
existingParameter.setValue(shippingMethodParameter.getValue());
parameterUpdated = true;
return false;
}
});
if (!parameterUpdated) {
this._parameters.push(shippingMethodParameter);
}
}
public removeParameter(key: string): void
{
Apicart.Utils.Loops.forEach(
this.getParameters(),
(parameter: ShippingMethodParameter, index: number): void | boolean => {
if (parameter.getKey() === key) {
delete this._parameters[index];
return false;
}
}
);
}
public getParameterValue(key: string, defaultValue: any = null): any
{
let parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: ShippingMethodParameter): void | boolean => {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
return false;
}
});
return parameterValue;
}
public serialize(): ShippingMethodSerializedStructureInterface
{
const structure = {
id: this.getId(),
uid: this.getUid(),
name: this.getName(),
description: this.getDescription(),
image: this.getImage(),
price: this.getPrice(),
sort: this.getSort(),
enabled: this.isEnabled(),
parameters: []
};
Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: ShippingMethodParameter): void => {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
}
public static deserialize(data: any): ShippingMethod
{
Apicart.Utils.Loops.forEach(
['id', 'name', 'image', 'price', 'sort', 'enabled'],
(key: string): void => {
if ( ! (key in data)) {
throw 'ShippingMethod: Missing required parameter: "' + key + '".';
}
}
);
const shippingMethod = new ShippingMethod(
data.id, data.name, data.description, data.image, data.price, data.sort, data.enabled, data.uid,
);
if ('parameters' in data && Array.isArray(data.parameters)) {
Apicart.Utils.Loops.forEach(data.parameters, (parameter: any): void => {
if ('key' in parameter) {
shippingMethod.addParameter(new ShippingMethodParameter(parameter.key, parameter.value || null));
}
});
}
return shippingMethod;
}
}