Skip to content

Commit

Permalink
Tavano/linx (#1004)
Browse files Browse the repository at this point in the history
* Release [0.56.13]

* tavano linx logs

* remove cf headers

* new headers

* Update transform.ts (#955)

Added additionalProperty displayPrice in transform.ts linx

* Fix auctions

* remove console log

* check

---------

Co-authored-by: viktormarinho <viktormpcs@gmail.com>
Co-authored-by: italofg <99489312+italofg@users.noreply.github.com>
Co-authored-by: Matheus Gaudencio do Rêgo <matheusgr@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 13, 2025
1 parent dde531f commit 6aff05c
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 15 deletions.
43 changes: 43 additions & 0 deletions linx/loaders/auction/apiList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 = (
_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
["POST /v1/Catalog/API.svc/web/SearchProductAuctions"](
{},
// @ts-ignore body is required
{ body: {} },
).then(async (response) => {
return await response.json();
});

cachedPromise = responsePromise;
return responsePromise;
};

export const cache = "stale-while-revalidate";

export const cacheKey = (_props: unknown, req: Request, _ctx: AppContext) => {
const url = new URL(req.url);
url.pathname = "/v1/Catalog/API.svc/web/SearchProductAuctions";
return url.href;
};

export default loader;
8 changes: 7 additions & 1 deletion linx/loaders/auction/detailsPage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AppContext } from "../../../linx/mod.ts";
import { nullOnNotFound } from "../../../utils/http.ts";
import { removeCFHeaders } from "../../../website/handlers/proxy.ts";
import { isAuctionDetailModel } from "../../utils/paths.ts";
import { toAuctionDetail } from "../../utils/transform.ts";
import { Model as AuctionDetail } from "../../utils/types/auctionDetailJSON.ts";
Expand All @@ -17,10 +18,15 @@ const loader = async (
const upstream = new URL(req.url);
const splat = upstream.pathname.slice(1);

const headers = new Headers(req.headers);
removeCFHeaders(headers);

const response = await api["GET /*splat"]({
splat,
}, {
headers: req.headers,
headers: {
...headers,
},
}).catch(nullOnNotFound);

if (response === null) {
Expand Down
19 changes: 12 additions & 7 deletions linx/loaders/product/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { STALE } from "../../../utils/fetch.ts";
import { nullOnNotFound } from "../../../utils/http.ts";
import type { AppContext } from "../../mod.ts";
import { isGridProductsModel } from "../../utils/paths.ts";
import { toProduct } from "../../utils/transform.ts";
import { addAuctions, toProduct } from "../../utils/transform.ts";

export interface Props {
/** @description e.g.: /listas/vitrine-destaque */
Expand Down Expand Up @@ -39,12 +39,17 @@ const loader = async (

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

return products.map((product) =>
toProduct(product, product.ProductSelection?.SkuID, {
cdn,
url,
currency: "BRL",
})
return await Promise.all(
products.map(async (product) =>
await addAuctions(
toProduct(product, product.ProductSelection?.SkuID, {
cdn,
url,
currency: "BRL",
}),
ctx,
)
),
);
};

Expand Down
18 changes: 12 additions & 6 deletions linx/loaders/product/listingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ProductListingPage } from "../../../commerce/types.ts";
import { AppContext } from "../../mod.ts";
import { isGridProductsModel } from "../../utils/paths.ts";
import {
addAuctions,
toBreadcrumbList,
toFilters,
toProduct,
Expand Down Expand Up @@ -77,12 +78,17 @@ const loader = async (
} = forProducts;
const { Model: { Grid: { Facets } } } = forProducts;

const products = Products.map((product) =>
toProduct(product, product.ProductSelection?.SkuID, {
cdn,
currency: "BRL",
url,
})
const products = await Promise.all(
Products.map(async (product) =>
await addAuctions(
toProduct(product, product.ProductSelection?.SkuID, {
cdn,
currency: "BRL",
url,
}),
ctx,
)
),
);

return {
Expand Down
2 changes: 2 additions & 0 deletions linx/manifest.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as $$$$0 from "./handlers/sitemap.ts";
import * as $$$1 from "./loaders/auction/detailsPage.ts";
import * as $$$2 from "./loaders/auction/list.ts";
import * as $$$0 from "./loaders/auction/ListBids.ts";
import * as $$$100000 from "./loaders/auction/apiList.ts";
import * as $$$3 from "./loaders/cart.ts";
import * as $$$4 from "./loaders/page.ts";
import * as $$$5 from "./loaders/pages.ts";
Expand All @@ -40,6 +41,7 @@ const manifest = {
"linx/loaders/auction/detailsPage.ts": $$$1,
"linx/loaders/auction/list.ts": $$$2,
"linx/loaders/auction/ListBids.ts": $$$0,
"linx/loaders/auction/apiList.ts": $$$100000,
"linx/loaders/cart.ts": $$$3,
"linx/loaders/page.ts": $$$4,
"linx/loaders/pages.ts": $$$5,
Expand Down
6 changes: 6 additions & 0 deletions linx/utils/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ShareWishlistResponse,
} from "./types/wishlistJSON.ts";

import { Auction } from "./types/auctionAPI.ts";

export interface LayerAPI {
"POST /v1/Profile/API.svc/web/SearchWishlist": {
response: SearchWishlistResponse;
Expand All @@ -21,6 +23,10 @@ export interface LayerAPI {
};
};

"POST /v1/Catalog/API.svc/web/SearchProductAuctions": {
response: Auction[];
};

"POST /v1/Profile/API.svg/web/ShareWishlist": {
response: ShareWishlistResponse;
body: {
Expand Down
48 changes: 48 additions & 0 deletions linx/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +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";

type LinxProductGroup =
| LinxProductGroupList
Expand Down Expand Up @@ -58,6 +59,45 @@ 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();

const auctionPropertyIndex = product.additionalProperty?.findIndex(
(prop) => prop.name === "id_leilao",
);

if (auctionPropertyIndex !== undefined && auctionPropertyIndex !== -1) {
product.additionalProperty?.splice(auctionPropertyIndex, 1);
}

const now = new Date().getTime() - (3 * 60 * 60 * 1000);

const parseDate = (dateString: string) => {
const match = dateString.match(/\/Date\((\d+)([-+]\d{4})?\)\//);
return match ? parseInt(match[1], 10) : null;
};

leiloes?.find((leilao) => {
const visibleFrom = parseDate(leilao.VisibleFrom);
const visibleTo = parseDate(leilao.VisibleTo);

let isVisible = (visibleFrom !== null && now >= visibleFrom) ||
(visibleFrom === null);
isVisible &&= (visibleTo !== null && now <= visibleTo) ||
(visibleTo === null);
isVisible &&= visibleTo !== null && visibleFrom !== null;
if (`${leilao.SkuID}` === product.productID && isVisible) {
product.additionalProperty = product.additionalProperty || [];
product.additionalProperty.push({
"@type": "PropertyValue",
name: "id_leilao",
value: `${leilao.ProductAuctionID}`,
});
}
});
return product;
};

const toOffer = (variant: LinxProduct, product: LinxProductGroup): Offer => {
const item = variant.Price ? variant : product;
const priceSpecification: UnitPriceSpecification[] = [
Expand Down Expand Up @@ -205,12 +245,20 @@ export const toProduct = (
additionalType: "categoryItem",
}));

const displayPrice = {
"@type": "PropertyValue" as const,
name: product.DisplayPrice,
value: product.DisplayPrice,
additionalType: "displayPrice",
};

const additionalProperty = [
...skuOptions,
...metadatas,
...descriptions,
...prodOptions,
...categoryItems,
displayPrice,
];

const hasVariant = level < 1
Expand Down
18 changes: 18 additions & 0 deletions linx/utils/types/auctionAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type Auction = {
BidCount: number;
ExecutionFrom: string;
ExecutionTo: string;
IsVisible: boolean;
LastBidAmount: number;
LastBidCustomerID: number;
LastBidCustomerName: string;
LastBidCustomerState: string;
Name: string;
ProductAuctionID: number;
ProductID: number;
SkuID: number;
Status: number;
StatusInternal: string;
VisibleFrom: string;
VisibleTo: string;
};
1 change: 0 additions & 1 deletion linx/utils/types/auctionJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ export type Alias =
| "WarrantyDescription"
| "LongDescription"
| "Specifications"
| "id_leilao"
| "SelecionarAro"
| "Pedra"
| "rank_products_mostseen"
Expand Down

0 comments on commit 6aff05c

Please sign in to comment.