Skip to content

Commit

Permalink
Tavano/change auctions (#1008)
Browse files Browse the repository at this point in the history
* change auctions

* remove cache in module

* fix apiList
  • Loading branch information
guitavano authored Feb 13, 2025
1 parent cc4abfb commit 48c3a7a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
18 changes: 4 additions & 14 deletions linx/loaders/auction/apiList.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
import { Auction } from "../../utils/types/auctionAPI.ts";
import type { AppContext } from "../../../linx/mod.ts";

let cachedPromise: Promise<Auction[]> | null = null;
let lastUpdate: number = Date.now();
/**
* @title Linx Integration
* @description Search Wishlist loader
*/
const loader = (
const loader = async (
_props: unknown,
_req: Request,
ctx: AppContext,
): Promise<Auction[] | null> => {
const { layer } = ctx;

if (cachedPromise && Date.now() - lastUpdate < (60 * 1000)) {
return cachedPromise;
}

lastUpdate = Date.now();
const responsePromise = layer
const responsePromise = await layer
["POST /v1/Catalog/API.svc/web/SearchProductAuctions"](
{},
// @ts-ignore body is required
{ body: {} },
).then(async (response) => {
return await response.json();
});
);

cachedPromise = responsePromise;
return responsePromise;
return await responsePromise.json();
};

export const cache = "stale-while-revalidate";
Expand Down
14 changes: 6 additions & 8 deletions linx/loaders/product/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,18 @@ const loader = async (

const products = response?.Model?.Grid?.Products ?? [];

const transformedProducts = [];
for (const product of products) {
const transformedProduct = await addAuctions(
const leiloes = await ctx.invoke.linx.loaders.auction.apiList();

return products.map((product) => {
return addAuctions(
toProduct(product, product.ProductSelection?.SkuID, {
cdn,
url,
currency: "BRL",
}),
ctx,
leiloes,
);
transformedProducts.push(transformedProduct);
}

return transformedProducts;
});
};

export default loader;
12 changes: 6 additions & 6 deletions linx/loaders/product/listingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ const loader = async (
} = forProducts;
const { Model: { Grid: { Facets } } } = forProducts;

const products = [];
for (const product of Products) {
const transformedProduct = await addAuctions(
const leiloes = await ctx.invoke.linx.loaders.auction.apiList();

const products = Products.map((product) => {
return addAuctions(
toProduct(product, product.ProductSelection?.SkuID, {
cdn,
currency: "BRL",
url,
}),
ctx,
leiloes,
);
products.push(transformedProduct);
}
});

return {
"@type": "ProductListingPage",
Expand Down
9 changes: 5 additions & 4 deletions linx/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ProductAuction } from "./types/auctionJSON.ts";
import { Model as ProductAuctionDetail } from "./types/auctionDetailJSON.ts";
import { Product as LinxProductGetByIdJSON } from "./types/productByIdJSON.ts";
import { Associations } from "./types/associationsJSON.ts";
import type { AppContext } from "../mod.ts";
import { Auction } from "./types/auctionAPI.ts";

type LinxProductGroup =
| LinxProductGroupList
Expand Down Expand Up @@ -59,9 +59,10 @@ const pickVariant = (variants: LinxProduct[], variantId: number | null) => {
return variants[0];
};

export const addAuctions = async (product: Product, ctx: AppContext) => {
const leiloes = await ctx.invoke.linx.loaders.auction.apiList();

export const addAuctions = (
product: Product,
leiloes: Auction[] | null,
) => {
const auctionPropertyIndex = product.additionalProperty?.findIndex(
(prop) => prop.name === "id_leilao",
);
Expand Down

0 comments on commit 48c3a7a

Please sign in to comment.