Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: product list return kitlook array #98

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commerce/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export interface Product extends Omit<Thing, "@type"> {
sku: string;
/** A pointer to another product (or multiple products) for which this product is an accessory or spare part. */
isAccessoryOrSparePartFor?: ProductLeaf[];
kitItems?: Product[];
}

export interface ListItem<T = string> extends Omit<Thing, "@type"> {
Expand Down
16 changes: 14 additions & 2 deletions vtex/loaders/intelligentSearch/productList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Product } from "../../../commerce/types.ts";
import type { Product as VTEXProduct } from "../../utils/types.ts";
import { STALE } from "../../../utils/fetch.ts";
import { AppContext } from "../../mod.ts";
import {
Expand All @@ -10,6 +11,7 @@ import { getSegment, withSegmentCookie } from "../../utils/segment.ts";
import { withIsSimilarTo } from "../../utils/similars.ts";
import { toProduct } from "../../utils/transform.ts";
import type { ProductID, Sort } from "../../utils/types.ts";
import { withIsKitlookTo } from "../../utils/kitlook.ts";

export interface CollectionProps extends CommonProps {
// TODO: pattern property isn't being handled by RJSF
Expand Down Expand Up @@ -50,6 +52,11 @@ export interface CommonProps {
* @description Do not return out of stock items
*/
hideUnavailableItems?: boolean;
/**
* @title Active Kit Items
* @description Send kit items product by product
*/
isKit?: boolean;
/**
* @description Include similar products
*/
Expand Down Expand Up @@ -127,17 +134,22 @@ const loader = async (
...params,
facets: toPath(facets),
}, { ...STALE, headers: withSegmentCookie(segment) })
.then((res) => res.json());
.then((res: Response) => res.json()) as { products: VTEXProduct[] };

const options = {
baseUrl: url,
priceCurrency: "BRL", // config!.defaultPriceCurrency, // TOO
};

// if isKit is true, we need to fetch the kit items for each product
const currentProducts = props.isKit
? await withIsKitlookTo({ vtexProducts, ctx, params, options })
: vtexProducts;

// Transform VTEX product format into schema.org's compatible format
// If a property is missing from the final `products` array you can add
// it in here
const products = vtexProducts
const products = currentProducts
.map((p) => toProduct(p, p.items[0], 0, options));

return Promise.all(
Expand Down
87 changes: 87 additions & 0 deletions vtex/utils/kitlook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { Product } from "../../commerce/types.ts";
import type { LegacyProduct, Product as VTEXProduct } from "../utils/types.ts";
import type { AppContext } from "../mod.ts";
import type { Sort } from "../utils/types.ts";

import { toProduct } from "../utils/transform.ts";
import { getSegment, withSegmentCookie } from "../utils/segment.ts";

interface Params {
query: string;
page: number;
count: number;
sort: Sort;
fuzzy: string;
locale?: string;
hideUnavailableItems: boolean;
}

interface WithKitLookToProps {
vtexProducts: VTEXProduct[];
params: Params;
options: {
baseUrl: string;
priceCurrency: string;
};
ctx: AppContext;
}

interface GETProductKitLookItem {
product: LegacyProduct;
ctx: AppContext;
params: Params;
}

/** Retrieves a KitLook items for a given product. */
async function getKitlookItem({ product, ctx, params }: GETProductKitLookItem) {
const { vcsDeprecated } = ctx;
const { items } = product;
if (!items[0]?.isKit || !product.items[0]?.kitItems) return [];

const kitItems = await vcsDeprecated
["GET /api/catalog_system/pub/products/search/:term?"]({
...params,
fq: product.items[0].kitItems.map((item) => `skuId:${item.itemId}`),
}, { deco: { cache: "stale-while-revalidate" } });

return kitItems.json();
}

/** Retrieves VTEX products with Kitlook. */
export async function withIsKitlookTo(
{ vtexProducts, params, options, ctx }: WithKitLookToProps,
): Promise<Array<LegacyProduct & { kitItems: Product[] }>> {
const { vcsDeprecated } = ctx;
const segment = getSegment(ctx);

const productIds = vtexProducts.map((p) => p.productId);

const products: LegacyProduct[] = await vcsDeprecated
[
`GET /api/catalog_system/pub/products/search/:term?`
](
{
...params,
fq: productIds.map((productId) => `productId:${productId}`),
},
{
deco: { cache: "stale-while-revalidate" },
headers: withSegmentCookie(segment),
},
).then((res: Response) => res.json());

const formattedProducts = await Promise.all(products.map(async (product) => {
const kitItems: LegacyProduct[] = await getKitlookItem({
product,
ctx,
params,
});

return {
...product,
kitItems: kitItems.map((p) => toProduct(p, p.items[0], 0, options)),
};
}));

return formattedProducts;
}
3 changes: 2 additions & 1 deletion vtex/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export const aggregateOffers = (
};

export const toProduct = <P extends LegacyProductVTEX | ProductVTEX>(
product: P,
product: P & { kitItems?: Product[] },
sku: P["items"][number],
level = 0, // prevent inifinte loop while self referencing the product
options: ProductOptions,
Expand Down Expand Up @@ -347,6 +347,7 @@ export const toProduct = <P extends LegacyProductVTEX | ProductVTEX>(
return {
"@type": "Product",
category: categoriesString,
kitItems: product?.kitItems,
productID: skuId,
url: getProductURL(baseUrl, product, sku.itemId).href,
name,
Expand Down
Loading