@klevu/core
Version:
Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.
67 lines (66 loc) • 3.17 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.personalisation = void 0;
const index_js_1 = require("../../models/index.js");
const lastClickedProducts_js_1 = require("../../store/lastClickedProducts.js");
/**
* Enable personlisation to the query
*
* @category Modifier
* @param options
* @returns
*/
function personalisation(options) {
return {
klevuModifierId: "personalisation",
modifyAfter: (queries, klevuFunc) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const copy = Array.from(queries);
for (const q of queries) {
if (!q.settings) {
q.settings = {};
}
q.settings.personalisation = {
enablePersonalisation: true,
fields: options === null || options === void 0 ? void 0 : options.fields,
};
let records = [];
if ((options === null || options === void 0 ? void 0 : options.lastClickedProductIds) || (options === null || options === void 0 ? void 0 : options.lastClickedItemIds)) {
const ids = (_a = (options.lastClickedItemIds || options.lastClickedProductIds)) !== null && _a !== void 0 ? _a : [];
records = ids.map((pId) => ({
id: pId,
}));
}
else if (klevuFunc.klevuFunctionId === "categoryMerchandising" &&
((_b = klevuFunc.params) === null || _b === void 0 ? void 0 : _b.category)) {
const { category } = klevuFunc.params;
records = lastClickedProducts_js_1.KlevuLastClickedProducts.getCategoryPersonalisationIds(category).map((id) => ({ id }));
}
else {
records = lastClickedProducts_js_1.KlevuLastClickedProducts.getLastClickedLatestsFirst(10, true).map((id) => ({ id }));
}
if (records.length > 0) {
q.settings.context = {
recentObjects: [
{
typeOfRecord: index_js_1.KlevuTypeOfRecord.Product,
records,
},
],
};
}
}
return copy;
}),
};
}
exports.personalisation = personalisation;