fix(core-flows): handle inventory kit items in mark-as-shipped/delivered flows (#12269)
* fix(core-flows): handle invetory kit items in mark-as-shipped and mark-as-delivered flows * fix: typo * chore: more assertion * chore: fix comment * Update packages/core/core-flows/src/order/workflows/create-shipment.ts Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> * fix: undo comment --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
@@ -1534,6 +1534,367 @@ medusaIntegrationTestRunner({
|
||||
message: "Fulfillment has already been marked delivered",
|
||||
})
|
||||
})
|
||||
|
||||
describe("with inventory kit items", () => {
|
||||
let inventoryItemDesk
|
||||
let inventoryItemLeg
|
||||
|
||||
beforeEach(async () => {
|
||||
const container = getContainer()
|
||||
|
||||
const publishableKey = await generatePublishableKey(container)
|
||||
|
||||
const storeHeaders = generateStoreHeaders({
|
||||
publishableKey,
|
||||
})
|
||||
|
||||
const region = (
|
||||
await api.post(
|
||||
"/admin/regions",
|
||||
{ name: "Test region", currency_code: "usd" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.region
|
||||
|
||||
const salesChannel = (
|
||||
await api.post(
|
||||
"/admin/sales-channels",
|
||||
{ name: "first channel", description: "channel" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.sales_channel
|
||||
|
||||
const stockLocation = (
|
||||
await api.post(
|
||||
`/admin/stock-locations`,
|
||||
{ name: "test location" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location
|
||||
|
||||
inventoryItemDesk = (
|
||||
await api.post(
|
||||
`/admin/inventory-items`,
|
||||
{ sku: "table-desk" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.inventory_item
|
||||
|
||||
inventoryItemLeg = (
|
||||
await api.post(
|
||||
`/admin/inventory-items`,
|
||||
{ sku: "table-leg" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.inventory_item
|
||||
|
||||
await api.post(
|
||||
`/admin/inventory-items/${inventoryItemDesk.id}/location-levels`,
|
||||
{
|
||||
location_id: stockLocation.id,
|
||||
stocked_quantity: 10,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/inventory-items/${inventoryItemLeg.id}/location-levels`,
|
||||
{
|
||||
location_id: stockLocation.id,
|
||||
stocked_quantity: 40,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/sales-channels`,
|
||||
{ add: [salesChannel.id] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const shippingProfile = (
|
||||
await api.post(
|
||||
`/admin/shipping-profiles`,
|
||||
{ name: `test-${stockLocation.id}`, type: "default" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_profile
|
||||
|
||||
const product = (
|
||||
await api.post(
|
||||
"/admin/products",
|
||||
{
|
||||
title: `Wooden table`,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
options: [{ title: "color", values: ["green"] }],
|
||||
variants: [
|
||||
{
|
||||
title: "Green table",
|
||||
sku: "green-table",
|
||||
inventory_items: [
|
||||
{
|
||||
inventory_item_id: inventoryItemDesk.id,
|
||||
required_quantity: 1,
|
||||
},
|
||||
{
|
||||
inventory_item_id: inventoryItemLeg.id,
|
||||
required_quantity: 4,
|
||||
},
|
||||
],
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 100,
|
||||
},
|
||||
],
|
||||
options: {
|
||||
color: "green",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
const fulfillmentSets = (
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
|
||||
{
|
||||
name: `Test-${shippingProfile.id}`,
|
||||
type: "test-type",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location.fulfillment_sets
|
||||
|
||||
const fulfillmentSet = (
|
||||
await api.post(
|
||||
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
|
||||
{
|
||||
name: `Test-${shippingProfile.id}`,
|
||||
geo_zones: [{ type: "country", country_code: "us" }],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.fulfillment_set
|
||||
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
|
||||
{ add: ["manual_test-provider"] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const shippingOption = (
|
||||
await api.post(
|
||||
`/admin/shipping-options`,
|
||||
{
|
||||
name: `Test shipping option ${fulfillmentSet.id}`,
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
prices: [{ currency_code: "usd", amount: 1000 }],
|
||||
rules: [],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_option
|
||||
|
||||
const cart = (
|
||||
await api.post(
|
||||
`/store/carts`,
|
||||
{
|
||||
currency_code: "usd",
|
||||
email: "tony@stark-industries.com",
|
||||
region_id: region.id,
|
||||
shipping_address: {
|
||||
address_1: "test address 1",
|
||||
address_2: "test address 2",
|
||||
city: "ny",
|
||||
country_code: "us",
|
||||
province: "ny",
|
||||
postal_code: "94016",
|
||||
},
|
||||
billing_address: {
|
||||
address_1: "test billing address 1",
|
||||
address_2: "test billing address 2",
|
||||
city: "ny",
|
||||
country_code: "us",
|
||||
province: "ny",
|
||||
postal_code: "94016",
|
||||
},
|
||||
sales_channel_id: salesChannel.id,
|
||||
items: [{ quantity: 2, variant_id: product.variants[0].id }],
|
||||
},
|
||||
storeHeaders
|
||||
)
|
||||
).data.cart
|
||||
|
||||
await api.post(
|
||||
`/store/carts/${cart.id}/shipping-methods`,
|
||||
{ option_id: shippingOption.id },
|
||||
storeHeaders
|
||||
)
|
||||
|
||||
const paymentCollection = (
|
||||
await api.post(
|
||||
`/store/payment-collections`,
|
||||
{
|
||||
cart_id: cart.id,
|
||||
},
|
||||
storeHeaders
|
||||
)
|
||||
).data.payment_collection
|
||||
|
||||
await api.post(
|
||||
`/store/payment-collections/${paymentCollection.id}/payment-sessions`,
|
||||
{ provider_id: "pp_system_default" },
|
||||
storeHeaders
|
||||
)
|
||||
|
||||
order = (
|
||||
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
|
||||
).data.order
|
||||
})
|
||||
|
||||
it("set correct quantity as delivered on the line item when marking fulfillment as delivered", async () => {
|
||||
let reservations = (
|
||||
await api.get(`/admin/reservations`, adminHeaders)
|
||||
).data.reservations
|
||||
|
||||
expect(reservations).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
inventory_item_id: inventoryItemDesk.id,
|
||||
quantity: 2,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
inventory_item_id: inventoryItemLeg.id,
|
||||
quantity: 8,
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
// 1. create a partial fulfillment
|
||||
const fulOrder = (
|
||||
await api.post(
|
||||
`/admin/orders/${order.id}/fulfillments?fields=*fulfillments,*fulfillments.items`,
|
||||
{
|
||||
items: [{ id: order.items[0].id, quantity: 1 }],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order
|
||||
|
||||
// 2. two fulfillment items are created for a single (inventory kit) line item
|
||||
expect(fulOrder.fulfillments[0].items).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
inventory_item_id: inventoryItemDesk.id,
|
||||
quantity: 1,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
inventory_item_id: inventoryItemLeg.id,
|
||||
quantity: 4,
|
||||
}),
|
||||
])
|
||||
)
|
||||
|
||||
expect(fulOrder.items[0].detail.fulfilled_quantity).toEqual(1)
|
||||
|
||||
// 3. mark the fulfillment as shipped
|
||||
const shippedOrder = (
|
||||
await api.post(
|
||||
`/admin/orders/${fulOrder.id}/fulfillments/${fulOrder.fulfillments[0].id}/shipments`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
id: fulOrder.items[0].id,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order
|
||||
|
||||
expect(shippedOrder.items[0].detail.fulfilled_quantity).toEqual(1)
|
||||
expect(shippedOrder.items[0].detail.shipped_quantity).toEqual(1)
|
||||
|
||||
// 4. mark the fulfillment as delivered
|
||||
const deliveredOrder = (
|
||||
await api.post(
|
||||
`/admin/orders/${fulOrder.id}/fulfillments/${fulOrder.fulfillments[0].id}/mark-as-delivered`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order
|
||||
|
||||
// 5. 1 line item was fulfilled so 1 line item is delivered
|
||||
expect(deliveredOrder.items[0].detail.fulfilled_quantity).toEqual(1)
|
||||
expect(deliveredOrder.items[0].detail.shipped_quantity).toEqual(1)
|
||||
expect(deliveredOrder.items[0].detail.delivered_quantity).toEqual(1)
|
||||
|
||||
// 6. repeat the same steps for the rest of the line items
|
||||
|
||||
// 7. create a partial fulfillment
|
||||
const fulOrder2 = (
|
||||
await api.post(
|
||||
`/admin/orders/${order.id}/fulfillments?fields=*fulfillments,*fulfillments.items`,
|
||||
{
|
||||
items: [{ id: order.items[0].id, quantity: 1 }],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order
|
||||
|
||||
expect(fulOrder2.items[0].detail.fulfilled_quantity).toEqual(2)
|
||||
|
||||
const secondFulfillment = fulOrder2.fulfillments.find(
|
||||
(f) => !f.shipped_at
|
||||
)!
|
||||
|
||||
// 8. mark the fulfillment as shipped
|
||||
const shippedOrder2 = (
|
||||
await api.post(
|
||||
`/admin/orders/${fulOrder2.id}/fulfillments/${secondFulfillment.id}/shipments`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
id: fulOrder2.items[0].id,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order
|
||||
|
||||
expect(shippedOrder2.items[0].detail.fulfilled_quantity).toEqual(2)
|
||||
expect(shippedOrder2.items[0].detail.shipped_quantity).toEqual(2)
|
||||
expect(shippedOrder2.items[0].detail.delivered_quantity).toEqual(1)
|
||||
|
||||
// 9. mark the fulfillment as delivered
|
||||
const deliveredOrder2 = (
|
||||
await api.post(
|
||||
`/admin/orders/${fulOrder2.id}/fulfillments/${secondFulfillment.id}/mark-as-delivered`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
).data.order
|
||||
|
||||
// 10. both items are fulfilled, shipped and delivered
|
||||
expect(deliveredOrder2.items[0].detail.fulfilled_quantity).toEqual(2)
|
||||
expect(deliveredOrder2.items[0].detail.shipped_quantity).toEqual(2)
|
||||
expect(deliveredOrder2.items[0].detail.delivered_quantity).toEqual(2)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /orders/:id/credit-lines", () => {
|
||||
|
||||
Reference in New Issue
Block a user