fix(medusa,utils,types): inventory management nullable (#13703)

* fix(medusa,utils,types): inventory management nullable

* fix unit

* fix changeset
This commit is contained in:
William Bouchard authored and GitHub committed 2025-10-07 14:44:30 -04:00
1 parent 86c975258b
commit c61f3150c1
5 files changed
+26 -13

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
---
"@medusajs/types": patch
"@medusajs/utils": patch
"@medusajs/medusa": patch
---
fix(medusa,utils,types): inventory management nullable
@@ -173,10 +173,10 @@ export interface BaseProductVariant {
* This field is only retrieved in the [Get Product](https://docs.medusajs.com/api/store#products_getproductsid) * This field is only retrieved in the [Get Product](https://docs.medusajs.com/api/store#products_getproductsid)
* and [List Products](https://docs.medusajs.com/api/store#products_getproducts) API routes if you * and [List Products](https://docs.medusajs.com/api/store#products_getproducts) API routes if you
* pass `+variants.inventory_quantity` in the `fields` query parameter. * pass `+variants.inventory_quantity` in the `fields` query parameter.
* *
* Learn more in the [Retrieve Product Variant's Inventory](https://docs.medusajs.com/resources/storefront-development/products/inventory) storefront guide. * Learn more in the [Retrieve Product Variant's Inventory](https://docs.medusajs.com/resources/storefront-development/products/inventory) storefront guide.
*/ */
inventory_quantity?: number inventory_quantity?: number | null
/** /**
* The variant's HS code. * The variant's HS code.
*/ */
@@ -9,7 +9,7 @@ export type VariantAvailabilityResult = {
/** /**
* The available inventory quantity for the variant in the sales channel. * The available inventory quantity for the variant in the sales channel.
*/ */
availability: number availability: number | null
/** /**
* The ID of the sales channel for which the availability was computed. * The ID of the sales channel for which the availability was computed.
*/ */
@@ -18,7 +18,7 @@ export type VariantAvailabilityResult = {
} }
/** /**
* Computes the varaint availability for a list of variants in a given sales channel * Computes the variant availability for a list of variants in a given sales channel
* *
* The availability algorithm works as follows: * The availability algorithm works as follows:
* 1. For each variant, we retrieve its inventory items. * 1. For each variant, we retrieve its inventory items.
@@ -76,7 +76,7 @@ export async function getTotalVariantAvailability(
data: TotalVariantAvailabilityData data: TotalVariantAvailabilityData
): Promise<{ ): Promise<{
[variant_id: string]: { [variant_id: string]: {
availability: number availability: number | null
} }
}> { }> {
const { variantInventoriesMap, locationIds } = await getDataForComputation( const { variantInventoriesMap, locationIds } = await getDataForComputation(
@@ -116,7 +116,7 @@ const computeVariantAvailability = (
variantInventoryItems: VariantItems[], variantInventoryItems: VariantItems[],
channelLocationsSet: Set<string>, channelLocationsSet: Set<string>,
{ requireChannelCheck } = { requireChannelCheck: true } { requireChannelCheck } = { requireChannelCheck: true }
) => { ): number | null => {
const inventoryQuantities: number[] = [] const inventoryQuantities: number[] = []
for (const link of variantInventoryItems) { for (const link of variantInventoryItems) {
@@ -143,7 +143,7 @@ const computeVariantAvailability = (
inventoryQuantities.push(maxInventoryQuantity) inventoryQuantities.push(maxInventoryQuantity)
} }
return inventoryQuantities.length ? Math.min(...inventoryQuantities) : 0 return inventoryQuantities.length ? Math.min(...inventoryQuantities) : null
} }
type VariantAvailabilityData = { type VariantAvailabilityData = {
@@ -7,8 +7,8 @@ import {
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
import { MedusaRequest, MedusaStoreRequest } from "@medusajs/framework/http" import { MedusaRequest, MedusaStoreRequest } from "@medusajs/framework/http"
import { import {
wrapVariantsWithTotalInventoryQuantity,
wrapVariantsWithInventoryQuantityForSalesChannel, wrapVariantsWithInventoryQuantityForSalesChannel,
wrapVariantsWithTotalInventoryQuantity,
} from "../variant-inventory-quantity" } from "../variant-inventory-quantity"
jest.mock("@medusajs/framework/utils", () => { jest.mock("@medusajs/framework/utils", () => {
@@ -79,7 +79,12 @@ describe("variant-inventory-quantity", () => {
"variant-1": { availability: 10 }, "variant-1": { availability: 10 },
"variant-2": { availability: 5 }, "variant-2": { availability: 5 },
"variant-3": { availability: 20 }, "variant-3": { availability: 20 },
"variant-4": { availability: null },
} }
const _variants = [
...variants,
{ id: "variant-4", manage_inventory: true },
]
;(getTotalVariantAvailability as jest.Mock).mockResolvedValueOnce( ;(getTotalVariantAvailability as jest.Mock).mockResolvedValueOnce(
mockAvailability mockAvailability
@@ -87,12 +92,13 @@ describe("variant-inventory-quantity", () => {
await wrapVariantsWithTotalInventoryQuantity( await wrapVariantsWithTotalInventoryQuantity(
req as MedusaRequest, req as MedusaRequest,
variants _variants
) )
expect(variants[0].inventory_quantity).toBe(10) expect(_variants[0].inventory_quantity).toBe(10)
expect(variants[1].inventory_quantity).toBe(5) expect(_variants[1].inventory_quantity).toBe(5)
expect(variants[2].inventory_quantity).toBeUndefined() expect(_variants[2].inventory_quantity).toBeUndefined()
expect(_variants[3].inventory_quantity).toBeNull()
}) })
}) })
@@ -65,7 +65,7 @@ export const wrapVariantsWithInventoryQuantityForSalesChannel = async (
type VariantInput = { type VariantInput = {
id: string id: string
inventory_quantity?: number inventory_quantity?: number | null
manage_inventory?: boolean manage_inventory?: boolean
} }