@zhiguang-gastrofy/capi
Version:
comany apis, including Northfork api and Gastrofy api
281 lines • 11.4 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var http_service_1 = require("./http.service");
var utils_1 = require("./utils");
var axios_1 = require("axios");
var CancelToken = axios_1.default.CancelToken;
var ResolveService = /** @class */ (function () {
function ResolveService(config) {
this.total = {};
this.products = [];
this.ingredients = [];
this.resolving = false;
config = __assign({}, config || {});
this._NFHttp = new http_service_1.HttpService(__assign({
host: 'https://api.northfork.se/api',
}, config));
}
ResolveService.prototype.cancel = function (type) {
if ((!type || type === 'main') && this._mainCancel)
this._mainCancel();
if (!type || type === 'diff') {
}
};
/**
* the recipe id can be gastrofy_id or external_id:
* { gastrofy_id: 1, portions: 1 }
* OR
* { external_id: 1, portions: 1 }
*
* options: {
* tags: Array,
* storeProvider: String,
* storeIdentifier: String',
* productExceptions: Array,
* calculation: price | waste,
* prioritization: String,
* promotedBrand: String,
* params: {}
* }
*/
ResolveService.prototype.resolve = function (recipes, options) {
var _this = this;
this.resolving = true;
return this._NFHttp.post('smart-cart/recipes/resolve', {
recipes: recipes,
store: {
tags: options.tags || [],
provider: options.storeProvider,
identifier: options.storeIdentifier,
},
options: {
calculation: options.calculation,
prioritization: options.prioritization,
product_exceptions: options.productExceptions,
},
}, {
params: options.params,
promoted_brand: options.promotedBrand,
cancelToken: new CancelToken(function (c) {
_this._mainCancel = c;
}),
}).then(function (res) {
_this.resolving = false;
var data = res.data;
_this.products = _this._formatProducts(data.products);
_this.ingredients = _this._formaIngredients(data.recipes);
_this._recipes = data.recipes;
_this._calculateTotal();
return _this;
}, function (err) {
_this.resolving = false;
return err;
});
};
// substitute can be new quantity or new product
ResolveService.prototype.substituteProduct = function (identifier, substitute) {
var original = null;
for (var _i = 0, _a = this.products; _i < _a.length; _i++) {
var product = _a[_i];
if (product.substitute_identifier === identifier) {
original = product;
break;
}
}
if (!original)
return;
// TODO: TO FIX:
// NOTE: The consumption of the replaced product is 0 from api, so set to 0 at frontend now
original.consumption = 0;
original.replaced = true;
if (typeof substitute === 'number') {
original.quantity = substitute;
}
else {
for (var field in original) {
if (field === 'substitute_identifier' || typeof substitute[field] === 'undefined')
continue;
original[field] = substitute[field];
}
}
this._calculateTotal();
};
ResolveService.prototype.productsBySections = function () {
var sharedProducts = [];
var commonProducts = [];
var recipesProducts = [];
for (var _i = 0, _a = this.products; _i < _a.length; _i++) {
var product = _a[_i];
if (product.common)
commonProducts.push(product);
else if (product.shared)
sharedProducts.push(product);
else
recipesProducts.push(product);
}
return {
common: commonProducts,
shared: sharedProducts,
recipes: this._recipes.map(function (recipe) {
var recipeProducts = [];
for (var _i = 0, _a = recipe.ingredients; _i < _a.length; _i++) {
var ingredient = _a[_i];
for (var _b = 0, recipesProducts_1 = recipesProducts; _b < recipesProducts_1.length; _b++) {
var product = recipesProducts_1[_b];
for (var _c = 0, _d = product.recipe_ingredient_ids; _c < _d.length; _c++) {
var ingredientId = _d[_c];
if (ingredientId !== ingredient.id)
continue;
recipeProducts.push(product);
}
}
}
return {
id: recipe.id,
title: recipe.title,
portions: recipe.portions,
image_url: utils_1.parseImage(recipe),
products: recipeProducts,
};
}),
};
};
ResolveService.prototype.productsByCategories = function () {
var index = {};
var result = [];
for (var _i = 0, _a = this.products; _i < _a.length; _i++) {
var product = _a[_i];
var category = product.categories[0] || { id: -1, sort_order: 999999999, title: null };
var categoryId = category.id;
var productIndex = index[categoryId];
if (productIndex === undefined) {
productIndex = result.length;
index[categoryId] = productIndex;
result[productIndex] = {
id: category.id,
title: category.title,
sort_order: category.sort_order,
products: [],
};
}
result[productIndex].products.push(product);
}
result.sort(function (a, b) {
if (a.sort_order > b.sort_order)
return 1;
return -1;
});
return result;
};
ResolveService.prototype._formatProducts = function (data) {
var _this = this;
var products = [];
for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
var productGroup = data_1[_i];
products = products.concat(productGroup.map(function (product) {
product.brand = product.brand && product.brand !== 'na' ? product.brand : null;
product.price = utils_1.parsePrice(product.price);
product.is_active = product.price > 0 && (product.strategy !== 'out_of_stock' && product.strategy !== 'replaced_out_of_stock');
product.replaced = product.strategy === 'replaced' || product.strategy === 'replaced_out_of_stock';
product.recipe_ingredient_ids = product.recipe_ingredient_ids.map(function (item) { return item.id; });
product.common = product.common === true;
product.shared = product.recipe_ingredient_ids.length > 1;
product.name = product.name.trim();
delete product.strategy;
delete product.partner_attributes;
delete product.ean;
delete product.sub_text;
product.ingredients = (function (ids) {
return function () {
return ids.map(function (id) {
for (var _i = 0, _a = _this.ingredients; _i < _a.length; _i++) {
var ingredient = _a[_i];
if (ingredient.id === id)
return ingredient;
}
});
};
})(product.recipe_ingredient_ids);
return product;
}));
}
return products;
};
ResolveService.prototype._formaIngredients = function (recipes) {
var _this = this;
var ingredients = [];
for (var _i = 0, recipes_1 = recipes; _i < recipes_1.length; _i++) {
var recipe = recipes_1[_i];
ingredients = ingredients.concat(recipe.ingredients.map(function (ingredient) {
ingredient.amount = ingredient.amount.replace(/(\.\w)0$/, function (v, m) {
return m > 0 ? m : '';
});
ingredient.products = (function (ingredientId) {
return function () {
var result = [];
for (var _i = 0, _a = _this.products; _i < _a.length; _i++) {
var product = _a[_i];
for (var _b = 0, _c = product.recipe_ingredient_ids; _b < _c.length; _b++) {
var id = _c[_b];
if (id === ingredientId) {
result.push(product);
break;
}
}
}
return result;
};
})(ingredient.id);
return ingredient;
}));
}
return ingredients;
};
ResolveService.prototype._calculateTotal = function () {
var total = {
price: 0,
price_with_common: 0,
consumption_price: 0,
consumption_price_with_common: 0,
quantity: 0,
quantity_with_common: 0,
checkout_price: 0,
checkout_quantity: 0,
};
for (var _i = 0, _a = this.products; _i < _a.length; _i++) {
var product = _a[_i];
var curTotal = product.price * product.quantity;
var consumptionPrice = curTotal * product.consumption;
total.price_with_common += curTotal;
total.consumption_price_with_common += consumptionPrice;
total.quantity_with_common += product.quantity;
if (!product.common || product.replaced) {
total.price += curTotal;
total.consumption_price += consumptionPrice;
total.quantity += product.quantity;
product.quantity = product.quantity;
if (product.is_active) {
total.checkout_price += curTotal;
total.checkout_quantity += product.quantity;
}
}
}
for (var totalField in total) {
this.total[totalField] = utils_1.decimalPrice(total[totalField]);
}
};
return ResolveService;
}());
exports.ResolveService = ResolveService;
//# sourceMappingURL=resolve.service.js.map