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:
@@ -1744,6 +1744,165 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("creates product with variant inventory kits", async () => {
|
||||
const inventoryItem1 = (
|
||||
await api.post(
|
||||
`/admin/inventory-items`,
|
||||
{ sku: "inventory-1" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.inventory_item
|
||||
|
||||
const inventoryItem2 = (
|
||||
await api.post(
|
||||
`/admin/inventory-items`,
|
||||
{ sku: "inventory-2" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.inventory_item
|
||||
|
||||
const inventoryItem3 = (
|
||||
await api.post(
|
||||
`/admin/inventory-items`,
|
||||
{ sku: "inventory-3" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.inventory_item
|
||||
|
||||
const payload = {
|
||||
title: "Test product - 1",
|
||||
handle: "test-1",
|
||||
variants: [
|
||||
{
|
||||
title: "Custom inventory 1",
|
||||
prices: [{ currency_code: "usd", amount: 100 }],
|
||||
manage_inventory: true,
|
||||
inventory_items: [
|
||||
{
|
||||
inventory_item_id: inventoryItem1.id,
|
||||
required_quantity: 4,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "No inventory",
|
||||
prices: [{ currency_code: "usd", amount: 100 }],
|
||||
manage_inventory: false,
|
||||
},
|
||||
{
|
||||
title: "Default Inventory",
|
||||
prices: [{ currency_code: "usd", amount: 100 }],
|
||||
manage_inventory: true,
|
||||
},
|
||||
{
|
||||
title: "Custom inventory 2",
|
||||
prices: [{ currency_code: "usd", amount: 100 }],
|
||||
manage_inventory: true,
|
||||
inventory_items: [
|
||||
{
|
||||
inventory_item_id: inventoryItem2.id,
|
||||
required_quantity: 5,
|
||||
},
|
||||
{
|
||||
inventory_item_id: inventoryItem3.id,
|
||||
required_quantity: 6,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/admin/products?fields=%2bvariants.inventory_items.inventory.*,%2bvariants.inventory_items.*",
|
||||
payload,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.product).toEqual(
|
||||
expect.objectContaining({
|
||||
title: "Test product - 1",
|
||||
variants: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
title: "Custom inventory 1",
|
||||
manage_inventory: true,
|
||||
inventory_items: [
|
||||
expect.objectContaining({
|
||||
required_quantity: 4,
|
||||
inventory_item_id: inventoryItem1.id,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
title: "No inventory",
|
||||
manage_inventory: false,
|
||||
inventory_items: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
title: "Default Inventory",
|
||||
manage_inventory: true,
|
||||
inventory_items: [
|
||||
expect.objectContaining({
|
||||
required_quantity: 1,
|
||||
inventory_item_id: expect.any(String),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
title: "Custom inventory 2",
|
||||
manage_inventory: true,
|
||||
inventory_items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
required_quantity: 5,
|
||||
inventory_item_id: inventoryItem2.id,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
required_quantity: 6,
|
||||
inventory_item_id: inventoryItem3.id,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when inventory item does not exist", async () => {
|
||||
const payload = {
|
||||
title: "Test product - 1",
|
||||
handle: "test-1",
|
||||
variants: [
|
||||
{
|
||||
title: "Custom inventory 1",
|
||||
prices: [{ currency_code: "usd", amount: 100 }],
|
||||
manage_inventory: true,
|
||||
inventory_items: [
|
||||
{
|
||||
inventory_item_id: "does-not-exist",
|
||||
required_quantity: 4,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post("/admin/products", payload, adminHeaders)
|
||||
.catch((err) => err)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data).toEqual(
|
||||
expect.objectContaining({
|
||||
type: "invalid_data",
|
||||
message: "Inventory Items with ids: does-not-exist was not found",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/products/:id/options/:option_id", () => {
|
||||
@@ -2908,11 +3067,14 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.variant.inventory_items[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
required_quantity: 5,
|
||||
inventory_item_id: inventoryItem.id,
|
||||
})
|
||||
expect(res.data.variant.inventory_items).toHaveLength(2)
|
||||
expect(res.data.variant.inventory_items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
required_quantity: 5,
|
||||
inventory_item_id: inventoryItem.id,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -2963,13 +3125,15 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.variant.inventory_items)
|
||||
expect(res.data.variant.inventory_items).toEqual([
|
||||
expect.objectContaining({
|
||||
required_quantity: 10,
|
||||
inventory_item_id: inventoryItem.id,
|
||||
}),
|
||||
])
|
||||
expect(res.data.variant.inventory_items).toHaveLength(2)
|
||||
expect(res.data.variant.inventory_items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
required_quantity: 10,
|
||||
inventory_item_id: inventoryItem.id,
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3008,8 +3172,10 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.data.parent.inventory_items)
|
||||
expect(res.data.parent.inventory_items).toEqual([])
|
||||
expect(res.data.parent.inventory_items).toHaveLength(1)
|
||||
expect(res.data.parent.inventory_items[0].id).not.toBe(
|
||||
inventoryItem.id
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3131,11 +3297,14 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
).data.variant
|
||||
|
||||
expect(createdLinkVariant.inventory_items[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
required_quantity: 15,
|
||||
inventory_item_id: inventoryItemToCreate.id,
|
||||
})
|
||||
expect(createdLinkVariant.inventory_items).toHaveLength(2)
|
||||
expect(createdLinkVariant.inventory_items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
required_quantity: 15,
|
||||
inventory_item_id: inventoryItemToCreate.id,
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
const updatedLinkVariant = (
|
||||
@@ -3145,11 +3314,14 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
).data.variant
|
||||
|
||||
expect(updatedLinkVariant.inventory_items[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
required_quantity: 25,
|
||||
inventory_item_id: inventoryItemToUpdate.id,
|
||||
})
|
||||
expect(updatedLinkVariant.inventory_items).toHaveLength(2)
|
||||
expect(updatedLinkVariant.inventory_items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
required_quantity: 25,
|
||||
inventory_item_id: inventoryItemToUpdate.id,
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
const deletedLinkVariant = (
|
||||
@@ -3159,7 +3331,10 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
).data.variant
|
||||
|
||||
expect(deletedLinkVariant.inventory_items).toHaveLength(0)
|
||||
expect(deletedLinkVariant.inventory_items).toHaveLength(1)
|
||||
expect(deletedLinkVariant.inventory_items[0].id).not.toEqual(
|
||||
inventoryItemToDelete.id
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user