@klevu/core
Version:
Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.
63 lines (62 loc) • 2.96 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());
});
};
import { KlevuTypeOfRecord } from "../../models/index.js";
import { KlevuLastClickedProducts } from "../../store/lastClickedProducts.js";
/**
* Enable personlisation to the query
*
* @category Modifier
* @param options
* @returns
*/
export 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 = KlevuLastClickedProducts.getCategoryPersonalisationIds(category).map((id) => ({ id }));
}
else {
records = KlevuLastClickedProducts.getLastClickedLatestsFirst(10, true).map((id) => ({ id }));
}
if (records.length > 0) {
q.settings.context = {
recentObjects: [
{
typeOfRecord: KlevuTypeOfRecord.Product,
records,
},
],
};
}
}
return copy;
}),
};
}