Skip to content

Commit b4ef99d

Browse files
committed
feat: add stock in products
1 parent d40cc4c commit b4ef99d

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

vnda/loaders/productDetailsPage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ async function loader(
4040
const result = await api["GET /api/v2/products/:id"]({
4141
id,
4242
include_images: "true",
43+
include_inventory_place: "true"
4344
}, STALE);
4445
return result.json();
4546
} catch (error) {

vnda/utils/client/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface API {
1515
/** @docs https://developers.vnda.com.br/reference/get-api-v2-products-id */
1616
"GET /api/v2/products/:id": {
1717
response: ProductGroup;
18-
searchParams: { include_images: boolean };
18+
searchParams: { include_images: boolean, include_inventory_place: boolean };
1919
};
2020

2121
/** @docs https://developers.vnda.com.br/reference/get-api-v2-products-product_id-price */

vnda/utils/transform.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
ProductInstallment,
1717
ProductSearch,
1818
ProductVariant,
19+
ProductVariantInventory,
1920
VariantProductSearch,
2021
} from "./openapi/vnda.openapi.gen.ts";
2122
import { getSegmentFromCookie, parse } from "./segment.ts";
@@ -246,6 +247,19 @@ const toPropertyValueCategoryTags = (
246247
});
247248
};
248249

250+
const toPropertyValueInventory = (inventories?: ProductVariantInventory[]) => {
251+
if (!inventories) return []
252+
return inventories.map((inventory) => {
253+
return {
254+
"@type": "PropertyValue",
255+
name: inventory.name ?? inventory.place_name,
256+
value: inventory.quantity.toString(),
257+
description: inventory.slug,
258+
valueReference: "INVENTORY"
259+
} as PropertyValue
260+
})
261+
}
262+
249263
// deno-lint-ignore no-explicit-any
250264
const isProductVariant = (p: any): p is VariantProductSearch =>
251265
typeof p.id === "number";
@@ -298,6 +312,7 @@ export const toProduct = (
298312

299313
const productID = `${variant.sku}`;
300314
const productGroupID = `${product.id}`;
315+
const inventories = variant.inventories as ProductVariantInventory[]
301316

302317
const myTags = "tags" in product ? product.tags : [];
303318
const myCategoryTags = "category_tags" in product
@@ -315,6 +330,7 @@ export const toProduct = (
315330
...toPropertyValue(variant),
316331
...toPropertyValueTags(myTags),
317332
...toPropertyValueCategoryTags(myCategoryTags),
333+
...toPropertyValueInventory(inventories)
318334
],
319335
inProductGroupWithID: productGroupID,
320336
gtin: product.reference,

0 commit comments

Comments
 (0)