fix(medusa,utils,types): inventory management nullable (#13703)
* fix(medusa,utils,types): inventory management nullable * fix unit * fix changeset
This commit is contained in:
7
.changeset/chilly-timers-jog.md
Normal file
7
.changeset/chilly-timers-jog.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"@medusajs/types": patch
|
||||
"@medusajs/utils": patch
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa,utils,types): inventory management nullable
|
||||
@@ -176,7 +176,7 @@ export interface BaseProductVariant {
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ export type VariantAvailabilityResult = {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@@ -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:
|
||||
* 1. For each variant, we retrieve its inventory items.
|
||||
@@ -76,7 +76,7 @@ export async function getTotalVariantAvailability(
|
||||
data: TotalVariantAvailabilityData
|
||||
): Promise<{
|
||||
[variant_id: string]: {
|
||||
availability: number
|
||||
availability: number | null
|
||||
}
|
||||
}> {
|
||||
const { variantInventoriesMap, locationIds } = await getDataForComputation(
|
||||
@@ -116,7 +116,7 @@ const computeVariantAvailability = (
|
||||
variantInventoryItems: VariantItems[],
|
||||
channelLocationsSet: Set<string>,
|
||||
{ requireChannelCheck } = { requireChannelCheck: true }
|
||||
) => {
|
||||
): number | null => {
|
||||
const inventoryQuantities: number[] = []
|
||||
|
||||
for (const link of variantInventoryItems) {
|
||||
@@ -143,7 +143,7 @@ const computeVariantAvailability = (
|
||||
inventoryQuantities.push(maxInventoryQuantity)
|
||||
}
|
||||
|
||||
return inventoryQuantities.length ? Math.min(...inventoryQuantities) : 0
|
||||
return inventoryQuantities.length ? Math.min(...inventoryQuantities) : null
|
||||
}
|
||||
|
||||
type VariantAvailabilityData = {
|
||||
|
||||
@@ -7,8 +7,8 @@ import {
|
||||
} from "@medusajs/framework/utils"
|
||||
import { MedusaRequest, MedusaStoreRequest } from "@medusajs/framework/http"
|
||||
import {
|
||||
wrapVariantsWithTotalInventoryQuantity,
|
||||
wrapVariantsWithInventoryQuantityForSalesChannel,
|
||||
wrapVariantsWithTotalInventoryQuantity,
|
||||
} from "../variant-inventory-quantity"
|
||||
|
||||
jest.mock("@medusajs/framework/utils", () => {
|
||||
@@ -79,7 +79,12 @@ describe("variant-inventory-quantity", () => {
|
||||
"variant-1": { availability: 10 },
|
||||
"variant-2": { availability: 5 },
|
||||
"variant-3": { availability: 20 },
|
||||
"variant-4": { availability: null },
|
||||
}
|
||||
const _variants = [
|
||||
...variants,
|
||||
{ id: "variant-4", manage_inventory: true },
|
||||
]
|
||||
|
||||
;(getTotalVariantAvailability as jest.Mock).mockResolvedValueOnce(
|
||||
mockAvailability
|
||||
@@ -87,12 +92,13 @@ describe("variant-inventory-quantity", () => {
|
||||
|
||||
await wrapVariantsWithTotalInventoryQuantity(
|
||||
req as MedusaRequest,
|
||||
variants
|
||||
_variants
|
||||
)
|
||||
|
||||
expect(variants[0].inventory_quantity).toBe(10)
|
||||
expect(variants[1].inventory_quantity).toBe(5)
|
||||
expect(variants[2].inventory_quantity).toBeUndefined()
|
||||
expect(_variants[0].inventory_quantity).toBe(10)
|
||||
expect(_variants[1].inventory_quantity).toBe(5)
|
||||
expect(_variants[2].inventory_quantity).toBeUndefined()
|
||||
expect(_variants[3].inventory_quantity).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ export const wrapVariantsWithInventoryQuantityForSalesChannel = async (
|
||||
|
||||
type VariantInput = {
|
||||
id: string
|
||||
inventory_quantity?: number
|
||||
inventory_quantity?: number | null
|
||||
manage_inventory?: boolean
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user