diff --git a/smarthint/loaders/productList.ts b/smarthint/loaders/productList.ts new file mode 100644 index 000000000..098ac1f3d --- /dev/null +++ b/smarthint/loaders/productList.ts @@ -0,0 +1,110 @@ +import { Product } from "../../commerce/types.ts"; +import { AppContext } from "../mod.ts"; +import { getFilterParam, toProduct } from "../utils/transform.ts"; +import { ComplexPageType, FilterProp } from "../utils/typings.ts"; +import { getSessionCookie } from "../utils/getSession.ts"; +import { getCategoriesParam, getProductParam } from "./recommendations.ts"; + +export interface Props { + /** + * @hide + */ + filter?: FilterProp[]; + /** + * @hide + */ + categories?: string; + /** + * @hide + */ + products?: string[]; + /** + * @description Your recommendations are divided by positions, defining which position of the recommendations according to your desire. All recommendations configured in the Admin Panel will be returned. + */ + position: string; + /** + * @description Type of page you are setting up. + */ + pagetype: ComplexPageType; + /** + * @default padrao + */ + channel?: string; +} + +/** + * @title SmartHint Integration + * @description Product List from Recommendations (for ProductShelf) + */ +const loader = async ( + props: Props, + req: Request, + ctx: AppContext, +): Promise => { + const { recs, shcode, publicUrl, categoryTree } = ctx; + const { + categories: categoriesParam, + filter = [], + position, + products: productsParam = [], + pagetype, + channel = "padrao", + } = props; + + const url = new URL(req.url); + + const { anonymous } = getSessionCookie(req.headers); + + const pageIdentifier = new URL(url.pathname, publicUrl)?.href; + + const filters = getFilterParam(url, filter); + + const productsString = getProductParam(pagetype, productsParam); + + const categories = getCategoriesParam({ + categoriesParam, + categoryTree, + url, + }); + + const data = await recs["GET /recommendationByPage/withProducts"]({ + shcode, + anonymous, + categories, + channel, + filter: filters, + pageIdentifier, + pagetype: pagetype.type, + position, + products: productsString, + }).then((r) => r.json()); + + const positionItem = data.find((item) => + Number(item.SmartHintPosition) == Number(position) + ); + + if (!positionItem) return null; + + const products: Product[] = []; + + // Extract products from RecommendationsProducts + positionItem.RecommendationsProducts?.forEach((rec) => { + rec.Products?.forEach((p) => products.push(toProduct(p))); + }); + + // Extract products from RecommendationsPromotional + positionItem.RecommendationsPromotional?.forEach((rec) => { + rec.Products?.forEach((p) => products.push(toProduct(p))); + }); + + // Extract products from RecommendationsCombination combos + positionItem.RecommendationsCombination?.forEach((rec) => { + rec.combos?.forEach((combo) => { + combo.Products?.forEach((p) => products.push(toProduct(p))); + }); + }); + + return products.length ? products : null; +}; + +export default loader; diff --git a/smarthint/loaders/recommendations.ts b/smarthint/loaders/recommendations.ts index 1b8b6876a..59210794f 100644 --- a/smarthint/loaders/recommendations.ts +++ b/smarthint/loaders/recommendations.ts @@ -35,7 +35,7 @@ export interface Props { channel?: string; } -function getProductParam(pagetype: ComplexPageType, productsParam: string[]) { +export function getProductParam(pagetype: ComplexPageType, productsParam: string[]) { if (productsParam.length) { return productsParam.map((productId) => `productid:${productId}`).join("&"); } diff --git a/smarthint/manifest.gen.ts b/smarthint/manifest.gen.ts index 546e97e04..da7fae7b4 100644 --- a/smarthint/manifest.gen.ts +++ b/smarthint/manifest.gen.ts @@ -8,6 +8,7 @@ import * as $$$1 from "./loaders/autocomplete.ts"; import * as $$$2 from "./loaders/banners.ts"; import * as $$$0 from "./loaders/PLPBanners.ts"; import * as $$$3 from "./loaders/productListingPage.ts"; +import * as $$$5 from "./loaders/productList.ts"; import * as $$$4 from "./loaders/recommendations.ts"; import * as $$$$$$0 from "./sections/Analytics/SmarthintTracking.tsx"; @@ -16,6 +17,7 @@ const manifest = { "smarthint/loaders/autocomplete.ts": $$$1, "smarthint/loaders/banners.ts": $$$2, "smarthint/loaders/PLPBanners.ts": $$$0, + "smarthint/loaders/productList.ts": $$$5, "smarthint/loaders/productListingPage.ts": $$$3, "smarthint/loaders/recommendations.ts": $$$4, },