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
2024-06-04 13:49:31 +00:00
committed by GitHub
parent 6646a203df
commit e7005a0aac
9 changed files with 521 additions and 179 deletions
@@ -1823,36 +1823,6 @@ medusaIntegrationTestRunner({
)
).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 = (
await api.post(
`/admin/stock-locations`,
@@ -1902,20 +1872,41 @@ medusaIntegrationTestRunner({
],
})
await remoteLinkService.create([
{
[Modules.STOCK_LOCATION]: { stock_location_id: stockLocation.id },
[Modules.FULFILLMENT]: { fulfillment_set_id: fulfillmentSet.id },
},
{
[Modules.PRODUCT]: {
variant_id: product.variants[0].id,
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",
inventory_items: [
{
inventory_item_id: inventoryItem.id,
required_quantity: 1,
},
],
prices: [
{
currency_code: "usd",
amount: 100,
},
],
options: {
size: "large",
color: "green",
},
},
],
},
[Modules.INVENTORY]: {
inventory_item_id: inventoryItem.id,
},
},
])
adminHeaders
)
).data.product
shippingOption = (
await api.post(
@@ -96,7 +96,8 @@ medusaIntegrationTestRunner({
)
).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
expect(updatedVariant.inventory_items).toEqual([
expect.objectContaining({
required_quantity: originalQuantity,
}),
])
expect(updatedVariant.inventory_items).toHaveLength(2)
expect(updatedVariant.inventory_items).toEqual(
expect.arrayContaining([
expect.objectContaining({
required_quantity: originalQuantity,
}),
])
)
})
it("should throw an error when a link is not found", async () => {
@@ -258,11 +262,14 @@ medusaIntegrationTestRunner({
)
).data.variant
expect(updatedVariant.inventory_items).toEqual([
expect.objectContaining({
required_quantity: originalQuantity,
}),
])
expect(updatedVariant.inventory_items).toHaveLength(2)
expect(updatedVariant.inventory_items).toEqual(
expect.arrayContaining([
expect.objectContaining({
required_quantity: originalQuantity,
}),
])
)
})
it("should pass dismiss step if link not found if next step throws error", async () => {
@@ -304,7 +311,7 @@ medusaIntegrationTestRunner({
)
).data.variant
expect(updatedVariant.inventory_items).toEqual([])
expect(updatedVariant.inventory_items).toHaveLength(1)
})
})
})
@@ -27,6 +27,8 @@ medusaIntegrationTestRunner({
let variant2
let variant3
let variant4
let inventoryItem1
let inventoryItem2
const createProducts = async (data) => {
const response = await api.post(
@@ -88,6 +90,21 @@ medusaIntegrationTestRunner({
describe("GET /store/products", () => {
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({
title: "test product 1",
status: ProductStatus.PUBLISHED,
@@ -95,6 +112,16 @@ medusaIntegrationTestRunner({
{
title: "test variant 1",
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" }],
},
],
@@ -368,8 +395,6 @@ medusaIntegrationTestRunner({
describe("with inventory items", () => {
let location1
let location2
let inventoryItem1
let inventoryItem2
let salesChannel1
let publishableKey1
@@ -409,43 +434,23 @@ medusaIntegrationTestRunner({
adminHeaders
)
inventoryItem1 = (
await api.post(
`/admin/inventory-items`,
{ sku: "test-sku" },
adminHeaders
)
).data.inventory_item
await api.post(
`/admin/inventory-items/${inventoryItem1.id}/location-levels`,
{
location_id: location1.id,
stocked_quantity: 20,
},
adminHeaders
)
inventoryItem2 = (
await api.post(
`/admin/inventory-items`,
{ sku: "test-sku-2" },
adminHeaders
)
).data.inventory_item
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
await api.post(
`/admin/inventory-items/${inventoryItem2.id}/location-levels`,
{
location_id: location2.id,
stocked_quantity: 30,
},
adminHeaders
)
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
@@ -465,24 +470,6 @@ medusaIntegrationTestRunner({
})
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(
`/store/products?sales_channel_id[]=${salesChannel1.id}&fields=variants.inventory_items.inventory.location_levels.*`,
{ 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 () => {
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
await api.post(
`/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 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 },
},
])
await api.post(
`/admin/products/${product.id}/variants/${variant.id}/inventory-items`,
{ required_quantity: 20, inventory_item_id: inventoryItem2.id },
adminHeaders
)
let response = await api.get(
`/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(