Skip to content

feat: add stock in products #1042

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 vnda/loaders/productDetailsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function loader(
const result = await api["GET /api/v2/products/:id"]({
id,
include_images: "true",
include_inventory_place: "true"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as it is optional on the API this should be optional on the loader, make it a property

}, STALE);
return result.json();
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion vnda/utils/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface API {
/** @docs https://developers.vnda.com.br/reference/get-api-v2-products-id */
"GET /api/v2/products/:id": {
response: ProductGroup;
searchParams: { include_images: boolean };
searchParams: { include_images: boolean, include_inventory_place: boolean };
};

/** @docs https://developers.vnda.com.br/reference/get-api-v2-products-product_id-price */
Expand Down
16 changes: 16 additions & 0 deletions vnda/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ProductInstallment,
ProductSearch,
ProductVariant,
ProductVariantInventory,
VariantProductSearch,
} from "./openapi/vnda.openapi.gen.ts";
import { getSegmentFromCookie, parse } from "./segment.ts";
Expand Down Expand Up @@ -246,6 +247,19 @@ const toPropertyValueCategoryTags = (
});
};

const toPropertyValueInventory = (inventories?: ProductVariantInventory[]) => {
if (!inventories) return []
return inventories.map((inventory) => {
return {
"@type": "PropertyValue",
name: inventory.name ?? inventory.place_name,
value: inventory.quantity.toString(),
description: inventory.slug,
valueReference: "INVENTORY"
} as PropertyValue
})
}

// deno-lint-ignore no-explicit-any
const isProductVariant = (p: any): p is VariantProductSearch =>
typeof p.id === "number";
Expand Down Expand Up @@ -298,6 +312,7 @@ export const toProduct = (

const productID = `${variant.sku}`;
const productGroupID = `${product.id}`;
const inventories = variant.inventories as ProductVariantInventory[]

const myTags = "tags" in product ? product.tags : [];
const myCategoryTags = "category_tags" in product
Expand All @@ -315,6 +330,7 @@ export const toProduct = (
...toPropertyValue(variant),
...toPropertyValueTags(myTags),
...toPropertyValueCategoryTags(myCategoryTags),
...toPropertyValueInventory(inventories)
],
inProductGroupWithID: productGroupID,
gtin: product.reference,
Expand Down
Loading