@apicart/store-sdk
Version:
Apicart SDK for integrating store into any web application
154 lines (123 loc) • 3.49 kB
text/typescript
import Apicart from '@apicart/core-sdk';
import CartItemParameter from './CartItemParameter';
import Item from '../Item/Item';
export default class CartItem
{
private _quantity: number;
private _totalPrice: number;
private _parameters: CartItemParameter[] = [];
private _item: Item;
constructor(quantity: number, totalPrice: number, item: Item, parameters: CartItemParameter[] = [])
{
this._quantity = quantity;
this._totalPrice = totalPrice;
this._item = item;
Apicart.Utils.Loops.forEach(parameters, (cartItemParameter: CartItemParameter): void => {
this.addParameter(cartItemParameter);
});
}
public getQuantity(): number
{
return this._quantity;
}
public increaseQuantity(by = 1): void
{
this._quantity += by;
}
public decreaseQuantity(by = 1): void
{
if (this.getQuantity() - by <= 0) {
throw 'Product quantity cannot be decreased to zero.';
}
this._quantity -= by;
}
public getTotalPrice(): number
{
return this._totalPrice;
}
public getItem(): Item
{
return this._item;
}
public getParameters(): CartItemParameter[]
{
return this._parameters;
}
public addParameter(cartItemParameter: CartItemParameter): void
{
let parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), (existingParameter: CartItemParameter): void => {
if (existingParameter.getKey() === cartItemParameter.getKey()) {
existingParameter.setValue(cartItemParameter.getValue());
parameterUpdated = true;
return;
}
});
if (!parameterUpdated) {
this._parameters.push(cartItemParameter);
}
}
public removeParameter(key: string): void
{
Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CartItemParameter, 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: CartItemParameter): void => {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
}
});
return parameterValue;
}
public getExternalId(): string | number | null
{
return this.getItem().getExternalId();
}
public serialize(): Record<string, any>
{
const structure = {
dataUrl: this.getItem().getDataUrl(),
quantity: this.getQuantity(),
parameters: [],
totalPrice: this.getTotalPrice(),
item: this.getItem().serialize()
};
Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: CartItemParameter): void => {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
}
public serializeForGraphQL(): Record<string, any>
{
return this.serialize();
}
public static deserialize(data: any): CartItem
{
Apicart.Utils.Loops.forEach(
['quantity', 'totalPrice', 'item'],
(keyPath: string) => {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'CartItem: Missing required parameter: "' + keyPath + '".';
}
}
);
const cartItem = new CartItem(data.quantity, data.totalPrice, Item.deserialize(data.item));
if ('parameters' in data && Array.isArray(data.parameters)) {
Apicart.Utils.Loops.forEach(data.parameters, (parameter: any) => {
if ('key' in parameter) {
cartItem.addParameter(new CartItemParameter(parameter.key, parameter.value || null));
}
});
}
return cartItem;
}
}