chore(core-flows): adjust inventory when return is received (#8222)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-23 13:36:46 -03:00
committed by GitHub
parent f38f6d53b4
commit 7a5349c0ae
20 changed files with 579 additions and 286 deletions
@@ -1,4 +1,9 @@
import { ModuleRegistrationName, RuleOperator } from "@medusajs/utils"
import {
ContainerRegistrationKeys,
ModuleRegistrationName,
Modules,
RuleOperator,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
adminHeaders,
@@ -14,11 +19,35 @@ medusaIntegrationTestRunner({
let shippingProfile
let fulfillmentSet
let returnReason
let inventoryItem
let location
beforeEach(async () => {
const container = getContainer()
await createAdminUser(dbConnection, adminHeaders, container)
const product = (
await api.post(
"/admin/products",
{
title: "Test product",
variants: [
{
title: "Test variant",
sku: "test-variant",
prices: [
{
currency_code: "usd",
amount: 10,
},
],
},
],
},
adminHeaders
)
).data.product
returnReason = (
await api.post(
"/admin/return-reasons",
@@ -39,6 +68,7 @@ medusaIntegrationTestRunner({
items: [
{
title: "Custom Item 2",
variant_id: product.variants[0].id,
quantity: 2,
unit_price: 25,
},
@@ -123,7 +153,7 @@ medusaIntegrationTestRunner({
)
).data.shipping_profile
let location = (
location = (
await api.post(
`/admin/stock-locations`,
{
@@ -155,6 +185,54 @@ medusaIntegrationTestRunner({
)
).data.fulfillment_set
inventoryItem = (
await api.post(
`/admin/inventory-items`,
{ sku: "inv-1234" },
adminHeaders
)
).data.inventory_item
await api.post(
`/admin/inventory-items/${inventoryItem.id}/location-levels`,
{
location_id: location.id,
stocked_quantity: 2,
},
adminHeaders
)
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: "test",
},
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
},
{
[Modules.PRODUCT]: {
variant_id: product.variants[0].id,
},
[Modules.INVENTORY]: {
inventory_item_id: inventoryItem.id,
},
},
])
const shippingOptionPayload = {
name: "Return shipping",
service_zone_id: fulfillmentSet.service_zones[0].id,
@@ -715,6 +793,7 @@ medusaIntegrationTestRunner({
{
order_id: order.id,
description: "Test",
location_id: location.id,
},
adminHeaders
)
@@ -745,6 +824,14 @@ medusaIntegrationTestRunner({
})
it("should receive the return", async () => {
let inventoryLevel = (
await api.get(
`/admin/inventory-items/${inventoryItem.id}/location-levels?location_id[]=${location.id}`,
adminHeaders
)
).data.inventory_levels
expect(inventoryLevel[0].stocked_quantity).toEqual(2)
let result = await api.post(
`/admin/returns/${returnId}/receive`,
{
@@ -838,6 +925,14 @@ medusaIntegrationTestRunner({
],
})
)
inventoryLevel = (
await api.get(
`/admin/inventory-items/${inventoryItem.id}/location-levels?location_id[]=${location.id}`,
adminHeaders
)
).data.inventory_levels
expect(inventoryLevel[0].stocked_quantity).toEqual(3)
})
})
})
@@ -27,6 +27,7 @@ jest.setTimeout(500000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const providerId = "manual_test-provider"
const variantSkuWithInventory = "test-variant"
let inventoryItem
async function prepareDataFixtures({ container }) {
@@ -97,7 +98,7 @@ async function prepareDataFixtures({ container }) {
variants: [
{
title: "Test variant",
sku: "test-variant",
sku: variantSkuWithInventory,
},
{
title: "Test variant no inventory management",
@@ -225,6 +226,7 @@ async function createOrderFixture({ container, product, location }) {
title: "Custom Item 2",
variant_sku: product.variants[0].sku,
variant_title: product.variants[0].title,
variant_id: product.variants[0].id,
quantity: 1,
unit_price: 50,
adjustments: [
@@ -351,6 +353,9 @@ medusaIntegrationTestRunner({
)
const order = await createOrderFixture({ container, product, location })
const itemWithInventory = order.items!.find(
(o) => o.variant_sku === variantSkuWithInventory
)!
// Create a fulfillment
const createOrderFulfillmentData: OrderWorkflow.CreateOrderFulfillmentWorkflowInput =
@@ -359,7 +364,7 @@ medusaIntegrationTestRunner({
created_by: "user_1",
items: [
{
id: order.items![0].id,
id: itemWithInventory.id,
quantity: 1,
},
],
@@ -391,11 +396,17 @@ medusaIntegrationTestRunner({
const [orderFulfill] = await remoteQuery(remoteQueryObject)
let orderFulfillItemWithInventory = orderFulfill.items!.find(
(o) => o.variant_sku === variantSkuWithInventory
)!
expect(orderFulfill.fulfillments).toHaveLength(1)
expect(orderFulfill.items[0].detail.fulfilled_quantity).toEqual(1)
expect(orderFulfillItemWithInventory.detail.fulfilled_quantity).toEqual(
1
)
const reservation = await inventoryModule.listReservationItems({
line_item_id: order.items![0].id,
line_item_id: itemWithInventory.id,
})
expect(reservation).toHaveLength(0)
@@ -436,10 +447,14 @@ medusaIntegrationTestRunner({
remoteQueryObjectFulfill
)
orderFulfillItemWithInventory = orderFulfillAfterCancelled.items!.find(
(o) => o.variant_sku === variantSkuWithInventory
)!
expect(orderFulfillAfterCancelled.fulfillments).toHaveLength(1)
expect(
orderFulfillAfterCancelled.items[0].detail.fulfilled_quantity
).toEqual(0)
expect(orderFulfillItemWithInventory.detail.fulfilled_quantity).toEqual(
0
)
const stockAvailabilityAfterCancelled =
await inventoryModule.retrieveStockedQuantity(inventoryItem.id, [