fix(medusa,utils,types): inventory management nullable (#13703)
* fix(medusa,utils,types): inventory management nullable * fix unit * fix changeset
This commit is contained in:
1 parent
86c975258b
commit
c61f3150c1
5 files changed
+25
-12
No files matched your search
@@ -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.
|
* 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 = {
|
||||||
|
|||||||
+11
-5
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user