feat(core-flows,medusa): adds inventory kit creation to variants endpoint (#7599)

what:

When creating a variant, we can now create inventory as a part of the product and variants create endpoint.

This applies only to variants where `manage_inventory=true`. 2 cases present itself:

1. When inventory_items are present
  - Link an inventory item with required_quantity to the variant
  - the inventory item already needs to be present
2. When inventory_items are not present
  - A default inventory item will be created
  - links the created item to the variant with a default required_quantity
  
  
RESOLVES CORE-2220
This commit is contained in:
Riqwan Thamir authored and GitHub committed 2024-06-04 13:49:31 +00:00
1 parent 6646a203df
commit e7005a0aac
9 files changed
+521 -179

No files matched your search

@@ -1744,6 +1744,165 @@ medusaIntegrationTestRunner({
}) })
) )
}) })
it("creates product with variant inventory kits", async () => {
const inventoryItem1 = (
await api.post(
`/admin/inventory-items`,
{ sku: "inventory-1" },
adminHeaders
)
).data.inventory_item
const inventoryItem2 = (
await api.post(
`/admin/inventory-items`,
{ sku: "inventory-2" },
adminHeaders
)
).data.inventory_item
const inventoryItem3 = (
await api.post(
`/admin/inventory-items`,
{ sku: "inventory-3" },
adminHeaders
)
).data.inventory_item
const payload = {
title: "Test product - 1",
handle: "test-1",
variants: [
{
title: "Custom inventory 1",
prices: [{ currency_code: "usd", amount: 100 }],
manage_inventory: true,
inventory_items: [
{
inventory_item_id: inventoryItem1.id,
required_quantity: 4,
},
],
},
{
title: "No inventory",
prices: [{ currency_code: "usd", amount: 100 }],
manage_inventory: false,
},
{
title: "Default Inventory",
prices: [{ currency_code: "usd", amount: 100 }],
manage_inventory: true,
},
{
title: "Custom inventory 2",
prices: [{ currency_code: "usd", amount: 100 }],
manage_inventory: true,
inventory_items: [
{
inventory_item_id: inventoryItem2.id,
required_quantity: 5,
},
{
inventory_item_id: inventoryItem3.id,
required_quantity: 6,
},
],
},
],
}
const response = await api
.post(
"/admin/products?fields=%2bvariants.inventory_items.inventory.*,%2bvariants.inventory_items.*",
payload,
adminHeaders
)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
title: "Test product - 1",
variants: expect.arrayContaining([
expect.objectContaining({
title: "Custom inventory 1",
manage_inventory: true,
inventory_items: [
expect.objectContaining({
required_quantity: 4,
inventory_item_id: inventoryItem1.id,
}),
],
}),
expect.objectContaining({
title: "No inventory",
manage_inventory: false,
inventory_items: [],
}),
expect.objectContaining({
title: "Default Inventory",
manage_inventory: true,
inventory_items: [
expect.objectContaining({
required_quantity: 1,
inventory_item_id: expect.any(String),
}),
],
}),
expect.objectContaining({
title: "Custom inventory 2",
manage_inventory: true,
inventory_items: expect.arrayContaining([
expect.objectContaining({
required_quantity: 5,
inventory_item_id: inventoryItem2.id,
}),
expect.objectContaining({
required_quantity: 6,
inventory_item_id: inventoryItem3.id,
}),
]),
}),
]),
})
)
})
it("should throw an error when inventory item does not exist", async () => {
const payload = {
title: "Test product - 1",
handle: "test-1",
variants: [
{
title: "Custom inventory 1",
prices: [{ currency_code: "usd", amount: 100 }],
manage_inventory: true,
inventory_items: [
{
inventory_item_id: "does-not-exist",
required_quantity: 4,
},
],
},
],
}
const error = await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => err)
expect(error.response.status).toEqual(400)
expect(error.response.data).toEqual(
expect.objectContaining({
type: "invalid_data",
message: "Inventory Items with ids: does-not-exist was not found",
})
)
})
}) })
describe("DELETE /admin/products/:id/options/:option_id", () => { describe("DELETE /admin/products/:id/options/:option_id", () => {
@@ -2908,11 +3067,14 @@ medusaIntegrationTestRunner({
) )
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
expect(res.data.variant.inventory_items[0]).toEqual( expect(res.data.variant.inventory_items).toHaveLength(2)
expect.objectContaining({ expect(res.data.variant.inventory_items).toEqual(
required_quantity: 5, expect.arrayContaining([
inventory_item_id: inventoryItem.id, expect.objectContaining({
}) required_quantity: 5,
inventory_item_id: inventoryItem.id,
}),
])
) )
}) })
}) })
@@ -2963,13 +3125,15 @@ medusaIntegrationTestRunner({
) )
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
expect(res.data.variant.inventory_items) expect(res.data.variant.inventory_items).toHaveLength(2)
expect(res.data.variant.inventory_items).toEqual([ expect(res.data.variant.inventory_items).toEqual(
expect.objectContaining({ expect.arrayContaining([
required_quantity: 10, expect.objectContaining({
inventory_item_id: inventoryItem.id, required_quantity: 10,
}), inventory_item_id: inventoryItem.id,
]) }),
])
)
}) })
}) })
@@ -3008,8 +3172,10 @@ medusaIntegrationTestRunner({
) )
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
expect(res.data.parent.inventory_items) expect(res.data.parent.inventory_items).toHaveLength(1)
expect(res.data.parent.inventory_items).toEqual([]) expect(res.data.parent.inventory_items[0].id).not.toBe(
inventoryItem.id
)
}) })
}) })
@@ -3131,11 +3297,14 @@ medusaIntegrationTestRunner({
) )
).data.variant ).data.variant
expect(createdLinkVariant.inventory_items[0]).toEqual( expect(createdLinkVariant.inventory_items).toHaveLength(2)
expect.objectContaining({ expect(createdLinkVariant.inventory_items).toEqual(
required_quantity: 15, expect.arrayContaining([
inventory_item_id: inventoryItemToCreate.id, expect.objectContaining({
}) required_quantity: 15,
inventory_item_id: inventoryItemToCreate.id,
}),
])
) )
const updatedLinkVariant = ( const updatedLinkVariant = (
@@ -3145,11 +3314,14 @@ medusaIntegrationTestRunner({
) )
).data.variant ).data.variant
expect(updatedLinkVariant.inventory_items[0]).toEqual( expect(updatedLinkVariant.inventory_items).toHaveLength(2)
expect.objectContaining({ expect(updatedLinkVariant.inventory_items).toEqual(
required_quantity: 25, expect.arrayContaining([
inventory_item_id: inventoryItemToUpdate.id, expect.objectContaining({
}) required_quantity: 25,
inventory_item_id: inventoryItemToUpdate.id,
}),
])
) )
const deletedLinkVariant = ( const deletedLinkVariant = (
@@ -3159,7 +3331,10 @@ medusaIntegrationTestRunner({
) )
).data.variant ).data.variant
expect(deletedLinkVariant.inventory_items).toHaveLength(0) expect(deletedLinkVariant.inventory_items).toHaveLength(1)
expect(deletedLinkVariant.inventory_items[0].id).not.toEqual(
inventoryItemToDelete.id
)
}) })
}) })
}) })
@@ -1823,36 +1823,6 @@ medusaIntegrationTestRunner({
) )
).data.sales_channel ).data.sales_channel
product = (
await api.post(
"/admin/products",
{
title: "Test fixture",
tags: [{ value: "123" }, { value: "456" }],
options: [
{ title: "size", values: ["large", "small"] },
{ title: "color", values: ["green"] },
],
variants: [
{
title: "Test variant",
prices: [
{
currency_code: "usd",
amount: 100,
},
],
options: {
size: "large",
color: "green",
},
},
],
},
adminHeaders
)
).data.product
stockLocation = ( stockLocation = (
await api.post( await api.post(
`/admin/stock-locations`, `/admin/stock-locations`,
@@ -1902,20 +1872,41 @@ medusaIntegrationTestRunner({
], ],
}) })
await remoteLinkService.create([ product = (
{ await api.post(
[Modules.STOCK_LOCATION]: { stock_location_id: stockLocation.id }, "/admin/products",
[Modules.FULFILLMENT]: { fulfillment_set_id: fulfillmentSet.id }, {
}, title: "Test fixture",
{ tags: [{ value: "123" }, { value: "456" }],
[Modules.PRODUCT]: { options: [
variant_id: product.variants[0].id, { title: "size", values: ["large", "small"] },
{ title: "color", values: ["green"] },
],
variants: [
{
title: "Test variant",
inventory_items: [
{
inventory_item_id: inventoryItem.id,
required_quantity: 1,
},
],
prices: [
{
currency_code: "usd",
amount: 100,
},
],
options: {
size: "large",
color: "green",
},
},
],
}, },
[Modules.INVENTORY]: { adminHeaders
inventory_item_id: inventoryItem.id, )
}, ).data.product
},
])
shippingOption = ( shippingOption = (
await api.post( await api.post(
@@ -96,7 +96,8 @@ medusaIntegrationTestRunner({
) )
).data.variant ).data.variant
expect(updatedVariant.inventory_items).toHaveLength(0) // The default inventory item remains that was created as a part of create product
expect(updatedVariant.inventory_items).toHaveLength(1)
}) })
}) })
}) })
@@ -159,11 +160,14 @@ medusaIntegrationTestRunner({
) )
).data.variant ).data.variant
expect(updatedVariant.inventory_items).toEqual([ expect(updatedVariant.inventory_items).toHaveLength(2)
expect.objectContaining({ expect(updatedVariant.inventory_items).toEqual(
required_quantity: originalQuantity, expect.arrayContaining([
}), expect.objectContaining({
]) required_quantity: originalQuantity,
}),
])
)
}) })
it("should throw an error when a link is not found", async () => { it("should throw an error when a link is not found", async () => {
@@ -258,11 +262,14 @@ medusaIntegrationTestRunner({
) )
).data.variant ).data.variant
expect(updatedVariant.inventory_items).toEqual([ expect(updatedVariant.inventory_items).toHaveLength(2)
expect.objectContaining({ expect(updatedVariant.inventory_items).toEqual(
required_quantity: originalQuantity, expect.arrayContaining([
}), expect.objectContaining({
]) required_quantity: originalQuantity,
}),
])
)
}) })
it("should pass dismiss step if link not found if next step throws error", async () => { it("should pass dismiss step if link not found if next step throws error", async () => {
@@ -304,7 +311,7 @@ medusaIntegrationTestRunner({
) )
).data.variant ).data.variant
expect(updatedVariant.inventory_items).toEqual([]) expect(updatedVariant.inventory_items).toHaveLength(1)
}) })
}) })
}) })
@@ -27,6 +27,8 @@ medusaIntegrationTestRunner({
let variant2 let variant2
let variant3 let variant3
let variant4 let variant4
let inventoryItem1
let inventoryItem2
const createProducts = async (data) => { const createProducts = async (data) => {
const response = await api.post( const response = await api.post(
@@ -88,6 +90,21 @@ medusaIntegrationTestRunner({
describe("GET /store/products", () => { describe("GET /store/products", () => {
beforeEach(async () => { beforeEach(async () => {
inventoryItem1 = (
await api.post(
`/admin/inventory-items`,
{ sku: "test-sku" },
adminHeaders
)
).data.inventory_item
inventoryItem2 = (
await api.post(
`/admin/inventory-items`,
{ sku: "test-sku-2" },
adminHeaders
)
).data.inventory_item
;[product, [variant]] = await createProducts({ ;[product, [variant]] = await createProducts({
title: "test product 1", title: "test product 1",
status: ProductStatus.PUBLISHED, status: ProductStatus.PUBLISHED,
@@ -95,6 +112,16 @@ medusaIntegrationTestRunner({
{ {
title: "test variant 1", title: "test variant 1",
manage_inventory: true, manage_inventory: true,
inventory_items: [
{
inventory_item_id: inventoryItem1.id,
required_quantity: 20,
},
{
inventory_item_id: inventoryItem2.id,
required_quantity: 20,
},
],
prices: [{ amount: 3000, currency_code: "usd" }], prices: [{ amount: 3000, currency_code: "usd" }],
}, },
], ],
@@ -368,8 +395,6 @@ medusaIntegrationTestRunner({
describe("with inventory items", () => { describe("with inventory items", () => {
let location1 let location1
let location2 let location2
let inventoryItem1
let inventoryItem2
let salesChannel1 let salesChannel1
let publishableKey1 let publishableKey1
@@ -409,43 +434,23 @@ medusaIntegrationTestRunner({
adminHeaders adminHeaders
) )
inventoryItem1 = ( await api.post(
await api.post( `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
`/admin/inventory-items`, {
{ sku: "test-sku" }, location_id: location1.id,
adminHeaders stocked_quantity: 20,
) },
).data.inventory_item adminHeaders
)
inventoryItem2 = ( await api.post(
await api.post( `/admin/inventory-items/${inventoryItem2.id}/location-levels`,
`/admin/inventory-items`, {
{ sku: "test-sku-2" }, location_id: location2.id,
adminHeaders stocked_quantity: 30,
) },
).data.inventory_item adminHeaders
)
inventoryItem1 = (
await api.post(
`/admin/inventory-items/${inventoryItem1.id}/location-levels`,
{
location_id: location1.id,
stocked_quantity: 20,
},
adminHeaders
)
).data.inventory_item
inventoryItem2 = (
await api.post(
`/admin/inventory-items/${inventoryItem2.id}/location-levels`,
{
location_id: location2.id,
stocked_quantity: 30,
},
adminHeaders
)
).data.inventory_item
const remoteLink = appContainer.resolve( const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK ContainerRegistrationKeys.REMOTE_LINK
@@ -465,24 +470,6 @@ medusaIntegrationTestRunner({
}) })
it("should list all inventory items for a variant", async () => { it("should list all inventory items for a variant", async () => {
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
// TODO: Missing API endpoint. Remove this when its available
await remoteLink.create([
{
[Modules.PRODUCT]: { variant_id: variant.id },
[Modules.INVENTORY]: { inventory_item_id: inventoryItem1.id },
data: { required_quantity: 20 },
},
{
[Modules.PRODUCT]: { variant_id: variant.id },
[Modules.INVENTORY]: { inventory_item_id: inventoryItem2.id },
data: { required_quantity: 20 },
},
])
let response = await api.get( let response = await api.get(
`/store/products?sales_channel_id[]=${salesChannel1.id}&fields=variants.inventory_items.inventory.location_levels.*`, `/store/products?sales_channel_id[]=${salesChannel1.id}&fields=variants.inventory_items.inventory.location_levels.*`,
{ headers: { "x-publishable-api-key": publishableKey1.token } } { headers: { "x-publishable-api-key": publishableKey1.token } }
@@ -512,29 +499,21 @@ medusaIntegrationTestRunner({
}) })
it("should return inventory quantity when variant's manage_inventory is true", async () => { it("should return inventory quantity when variant's manage_inventory is true", async () => {
const remoteLink = appContainer.resolve( await api.post(
ContainerRegistrationKeys.REMOTE_LINK `/admin/products/${product.id}/variants/${variant.id}/inventory-items`,
{ required_quantity: 20, inventory_item_id: inventoryItem1.id },
adminHeaders
) )
// TODO: Missing API endpoint. Remove this when its available await api.post(
await remoteLink.create([ `/admin/products/${product.id}/variants/${variant.id}/inventory-items`,
{ { required_quantity: 20, inventory_item_id: inventoryItem2.id },
[Modules.PRODUCT]: { variant_id: variant.id }, adminHeaders
[Modules.INVENTORY]: { inventory_item_id: inventoryItem1.id }, )
data: { required_quantity: 20 },
},
{
[Modules.PRODUCT]: { variant_id: variant.id },
[Modules.INVENTORY]: { inventory_item_id: inventoryItem2.id },
data: { required_quantity: 20 },
},
])
let response = await api.get( let response = await api.get(
`/store/products?sales_channel_id[]=${salesChannel1.id}&fields=%2bvariants.inventory_quantity`, `/store/products?sales_channel_id[]=${salesChannel1.id}&fields=%2bvariants.inventory_quantity`,
{ { headers: { "x-publishable-api-key": publishableKey1.token } }
headers: { "x-publishable-api-key": publishableKey1.token },
}
) )
const product1Res = response.data.products.find( const product1Res = response.data.products.find(
@@ -0,0 +1,36 @@
import {
ContainerRegistrationKeys,
MedusaError,
arrayDifference,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { createStep } from "@medusajs/workflows-sdk"
export const validateInventoryItemsId = "validate-inventory-items-step"
export const validateInventoryItems = createStep(
validateInventoryItemsId,
async (id: string[], { container }) => {
const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
const query = remoteQueryObjectFromString({
entryPoint: "inventory_item",
variables: { id },
fields: ["id"],
})
const items = await remoteQuery(query)
const diff = arrayDifference(
id,
items.map(({ id }) => id)
)
if (diff.length > 0) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Inventory Items with ids: ${diff.join(", ")} was not found`
)
}
}
)
@@ -1,9 +1,13 @@
import { PricingTypes, ProductTypes } from "@medusajs/types" import { LinkDefinition, Modules } from "@medusajs/modules-sdk"
import { InventoryNext, PricingTypes, ProductTypes } from "@medusajs/types"
import { import {
WorkflowData, WorkflowData,
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { createLinksWorkflow } from "../../common/workflows/create-links"
import { validateInventoryItems } from "../../inventory/steps/validate-inventory-items"
import { createInventoryItemsWorkflow } from "../../inventory/workflows/create-inventory-items"
import { createPriceSetsStep } from "../../pricing" import { createPriceSetsStep } from "../../pricing"
import { createProductVariantsStep } from "../steps/create-product-variants" import { createProductVariantsStep } from "../steps/create-product-variants"
import { createVariantPricingLinkStep } from "../steps/create-variant-pricing-link" import { createVariantPricingLinkStep } from "../steps/create-variant-pricing-link"
@@ -12,9 +16,105 @@ import { createVariantPricingLinkStep } from "../steps/create-variant-pricing-li
type WorkflowInput = { type WorkflowInput = {
product_variants: (ProductTypes.CreateProductVariantDTO & { product_variants: (ProductTypes.CreateProductVariantDTO & {
prices?: PricingTypes.CreateMoneyAmountDTO[] prices?: PricingTypes.CreateMoneyAmountDTO[]
} & {
inventory_items?: {
inventory_item_id: string
required_quantity?: number
}[]
})[] })[]
} }
const buildLink = (
variant_id: string,
inventory_item_id: string,
required_quantity: number
) => {
const link: LinkDefinition = {
[Modules.PRODUCT]: { variant_id },
[Modules.INVENTORY]: { inventory_item_id: inventory_item_id },
data: { required_quantity: required_quantity },
}
return link
}
const buildLinksToCreate = (data: {
createdVariants: ProductTypes.ProductVariantDTO[]
inventoryIndexMap: Record<number, InventoryNext.InventoryItemDTO>
input: WorkflowInput
}) => {
let index = 0
const linksToCreate: LinkDefinition[] = []
for (const variant of data.createdVariants) {
const variantInput = data.input.product_variants[index]
const shouldManageInventory = variant.manage_inventory
const hasInventoryItems = variantInput.inventory_items?.length
index += 1
if (!shouldManageInventory) {
continue
}
if (!hasInventoryItems) {
const inventoryItem = data.inventoryIndexMap[index]
linksToCreate.push(buildLink(variant.id, inventoryItem.id, 1))
continue
}
for (const inventoryInput of variantInput.inventory_items || []) {
linksToCreate.push(
buildLink(
variant.id,
inventoryInput.inventory_item_id,
inventoryInput.required_quantity ?? 1
)
)
}
}
return linksToCreate
}
const buildVariantItemCreateMap = (data: {
createdVariants: ProductTypes.ProductVariantDTO[]
input: WorkflowInput
}) => {
let index = 0
const map: Record<number, InventoryNext.CreateInventoryItemInput> = {}
for (const variant of data.createdVariants || []) {
const variantInput = data.input.product_variants[index]
const shouldManageInventory = variant.manage_inventory
const hasInventoryItems = variantInput.inventory_items?.length
index += 1
if (!shouldManageInventory || hasInventoryItems) {
continue
}
// Create a default inventory item if the above conditions arent met
map[index] = {
sku: variantInput.sku,
origin_country: variantInput.origin_country,
mid_code: variantInput.mid_code,
material: variantInput.material,
weight: variantInput.weight,
length: variantInput.length,
height: variantInput.height,
width: variantInput.width,
title: variantInput.title,
description: variantInput.title,
hs_code: variantInput.hs_code,
requires_shipping: true,
}
}
return map
}
export const createProductVariantsWorkflowId = "create-product-variants" export const createProductVariantsWorkflowId = "create-product-variants"
export const createProductVariantsWorkflow = createWorkflow( export const createProductVariantsWorkflow = createWorkflow(
createProductVariantsWorkflowId, createProductVariantsWorkflowId,
@@ -31,6 +131,50 @@ export const createProductVariantsWorkflow = createWorkflow(
const createdVariants = createProductVariantsStep(variantsWithoutPrices) const createdVariants = createProductVariantsStep(variantsWithoutPrices)
// Setup variants inventory
const inventoryItemIds = transform(input, (data) => {
return data.product_variants
.map((variant) => variant.inventory_items || [])
.flat()
.map((item) => item.inventory_item_id)
.flat()
})
validateInventoryItems(inventoryItemIds)
const variantItemCreateMap = transform(
{ createdVariants, input },
buildVariantItemCreateMap
)
const createdInventoryItems = createInventoryItemsWorkflow.runAsStep({
input: {
items: transform(variantItemCreateMap, (data) => Object.values(data)),
},
})
const inventoryIndexMap = transform(
{ createdInventoryItems, variantItemCreateMap },
(data) => {
const map: Record<number, InventoryNext.InventoryItemDTO> = {}
let inventoryIndex = 0
for (const variantIndex of Object.keys(data.variantItemCreateMap)) {
map[variantIndex] = data.createdInventoryItems[inventoryIndex]
inventoryIndex += 1
}
return map
}
)
const linksToCreate = transform(
{ createdVariants, inventoryIndexMap, input },
buildLinksToCreate
)
createLinksWorkflow.runAsStep({ input: linksToCreate })
// Note: We rely on the same order of input and output when creating variants here, make sure that assumption holds // Note: We rely on the same order of input and output when creating variants here, make sure that assumption holds
const pricesToCreate = transform({ input, createdVariants }, (data) => const pricesToCreate = transform({ input, createdVariants }, (data) =>
data.createdVariants.map((v, i) => { data.createdVariants.map((v, i) => {
@@ -40,7 +184,6 @@ export const createProductVariantsWorkflow = createWorkflow(
}) })
) )
// TODO: From here until the final transform the code is the same as when creating a product, we can probably refactor
const createdPriceSets = createPriceSetsStep(pricesToCreate) const createdPriceSets = createPriceSetsStep(pricesToCreate)
const variantAndPriceSets = transform( const variantAndPriceSets = transform(
@@ -59,5 +59,6 @@ export const POST = async (
req.scope, req.scope,
req.remoteQueryConfig.fields req.remoteQueryConfig.fields
) )
res.status(200).json({ product: remapProductResponse(product) }) res.status(200).json({ product: remapProductResponse(product) })
} }
@@ -128,27 +128,37 @@ export const AdminCreateProductType = z.object({
export type AdminCreateProductVariantType = z.infer< export type AdminCreateProductVariantType = z.infer<
typeof AdminCreateProductVariant typeof AdminCreateProductVariant
> >
export const AdminCreateProductVariant = z.object({ export const AdminCreateProductVariant = z
title: z.string(), .object({
sku: z.string().nullable().optional(), title: z.string(),
ean: z.string().nullable().optional(), sku: z.string().nullable().optional(),
upc: z.string().nullable().optional(), ean: z.string().nullable().optional(),
barcode: z.string().nullable().optional(), upc: z.string().nullable().optional(),
hs_code: z.string().nullable().optional(), barcode: z.string().nullable().optional(),
mid_code: z.string().nullable().optional(), hs_code: z.string().nullable().optional(),
allow_backorder: z.boolean().optional().default(false), mid_code: z.string().nullable().optional(),
manage_inventory: z.boolean().optional().default(true), allow_backorder: z.boolean().optional().default(false),
variant_rank: z.number().optional(), manage_inventory: z.boolean().optional().default(true),
weight: z.number().nullable().optional(), variant_rank: z.number().optional(),
length: z.number().nullable().optional(), weight: z.number().nullable().optional(),
height: z.number().nullable().optional(), length: z.number().nullable().optional(),
width: z.number().nullable().optional(), height: z.number().nullable().optional(),
origin_country: z.string().nullable().optional(), width: z.number().nullable().optional(),
material: z.string().nullable().optional(), origin_country: z.string().nullable().optional(),
metadata: z.record(z.unknown()).optional(), material: z.string().nullable().optional(),
prices: z.array(AdminCreateVariantPrice), metadata: z.record(z.unknown()).optional(),
options: z.record(z.string()).optional(), prices: z.array(AdminCreateVariantPrice),
}) options: z.record(z.string()).optional(),
inventory_items: z
.array(
z.object({
inventory_item_id: z.string(),
required_quantity: z.number(),
})
)
.optional(),
})
.strict()
export type AdminUpdateProductVariantType = z.infer< export type AdminUpdateProductVariantType = z.infer<
typeof AdminUpdateProductVariant typeof AdminUpdateProductVariant
@@ -87,8 +87,8 @@ export const wrapVariantsWithInventoryQuantity = async (
for (const link of links) { for (const link of links) {
const requiredQuantity = link.required_quantity const requiredQuantity = link.required_quantity
const availableQuantity = (link.inventory.location_levels || []).reduce( const availableQuantity = (link.inventory?.location_levels || []).reduce(
(sum, level) => sum + level.available_quantity || 0, (sum, level) => sum + (level?.available_quantity || 0),
0 0
) )