fix(medusa): Only add ordering select if not already present (#3319)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/inventory": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
List inventory items based on locations
|
||||||
@@ -278,90 +278,145 @@ describe("Inventory Items endpoints", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("List inventory items", async () => {
|
describe("List inventory items", () => {
|
||||||
const api = useApi()
|
it("Lists inventory items with location", async () => {
|
||||||
const inventoryItemId = inventoryItems[0].id
|
const api = useApi()
|
||||||
|
|
||||||
await api.post(
|
await api.post(
|
||||||
`/admin/inventory-items/${inventoryItemId}/location-levels`,
|
`/admin/products/test-product/variants`,
|
||||||
{
|
{
|
||||||
location_id: location2Id,
|
title: "Test Variant w. inventory 2",
|
||||||
stocked_quantity: 10,
|
sku: "MY_SKU1",
|
||||||
},
|
material: "material",
|
||||||
adminHeaders
|
origin_country: "UK",
|
||||||
)
|
manage_inventory: true,
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
option_id: "test-product-option",
|
||||||
|
value: "M",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
prices: [{ currency_code: "usd", amount: 200 }],
|
||||||
|
},
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
await api.post(
|
const inventoryItemId = inventoryItems[0].id
|
||||||
`/admin/inventory-items/${inventoryItemId}/location-levels`,
|
|
||||||
{
|
|
||||||
location_id: location3Id,
|
|
||||||
stocked_quantity: 5,
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
const response = await api.get(`/admin/inventory-items`, adminHeaders)
|
await api.post(
|
||||||
|
`/admin/inventory-items/${inventoryItemId}/location-levels`,
|
||||||
|
{
|
||||||
|
location_id: location3Id,
|
||||||
|
stocked_quantity: 5,
|
||||||
|
},
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
expect(response.data.inventory_items).toHaveLength(1)
|
const unfilteredResponse = await api.get(
|
||||||
expect(response.data.inventory_items[0]).toEqual(
|
`/admin/inventory-items`,
|
||||||
expect.objectContaining({
|
adminHeaders
|
||||||
id: inventoryItemId,
|
)
|
||||||
sku: "MY_SKU",
|
expect(unfilteredResponse.data.inventory_items).toHaveLength(2)
|
||||||
origin_country: "UK",
|
|
||||||
hs_code: "hs001",
|
const response = await api.get(
|
||||||
mid_code: "mids",
|
`/admin/inventory-items?location_id=${location3Id}`,
|
||||||
material: "material",
|
adminHeaders
|
||||||
weight: 300,
|
)
|
||||||
length: 100,
|
|
||||||
height: 200,
|
expect(response.data.inventory_items).toHaveLength(1)
|
||||||
width: 150,
|
expect(response.data.inventory_items[0]).toEqual(
|
||||||
requires_shipping: true,
|
expect.objectContaining({
|
||||||
metadata: null,
|
id: inventoryItemId,
|
||||||
variants: expect.arrayContaining([
|
sku: "MY_SKU",
|
||||||
expect.objectContaining({
|
})
|
||||||
id: variantId,
|
)
|
||||||
title: "Test Variant w. inventory",
|
})
|
||||||
product_id: "test-product",
|
|
||||||
sku: "MY_SKU",
|
it("Lists inventory items", async () => {
|
||||||
manage_inventory: true,
|
const api = useApi()
|
||||||
hs_code: "hs001",
|
const inventoryItemId = inventoryItems[0].id
|
||||||
origin_country: "UK",
|
|
||||||
mid_code: "mids",
|
await api.post(
|
||||||
material: "material",
|
`/admin/inventory-items/${inventoryItemId}/location-levels`,
|
||||||
weight: 300,
|
{
|
||||||
length: 100,
|
location_id: location2Id,
|
||||||
height: 200,
|
stocked_quantity: 10,
|
||||||
width: 150,
|
},
|
||||||
metadata: null,
|
adminHeaders
|
||||||
product: expect.objectContaining({
|
)
|
||||||
id: "test-product",
|
|
||||||
|
await api.post(
|
||||||
|
`/admin/inventory-items/${inventoryItemId}/location-levels`,
|
||||||
|
{
|
||||||
|
location_id: location3Id,
|
||||||
|
stocked_quantity: 5,
|
||||||
|
},
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
const response = await api.get(`/admin/inventory-items`, adminHeaders)
|
||||||
|
|
||||||
|
expect(response.data.inventory_items).toHaveLength(1)
|
||||||
|
expect(response.data.inventory_items[0]).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: inventoryItemId,
|
||||||
|
sku: "MY_SKU",
|
||||||
|
origin_country: "UK",
|
||||||
|
hs_code: "hs001",
|
||||||
|
mid_code: "mids",
|
||||||
|
material: "material",
|
||||||
|
weight: 300,
|
||||||
|
length: 100,
|
||||||
|
height: 200,
|
||||||
|
width: 150,
|
||||||
|
requires_shipping: true,
|
||||||
|
metadata: null,
|
||||||
|
variants: expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: variantId,
|
||||||
|
title: "Test Variant w. inventory",
|
||||||
|
product_id: "test-product",
|
||||||
|
sku: "MY_SKU",
|
||||||
|
manage_inventory: true,
|
||||||
|
hs_code: "hs001",
|
||||||
|
origin_country: "UK",
|
||||||
|
mid_code: "mids",
|
||||||
|
material: "material",
|
||||||
|
weight: 300,
|
||||||
|
length: 100,
|
||||||
|
height: 200,
|
||||||
|
width: 150,
|
||||||
|
metadata: null,
|
||||||
|
product: expect.objectContaining({
|
||||||
|
id: "test-product",
|
||||||
|
}),
|
||||||
}),
|
}),
|
||||||
}),
|
]),
|
||||||
]),
|
location_levels: expect.arrayContaining([
|
||||||
location_levels: expect.arrayContaining([
|
expect.objectContaining({
|
||||||
expect.objectContaining({
|
id: expect.any(String),
|
||||||
id: expect.any(String),
|
inventory_item_id: inventoryItemId,
|
||||||
inventory_item_id: inventoryItemId,
|
location_id: location2Id,
|
||||||
location_id: location2Id,
|
stocked_quantity: 10,
|
||||||
stocked_quantity: 10,
|
reserved_quantity: 0,
|
||||||
reserved_quantity: 0,
|
incoming_quantity: 0,
|
||||||
incoming_quantity: 0,
|
metadata: null,
|
||||||
metadata: null,
|
available_quantity: 10,
|
||||||
available_quantity: 10,
|
}),
|
||||||
}),
|
expect.objectContaining({
|
||||||
expect.objectContaining({
|
id: expect.any(String),
|
||||||
id: expect.any(String),
|
inventory_item_id: inventoryItemId,
|
||||||
inventory_item_id: inventoryItemId,
|
location_id: location3Id,
|
||||||
location_id: location3Id,
|
stocked_quantity: 5,
|
||||||
stocked_quantity: 5,
|
reserved_quantity: 0,
|
||||||
reserved_quantity: 0,
|
incoming_quantity: 0,
|
||||||
incoming_quantity: 0,
|
metadata: null,
|
||||||
metadata: null,
|
available_quantity: 5,
|
||||||
available_quantity: 5,
|
}),
|
||||||
}),
|
]),
|
||||||
]),
|
})
|
||||||
})
|
)
|
||||||
)
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("When deleting an inventory item it removes the product variants associated to it", async () => {
|
it("When deleting an inventory item it removes the product variants associated to it", async () => {
|
||||||
|
|||||||
@@ -16,13 +16,17 @@ export function getListQuery(
|
|||||||
const inventoryItemRepository = manager.getRepository(InventoryItem)
|
const inventoryItemRepository = manager.getRepository(InventoryItem)
|
||||||
|
|
||||||
const { q, ...selectorRest } = selector
|
const { q, ...selectorRest } = selector
|
||||||
const query = buildQuery(selectorRest, config) as ExtendedFindConfig<InventoryItem> & {
|
const query = buildQuery(
|
||||||
where: FindOptionsWhere<InventoryItem & {
|
selectorRest,
|
||||||
location_id?: string
|
config
|
||||||
}>
|
) as ExtendedFindConfig<InventoryItem> & {
|
||||||
|
where: FindOptionsWhere<
|
||||||
|
InventoryItem & {
|
||||||
|
location_id?: string
|
||||||
|
}
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const queryBuilder = inventoryItemRepository.createQueryBuilder("inv_item")
|
const queryBuilder = inventoryItemRepository.createQueryBuilder("inv_item")
|
||||||
|
|
||||||
if (q) {
|
if (q) {
|
||||||
@@ -65,7 +69,9 @@ export function getListQuery(
|
|||||||
const toSelect: string[] = []
|
const toSelect: string[] = []
|
||||||
const parsed = Object.entries(query.order).reduce((acc, [k, v]) => {
|
const parsed = Object.entries(query.order).reduce((acc, [k, v]) => {
|
||||||
const key = `inv_item.${k}`
|
const key = `inv_item.${k}`
|
||||||
toSelect.push(key)
|
if (!query.select?.[k]) {
|
||||||
|
toSelect.push(key)
|
||||||
|
}
|
||||||
acc[key] = v
|
acc[key] = v
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
|
|||||||
Reference in New Issue
Block a user