@apicart/store-sdk
Version:
Apicart SDK for integrating store into any web application
1,329 lines (1,302 loc) • 114 kB
JavaScript
/**
* Utils.js v1.0.0-alpha7
* (c) 2018-2020 Apicart Company
* Released under the MIT License.
*/
import Apicart from '@apicart/core-sdk';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
var CartState = (function () {
function CartState(code) {
this._code = code;
}
CartState.prototype.getCode = function () {
return this._code;
};
CartState.prototype.setCode = function (code) {
this._code = code;
};
CartState.CODE_ACTIVE = 'active';
CartState.CODE_MERGED = 'merged';
CartState.CODE_FINISHED = 'finished';
return CartState;
}());
var CartItemParameter = (function () {
function CartItemParameter(key, value) {
this._key = key;
this._value = value;
}
CartItemParameter.prototype.getKey = function () {
return this._key;
};
CartItemParameter.prototype.getValue = function () {
return this._value;
};
CartItemParameter.prototype.setValue = function (value) {
this._value = value;
};
return CartItemParameter;
}());
var ItemParameter = (function () {
function ItemParameter(key, value) {
this._key = key;
this._value = value;
}
ItemParameter.prototype.getKey = function () {
return this._key;
};
ItemParameter.prototype.getValue = function () {
return this._value;
};
ItemParameter.prototype.setValue = function (value) {
this._value = value;
};
return ItemParameter;
}());
var Item = (function () {
function Item(externalId, name, price, taxRate, dataUrl, parameters) {
var _this = this;
if (parameters === void 0) { parameters = []; }
this._parameters = [];
this._externalId = externalId;
this._name = name;
this._price = price;
this._taxRate = taxRate;
this._dataUrl = dataUrl;
Apicart.Utils.Loops.forEach(parameters, function (itemParameter) {
_this.addParameter(itemParameter);
});
}
Item.prototype.getExternalId = function () {
return this._externalId;
};
Item.prototype.getName = function () {
return this._name;
};
Item.prototype.getPrice = function () {
return this._price;
};
Item.prototype.getTaxRate = function () {
return this._taxRate;
};
Item.prototype.getDataUrl = function () {
return this._dataUrl;
};
Item.prototype.getParameters = function () {
return this._parameters;
};
Item.prototype.addParameter = function (itemParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (existingParameter) {
if (existingParameter.getKey() === itemParameter.getKey()) {
existingParameter.setValue(itemParameter.getValue());
parameterUpdated = true;
return;
}
});
if (!parameterUpdated) {
this._parameters.push(itemParameter);
}
};
Item.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter, index) {
if (parameter.getKey() === key) {
delete _this._parameters[index];
}
});
};
Item.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
}
});
return parameterValue;
};
Item.prototype.serialize = function () {
var structure = {
externalId: this.getExternalId(),
dataUrl: this.getDataUrl(),
name: this.getName(),
parameters: [],
taxRate: this.getTaxRate(),
price: this.getPrice()
};
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
};
Item.prototype.serializeForGraphQL = function () {
var serializedData = this.serialize();
delete serializedData['price'];
return serializedData;
};
Item.deserialize = function (data) {
Apicart.Utils.Loops.forEach(['externalId', 'name', 'taxRate', 'dataUrl'], function (keyPath) {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'Item: Missing required parameter: "' + keyPath + '".';
}
});
var 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, function (parameter) {
if ('key' in parameter) {
item.addParameter(new ItemParameter(parameter.key, parameter.value || null));
}
});
}
return item;
};
return Item;
}());
var CartItem = (function () {
function CartItem(quantity, totalPrice, item, parameters) {
var _this = this;
if (parameters === void 0) { parameters = []; }
this._parameters = [];
this._quantity = quantity;
this._totalPrice = totalPrice;
this._item = item;
Apicart.Utils.Loops.forEach(parameters, function (cartItemParameter) {
_this.addParameter(cartItemParameter);
});
}
CartItem.prototype.getQuantity = function () {
return this._quantity;
};
CartItem.prototype.increaseQuantity = function (by) {
if (by === void 0) { by = 1; }
this._quantity += by;
};
CartItem.prototype.decreaseQuantity = function (by) {
if (by === void 0) { by = 1; }
if (this.getQuantity() - by <= 0) {
throw 'Product quantity cannot be decreased to zero.';
}
this._quantity -= by;
};
CartItem.prototype.getTotalPrice = function () {
return this._totalPrice;
};
CartItem.prototype.getItem = function () {
return this._item;
};
CartItem.prototype.getParameters = function () {
return this._parameters;
};
CartItem.prototype.addParameter = function (cartItemParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (existingParameter) {
if (existingParameter.getKey() === cartItemParameter.getKey()) {
existingParameter.setValue(cartItemParameter.getValue());
parameterUpdated = true;
return;
}
});
if (!parameterUpdated) {
this._parameters.push(cartItemParameter);
}
};
CartItem.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter, index) {
if (parameter.getKey() === key) {
delete _this._parameters[index];
}
});
};
CartItem.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
}
});
return parameterValue;
};
CartItem.prototype.getExternalId = function () {
return this.getItem().getExternalId();
};
CartItem.prototype.serialize = function () {
var structure = {
dataUrl: this.getItem().getDataUrl(),
quantity: this.getQuantity(),
parameters: [],
totalPrice: this.getTotalPrice(),
item: this.getItem().serialize()
};
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
};
CartItem.prototype.serializeForGraphQL = function () {
return this.serialize();
};
CartItem.deserialize = function (data) {
Apicart.Utils.Loops.forEach(['quantity', 'totalPrice', 'item'], function (keyPath) {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'CartItem: Missing required parameter: "' + keyPath + '".';
}
});
var 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, function (parameter) {
if ('key' in parameter) {
cartItem.addParameter(new CartItemParameter(parameter.key, parameter.value || null));
}
});
}
return cartItem;
};
return CartItem;
}());
var CartParameter = (function () {
function CartParameter(key, value) {
this._key = key;
this._value = value;
}
CartParameter.prototype.getKey = function () {
return this._key;
};
CartParameter.prototype.getValue = function () {
return this._value;
};
CartParameter.prototype.setValue = function (value) {
this._value = value;
};
return CartParameter;
}());
var CustomerState = (function () {
function CustomerState(code) {
this._code = code;
}
CustomerState.prototype.getCode = function () {
return this._code;
};
CustomerState.prototype.setCode = function (code) {
this._code = code;
};
CustomerState.CODE_ACTIVE = 'active';
CustomerState.CODE_DISABLE = 'disable';
return CustomerState;
}());
var CustomerParameter = (function () {
function CustomerParameter(key, value) {
this._key = key;
this._value = value;
}
CustomerParameter.prototype.getKey = function () {
return this._key;
};
CustomerParameter.prototype.getValue = function () {
return this._value;
};
CustomerParameter.prototype.setValue = function (value) {
this._value = value;
};
return CustomerParameter;
}());
var Customer = (function () {
function Customer(createdAt, updatedAt, hash, customerState, parameters) {
var _this = this;
if (parameters === void 0) { parameters = []; }
this._parameters = [];
this._createdAt = createdAt;
this._updatedAt = updatedAt;
this._hash = hash;
this.setCustomerState(customerState);
Apicart.Utils.Loops.forEach(parameters, function (customerParameter) {
_this.addParameter(customerParameter);
});
}
Customer.prototype.getCreatedAt = function () {
return this._createdAt;
};
Customer.prototype.getUpdatedAt = function () {
return this._updatedAt;
};
Customer.prototype.getHash = function () {
return this._hash;
};
Customer.prototype.getCustomerState = function () {
return this._customerState;
};
Customer.prototype.setCustomerState = function (customerState) {
this._customerState = customerState;
};
Customer.prototype.getParameters = function () {
return this._parameters;
};
Customer.prototype.addParameter = function (customerParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === customerParameter.getKey()) {
parameter.setValue(customerParameter.getValue());
parameterUpdated = true;
}
});
if (!parameterUpdated) {
this._parameters.push(customerParameter);
}
};
Customer.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter, index) {
if (parameter.getKey() === key) {
delete _this._parameters[index];
return false;
}
});
};
Customer.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
return false;
}
});
return parameterValue;
};
Customer.prototype.getExternalId = function () {
var externalId = this.getParameterValue('externalId');
if (!['string', 'number'].includes(typeof externalId) && externalId !== null) {
externalId = null;
}
return externalId;
};
Customer.prototype.serialize = function () {
var structure = {
createdAt: this.getCreatedAt(),
updatedAt: this.getUpdatedAt(),
hash: this.getHash(),
state: {
code: this.getCustomerState().getCode()
},
parameters: []
};
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
};
Customer.prototype.serializeForGraphQL = function () {
var serializedCustomer = this.serialize();
return {
hash: serializedCustomer.hash,
state: serializedCustomer.state.code,
parameters: serializedCustomer.parameters
};
};
Customer.deserialize = function (data) {
Apicart.Utils.Loops.forEach(['createdAt', 'updatedAt', 'hash', 'state'], function (keyPath) {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'Customer: Missing required parameter: "' + keyPath + '".';
}
});
var 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, function (parameter) {
if ('key' in parameter) {
customer.addParameter(new CustomerParameter(parameter.key, parameter.value || null));
}
});
}
return customer;
};
return Customer;
}());
var PaymentMethodParameter = (function () {
function PaymentMethodParameter(key, value) {
this._key = key;
this._value = value;
}
PaymentMethodParameter.prototype.getKey = function () {
return this._key;
};
PaymentMethodParameter.prototype.getValue = function () {
return this._value;
};
PaymentMethodParameter.prototype.setValue = function (value) {
this._value = value;
};
return PaymentMethodParameter;
}());
var PaymentMethod = (function () {
function PaymentMethod(id, name, description, image, type, price, sort, enabled, uid, parameters) {
var _this = this;
if (uid === void 0) { uid = null; }
if (parameters === void 0) { parameters = []; }
this._uid = null;
this._enabled = false;
this._parameters = [];
this._id = id;
this._uid = uid;
this._name = name;
this._description = description;
this._image = image;
this._type = type;
this._price = price;
this._sort = sort;
this._enabled = enabled;
Apicart.Utils.Loops.forEach(parameters, function (paymentMethodParameter) {
_this.addParameter(paymentMethodParameter);
});
}
PaymentMethod.prototype.getId = function () {
return this._id;
};
PaymentMethod.prototype.getUid = function () {
return this._uid;
};
PaymentMethod.prototype.getName = function () {
return this._name;
};
PaymentMethod.prototype.getDescription = function () {
return this._description;
};
PaymentMethod.prototype.getImage = function () {
return this._image;
};
PaymentMethod.prototype.getType = function () {
return this._type;
};
PaymentMethod.prototype.getPrice = function () {
return this._price;
};
PaymentMethod.prototype.getSort = function () {
return this._sort;
};
PaymentMethod.prototype.isEnabled = function () {
return this._enabled;
};
PaymentMethod.prototype.getParameters = function () {
return this._parameters;
};
PaymentMethod.prototype.addParameter = function (paymentMethodParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (existingParameter) {
if (existingParameter.getKey() === paymentMethodParameter.getKey()) {
existingParameter.setValue(paymentMethodParameter.getValue());
parameterUpdated = true;
return false;
}
});
if (!parameterUpdated) {
this._parameters.push(paymentMethodParameter);
}
};
PaymentMethod.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (paymentMethodParameter, index) {
if (paymentMethodParameter.getKey() === key) {
delete _this._parameters[index];
return false;
}
});
};
PaymentMethod.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
return false;
}
});
return parameterValue;
};
PaymentMethod.prototype.serialize = function () {
var structure = {
id: this.getId(),
uid: this.getUid(),
name: this.getName(),
description: this.getDescription(),
image: this.getImage(),
type: this.getType(),
price: this.getPrice(),
sort: this.getSort(),
enabled: this.isEnabled(),
parameters: []
};
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
};
PaymentMethod.deserialize = function (data) {
Apicart.Utils.Loops.forEach(['id', 'name', 'image', 'type', 'price', 'sort', 'enabled'], function (key) {
if (!(key in data)) {
throw 'PaymentMethod: Missing required parameter: "' + key + '".';
}
});
var paymentMethod = new PaymentMethod(data.id, data.name, data.description, data.image, data.type, data.price, data.sort, data.enabled, data.uid);
if ('parameters' in data && Array.isArray(data.parameters)) {
Apicart.Utils.Loops.forEach(data.parameters, function (parameter) {
if ('key' in parameter) {
paymentMethod.addParameter(new PaymentMethodParameter(parameter.key, parameter.value || null));
}
});
}
return paymentMethod;
};
PaymentMethod.TYPE_APICART_PAYMENT_ADYEN = 'apicart-payment-adyen';
PaymentMethod.TYPE_APICART_PAYMENT_BRAINTREE = 'apicart-payment-braintree';
PaymentMethod.TYPE_APICART_PAYMENT_PAY_U = 'apicart-payment-pay_u';
PaymentMethod.TYPE_APICART_PAYMENT_STRIPE = 'apicart-payment-stripe';
PaymentMethod.TYPE_BANK_TRANSFER = 'bank-transfer';
PaymentMethod.TYPE_CASH = 'cash';
PaymentMethod.TYPE_CASH_ON_DELIVERY = 'cash-on-delivery';
PaymentMethod.TYPE_CUSTOM = 'custom';
return PaymentMethod;
}());
var ShippingMethodParameter = (function () {
function ShippingMethodParameter(key, value) {
this._key = key;
this._value = value;
}
ShippingMethodParameter.prototype.getKey = function () {
return this._key;
};
ShippingMethodParameter.prototype.getValue = function () {
return this._value;
};
ShippingMethodParameter.prototype.setValue = function (value) {
this._value = value;
};
return ShippingMethodParameter;
}());
var ShippingMethod = (function () {
function ShippingMethod(id, name, description, image, price, sort, enabled, uid, parameters) {
var _this = this;
if (uid === void 0) { uid = null; }
if (parameters === void 0) { parameters = []; }
this._uid = null;
this._enabled = false;
this._parameters = [];
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, function (shippingMethodParameter) {
_this.addParameter(shippingMethodParameter);
});
}
ShippingMethod.prototype.getId = function () {
return this._id;
};
ShippingMethod.prototype.getUid = function () {
return this._uid;
};
ShippingMethod.prototype.getName = function () {
return this._name;
};
ShippingMethod.prototype.getDescription = function () {
return this._description;
};
ShippingMethod.prototype.getImage = function () {
return this._image;
};
ShippingMethod.prototype.getPrice = function () {
return this._price;
};
ShippingMethod.prototype.getSort = function () {
return this._sort;
};
ShippingMethod.prototype.isEnabled = function () {
return this._enabled;
};
ShippingMethod.prototype.getParameters = function () {
return this._parameters;
};
ShippingMethod.prototype.addParameter = function (shippingMethodParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (existingParameter) {
if (existingParameter.getKey() === shippingMethodParameter.getKey()) {
existingParameter.setValue(shippingMethodParameter.getValue());
parameterUpdated = true;
return false;
}
});
if (!parameterUpdated) {
this._parameters.push(shippingMethodParameter);
}
};
ShippingMethod.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter, index) {
if (parameter.getKey() === key) {
delete _this._parameters[index];
return false;
}
});
};
ShippingMethod.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
return false;
}
});
return parameterValue;
};
ShippingMethod.prototype.serialize = function () {
var 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(), function (parameter) {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
};
ShippingMethod.deserialize = function (data) {
Apicart.Utils.Loops.forEach(['id', 'name', 'image', 'price', 'sort', 'enabled'], function (key) {
if (!(key in data)) {
throw 'ShippingMethod: Missing required parameter: "' + key + '".';
}
});
var 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, function (parameter) {
if ('key' in parameter) {
shippingMethod.addParameter(new ShippingMethodParameter(parameter.key, parameter.value || null));
}
});
}
return shippingMethod;
};
return ShippingMethod;
}());
var Cart = (function () {
function Cart(createdAt, updatedAt, hash, totalPrice, cartState, customer, paymentMethod, shippingMethod, items, parameters) {
var _this = this;
if (paymentMethod === void 0) { paymentMethod = null; }
if (shippingMethod === void 0) { shippingMethod = null; }
if (items === void 0) { items = []; }
if (parameters === void 0) { parameters = []; }
this._items = [];
this._parameters = [];
this._shippingMethod = null;
this._paymentMethod = null;
this._createdAt = createdAt;
this._updatedAt = updatedAt;
this._hash = hash;
this._totalPrice = totalPrice;
this._paymentMethod = paymentMethod;
this._shippingMethod = shippingMethod;
this.setCartState(cartState);
this.setCustomer(customer);
Apicart.Utils.Loops.forEach(items, function (cartItem) {
_this.addItem(cartItem);
});
Apicart.Utils.Loops.forEach(parameters, function (cartParameter) {
_this.addParameter(cartParameter);
});
}
Cart.prototype.getCreatedAt = function () {
return this._createdAt;
};
Cart.prototype.getUpdatedAt = function () {
return this._updatedAt;
};
Cart.prototype.getHash = function () {
return this._hash;
};
Cart.prototype.getTotalPrice = function () {
return this._totalPrice;
};
Cart.prototype.getCartState = function () {
return this._cartState;
};
Cart.prototype.setCartState = function (cartState) {
this._cartState = cartState;
};
Cart.prototype.getCustomer = function () {
return this._customer;
};
Cart.prototype.setCustomer = function (customer) {
this._customer = customer;
};
Cart.prototype.getItems = function () {
return this._items;
};
Cart.prototype.getTotalItemsQuantity = function () {
var quantity = 0;
Apicart.Utils.Loops.forEach(this.getItems(), function (item) {
quantity += item.getQuantity();
});
return quantity;
};
Cart.prototype.addItem = function (cartItem) {
this._items.push(cartItem);
};
Cart.prototype.removeItemByExternalId = function (externalId) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getItems(), function (item, key) {
if (item.getExternalId() === externalId) {
delete _this._items[key];
return false;
}
});
};
Cart.prototype.removeItemByDataUrl = function (dataUrl) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getItems(), function (item, key) {
if (item.getItem().getDataUrl() === dataUrl) {
delete _this._items[key];
return false;
}
});
};
Cart.prototype.findItemByExternalId = function (externalId) {
var selectedItem = null;
Apicart.Utils.Loops.forEach(this.getItems(), function (item) {
if (item.getItem().getExternalId() === externalId) {
selectedItem = item;
return false;
}
});
return selectedItem;
};
Cart.prototype.findItemByDataUrl = function (dataUrl) {
var selectedItem = null;
Apicart.Utils.Loops.forEach(this.getItems(), function (item) {
if (item.getItem().getDataUrl() === dataUrl) {
selectedItem = item;
return false;
}
});
return selectedItem;
};
Cart.prototype.getParameters = function () {
return this._parameters;
};
Cart.prototype.addParameter = function (cartParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === cartParameter.getKey()) {
parameter.setValue(cartParameter.getValue());
parameterUpdated = true;
return false;
}
});
if (!parameterUpdated) {
this._parameters.push(cartParameter);
}
};
Cart.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter, index) {
if (parameter.getKey() === key) {
delete _this._parameters[index];
return false;
}
});
};
Cart.prototype.getPaymentMethod = function () {
return this._paymentMethod;
};
Cart.prototype.getShippingMethod = function () {
return this._shippingMethod;
};
Cart.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
return false;
}
});
return parameterValue;
};
Cart.prototype.getExternalId = function () {
var externalId = this.getParameterValue('externalId');
if (!['string', 'number'].includes(typeof externalId) && externalId !== null) {
externalId = null;
}
return externalId;
};
Cart.prototype.serialize = function () {
var structure = {
createdAt: this.getCreatedAt(),
updatedAt: this.getUpdatedAt(),
hash: this.getHash(),
totalPrice: this.getTotalPrice(),
state: {
code: this.getCartState().getCode()
},
customer: this.getCustomer().serialize(),
items: [],
parameters: [],
paymentMethod: null,
shippingMethod: null
};
var paymentMethod = this.getPaymentMethod();
var shippingMethod = this.getShippingMethod();
if (paymentMethod) {
structure.paymentMethod = paymentMethod.serialize();
}
if (shippingMethod) {
structure.shippingMethod = shippingMethod.serialize();
}
Apicart.Utils.Loops.forEach(this.getItems(), function (cartItem) {
structure.items.push(cartItem.serialize());
});
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
structure.parameters.push({
key: parameter.getKey(),
value: parameter.getValue()
});
});
return structure;
};
Cart.prototype.serializeForGraphQL = function () {
var serializedCart = this.serialize();
return {
hash: serializedCart.hash,
state: serializedCart.state.code,
customerHash: serializedCart.customer.hash,
items: serializedCart.items,
parameters: serializedCart.parameters
};
};
Cart.deserialize = function (data) {
Apicart.Utils.Loops.forEach(['createdAt', 'updatedAt', 'hash', 'totalPrice', 'state.code', 'customer.hash', 'customer.state'], function (keyPath) {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'Cart: Missing required parameter: "' + keyPath + '".';
}
});
var cart = new Cart(new Date(data.createdAt), new Date(data.updatedAt), data.hash, data.totalPrice, new CartState(data.state.code), Customer.deserialize(data.customer), data.paymentMethod ? PaymentMethod.deserialize(data.paymentMethod) : null, data.shippingMethod ? ShippingMethod.deserialize(data.shippingMethod) : null);
if ('parameters' in data && Array.isArray(data.parameters)) {
Apicart.Utils.Loops.forEach(data.parameters, function (parameter) {
if ('key' in parameter) {
cart.addParameter(new CartParameter(parameter.key, parameter.value || null));
}
});
}
if ('items' in data && Array.isArray(data.items)) {
Apicart.Utils.Loops.forEach(data.items, function (item) {
cart.addItem(CartItem.deserialize(item));
});
}
return cart;
};
return Cart;
}());
var OrderState = (function () {
function OrderState(code) {
this._code = code;
}
OrderState.prototype.getCode = function () {
return this._code;
};
OrderState.prototype.setCode = function (code) {
this._code = code;
};
OrderState.CODE_NEW = 'new';
OrderState.CODE_WAITING_FOR_PAYMENT = 'waitingForPayment';
OrderState.CODE_PAID = 'paid';
OrderState.CODE_DISTRIBUTED = 'distributed';
OrderState.CODE_CANCELED = 'canceled';
return OrderState;
}());
var OrderParameter = (function () {
function OrderParameter(key, value) {
this._key = key;
this._value = value;
}
OrderParameter.prototype.getKey = function () {
return this._key;
};
OrderParameter.prototype.getValue = function () {
return this._value;
};
OrderParameter.prototype.setValue = function (value) {
this._value = value;
};
return OrderParameter;
}());
var OrderItemParameter = (function () {
function OrderItemParameter(key, value) {
this._key = key;
this._value = value;
}
OrderItemParameter.prototype.getKey = function () {
return this._key;
};
OrderItemParameter.prototype.getValue = function () {
return this._value;
};
OrderItemParameter.prototype.setValue = function (value) {
this._value = value;
};
return OrderItemParameter;
}());
var OrderItem = (function () {
function OrderItem(quantity, externalId, name, price, totalPrice, item, parameters) {
var _this = this;
if (parameters === void 0) { parameters = []; }
this._parameters = [];
this._quantity = quantity;
this._externalId = externalId;
this._name = name;
this._price = price;
this._totalPrice = totalPrice;
this._item = item;
Apicart.Utils.Loops.forEach(parameters, function (orderItemParameter) {
_this.addParameter(orderItemParameter);
});
}
OrderItem.prototype.getQuantity = function () {
return this._quantity;
};
OrderItem.prototype.getExternalId = function () {
return this._externalId;
};
OrderItem.prototype.getName = function () {
return this._name;
};
OrderItem.prototype.getPrice = function () {
return this._price;
};
OrderItem.prototype.getTotalPrice = function () {
return this._totalPrice;
};
OrderItem.prototype.getItem = function () {
return this._item;
};
OrderItem.prototype.getParameters = function () {
return this._parameters;
};
OrderItem.prototype.addParameter = function (orderItemParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === orderItemParameter.getKey()) {
parameter.setValue(orderItemParameter.getValue());
parameterUpdated = true;
return false;
}
});
if (!parameterUpdated) {
this._parameters.push(orderItemParameter);
}
};
OrderItem.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter, index) {
if (parameter.getKey() === key) {
delete _this._parameters[index];
return false;
}
});
};
OrderItem.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
return false;
}
});
return parameterValue;
};
OrderItem.deserialize = function (data) {
Apicart.Utils.Loops.forEach(['quantity', 'externalId', 'name', 'price', 'totalPrice', 'item'], function (keyPath) {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'Orderitem: Missing required parameter: "' + keyPath + '".';
}
});
var item = Item.deserialize(data.item);
var orderItem = new OrderItem(data.quantity, data.externalId, data.name, data.price, data.totalPrice, item);
if ('parameters' in data && Array.isArray(data.parameters)) {
Apicart.Utils.Loops.forEach(data.parameters, function (parameter) {
if ('key' in parameter) {
orderItem.addParameter(new OrderItemParameter(parameter.key, parameter.value || null));
}
});
}
return orderItem;
};
return OrderItem;
}());
var Order = (function () {
function Order(createdAt, updatedAt, hash, variableSymbol, totalPrice, orderState, cart, customer, items, parameters) {
var _this = this;
if (items === void 0) { items = []; }
if (parameters === void 0) { parameters = []; }
this._items = [];
this._parameters = [];
this._createdAt = createdAt;
this._updatedAt = updatedAt;
this._hash = hash;
this._variableSymbol = variableSymbol;
this._totalPrice = totalPrice;
this.setOrderState(orderState);
this.setCart(cart);
this.setCustomer(customer);
Apicart.Utils.Loops.forEach(items, function (orderItem) {
_this.addItem(orderItem);
});
Apicart.Utils.Loops.forEach(parameters, function (orderParameter) {
_this.addParameter(orderParameter);
});
}
Order.prototype.getCreatedAt = function () {
return this._createdAt;
};
Order.prototype.getUpdatedAt = function () {
return this._updatedAt;
};
Order.prototype.getHash = function () {
return this._hash;
};
Order.prototype.getVariableSymbol = function () {
return this._variableSymbol;
};
Order.prototype.getTotalPrice = function () {
return this._totalPrice;
};
Order.prototype.getOrderState = function () {
return this._orderState;
};
Order.prototype.setOrderState = function (orderState) {
this._orderState = orderState;
};
Order.prototype.getCart = function () {
return this._cart;
};
Order.prototype.setCart = function (cart) {
this._cart = cart;
};
Order.prototype.getCustomer = function () {
return this._customer;
};
Order.prototype.setCustomer = function (customer) {
this._customer = customer;
};
Order.prototype.getItems = function () {
return this._items;
};
Order.prototype.addItem = function (orderItem) {
this._items.push(orderItem);
};
Order.prototype.removeItemByExternalId = function (externalId) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getItems(), function (item, key) {
if (item.getExternalId() === externalId) {
delete _this._items[key];
return false;
}
});
};
Order.prototype.findItemByExternalId = function (externalId) {
var selectedItem = null;
Apicart.Utils.Loops.forEach(this.getItems(), function (item) {
if (item.getExternalId() === externalId) {
selectedItem = item;
return false;
}
});
return selectedItem;
};
Order.prototype.getParameters = function () {
return this._parameters;
};
Order.prototype.addParameter = function (orderParameter) {
var parameterUpdated = false;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === orderParameter.getKey()) {
parameter.setValue(orderParameter.getValue());
parameterUpdated = true;
return false;
}
});
if (!parameterUpdated) {
this._parameters.push(orderParameter);
}
};
Order.prototype.removeParameter = function (key) {
var _this = this;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter, index) {
if (parameter.getKey() === key) {
delete _this._parameters[index];
return false;
}
});
};
Order.prototype.getParameterValue = function (key, defaultValue) {
if (defaultValue === void 0) { defaultValue = null; }
var parameterValue = defaultValue;
Apicart.Utils.Loops.forEach(this.getParameters(), function (parameter) {
if (parameter.getKey() === key) {
parameterValue = parameter.getValue();
return false;
}
});
return parameterValue;
};
Order.prototype.getExternalId = function () {
var externalId = this.getParameterValue('externalId');
if (!['string', 'number'].includes(typeof externalId) && externalId !== null) {
externalId = null;
}
return externalId;
};
Order.deserialize = function (data) {
Apicart.Utils.Loops.forEach([
'createdAt', 'updatedAt', 'hash', 'variableSymbol', 'totalPrice', 'state.code', 'cart.hash',
'customer.hash', 'customer.state'
], function (keyPath) {
if (!Apicart.Utils.Objects.keyExists(data, keyPath)) {
throw 'Order: Missing required parameter: "' + keyPath + '".';
}
});
var order = new Order(new Date(data.createdAt), new Date(data.updatedAt), data.hash, data.variableSymbol, data.totalPrice, new OrderState(data.state.code), Cart.deserialize(data.cart), Customer.deserialize(data.customer));
if ('parameters' in data && Array.isArray(data.parameters)) {
Apicart.Utils.Loops.forEach(data.parameters, function (parameter) {
if ('key' in parameter) {
order.addParameter(new OrderParameter(parameter.key, parameter.value || null));
}
});
}
if ('items' in data && Array.isArray(data.items)) {
Apicart.Utils.Loops.forEach(data.items, function (item) {
order.addItem(OrderItem.deserialize(item));
});
}
return order;
};
return Order;
}());
var Api = (function () {
function Api(token, parameters) {
if (parameters === void 0) { parameters = {}; }
this._parameters = {};
if (!Apicart.Utils.Objects.keyExists(parameters, 'url')) {
throw 'Parameter "dataSource.url" is missing.';
}
this._token = token;
this._parameters = parameters;
}
Api.prototype.findCart = function (hash) {
return __awaiter(this, void 0, void 0, function () {
var query, response, responseData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
query = "\n\t\t\tquery findCart($input: FindCartQueryInput!){\n\t\t\t\tfindCart(input: $input) {\n\t\t\t\t\tcart {\n\t\t\t\t\t\tall\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}";
return [4, this.call(query, {
input: {
cartHash: hash
}
})];
case 1:
response = _a.sent();
responseData = this.getResponseData(response, 'findCart.cart.all');
return [2, responseData ? Cart.deserialize(responseData) : null];
}
});
});
};
Api.prototype.fullCartSynchronization = function (cart) {
return __awaiter(this, void 0, void 0, function () {
var query, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
query = "\n\t\t\tmutation fullCartSynchronization($input: FullCartSynchronizationMutationInput!){\n\t\t\t\tfullCartSynchronization(input: $input) {\n\t\t\t\t\tresult\n\t\t\t\t}\n\t\t\t}";
return [4, this.call(query, {
input: {
cart: cart.serializeForGraphQL()
}
})];
case 1:
response = _a.sent();
return [2, this.isSuccessResult(response, 'fullCartSynchronization')];
}
});
});