feat: Add exchange return shipping (#8108)

* wip

* finalize tests

* feat: Add exchange return shipping

* add shipping to preview

* test input

* move utils and ignore already inserted shipping method

* use custom price

---------

Co-authored-by: Carlos R. L. Rodrigues <rodrigolr@gmail.com>
This commit is contained in:
Oli Juhl
2024-07-15 22:04:20 +02:00
committed by GitHub
parent b38c0488be
commit ffd4b195ee
25 changed files with 511 additions and 165 deletions

View File

@@ -0,0 +1,126 @@
import {
beginExchangeOrderWorkflow,
createExchangeReturnShippingMethodWorkflow,
} from "@medusajs/core-flows"
import { OrderDTO, OrderExchangeDTO } from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createOrderFixture, prepareDataFixtures } from "../__fixtures__"
jest.setTimeout(50000)
medusaIntegrationTestRunner({
env: { MEDUSA_FF_MEDUSA_V2: true },
testSuite: ({ getContainer }) => {
let container
beforeAll(() => {
container = getContainer()
})
describe("Order change: Create exchange return shipping", () => {
let order: OrderDTO
let fixtures
let exchangeOrder: OrderExchangeDTO
beforeEach(async () => {
fixtures = await prepareDataFixtures({ container })
order = await createOrderFixture({
container,
product: fixtures.product,
location: fixtures.location,
inventoryItem: fixtures.inventoryItem,
})
await beginExchangeOrderWorkflow(container).run({
input: { order_id: order.id },
throwOnError: true,
})
const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
const remoteQueryObject = remoteQueryObjectFromString({
entryPoint: "order_exchange",
variables: { order_id: order.id },
fields: ["order_id", "id", "status", "order_change_id", "return_id"],
})
;[exchangeOrder] = await remoteQuery(remoteQueryObject)
})
describe("createExchangeReturnShippingMethodWorkflow", () => {
it("should successfully add exchange return shipping to order changes", async () => {
const shippingOptionId = fixtures.shippingOption.id
const { result } = await createExchangeReturnShippingMethodWorkflow(
container
).run({
input: {
exchangeId: exchangeOrder.id,
shippingOptionId: shippingOptionId,
},
})
const orderChange = result?.[0]
expect(orderChange).toEqual(
expect.objectContaining({
id: expect.any(String),
reference: "order_shipping_method",
reference_id: expect.any(String),
details: {
exchange_id: exchangeOrder.id,
order_id: exchangeOrder.order_id,
return_id: exchangeOrder.return_id,
},
raw_amount: { value: "10", precision: 20 },
applied: false,
action: "SHIPPING_ADD",
amount: 10,
})
)
})
it("should successfully add return shipping with custom price to order changes", async () => {
const shippingOptionId = fixtures.shippingOption.id
const { result } = await createExchangeReturnShippingMethodWorkflow(
container
).run({
input: {
exchangeId: exchangeOrder.id,
shippingOptionId: shippingOptionId,
customShippingPrice: 20,
},
})
const orderChange = result?.[0]
expect(orderChange).toEqual(
expect.objectContaining({
id: expect.any(String),
reference: "order_shipping_method",
reference_id: expect.any(String),
details: {
exchange_id: exchangeOrder.id,
order_id: exchangeOrder.order_id,
return_id: exchangeOrder.return_id,
},
raw_amount: { value: "20", precision: 20 },
applied: false,
action: "SHIPPING_ADD",
amount: 20,
})
)
})
})
})
},
})

View File

@@ -2,9 +2,10 @@ import {
beginReturnOrderWorkflow,
createReturnShippingMethodWorkflow,
} from "@medusajs/core-flows"
import { OrderDTO, ReturnDTO } from "@medusajs/types"
import { IFulfillmentModuleService, OrderDTO, ReturnDTO } from "@medusajs/types"
import {
ContainerRegistrationKeys,
ModuleRegistrationName,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
@@ -23,6 +24,7 @@ medusaIntegrationTestRunner({
describe("Order change: Create return shipping", () => {
let order: OrderDTO
let service: IFulfillmentModuleService
let fixtures
let returnOrder: ReturnDTO
@@ -52,6 +54,7 @@ medusaIntegrationTestRunner({
fields: ["order_id", "id", "status", "order_change_id"],
})
service = container.resolve(ModuleRegistrationName.FULFILLMENT)
;[returnOrder] = await remoteQuery(remoteQueryObject)
})
@@ -59,62 +62,50 @@ medusaIntegrationTestRunner({
it("should successfully add return shipping to order changes", async () => {
const shippingOptionId = fixtures.shippingOption.id
const { result } = await createReturnShippingMethodWorkflow(
container
).run({
input: {
return_id: returnOrder.id,
shipping_option_id: shippingOptionId,
},
})
const { result: orderChangePreview } =
await createReturnShippingMethodWorkflow(container).run({
input: {
return_id: returnOrder.id,
shipping_option_id: shippingOptionId,
},
})
const orderChange = result?.[0]
expect(orderChange).toEqual(
expect(orderChangePreview.shipping_methods[1].actions).toEqual([
expect.objectContaining({
id: expect.any(String),
reference: "order_shipping_method",
reference_id: expect.any(String),
order_id: returnOrder.order_id,
return_id: returnOrder.id,
details: {},
raw_amount: { value: "10", precision: 20 },
applied: false,
action: "SHIPPING_ADD",
amount: 10,
})
)
}),
])
})
it("should successfully add return shipping with custom price to order changes", async () => {
const shippingOptionId = fixtures.shippingOption.id
const { result } = await createReturnShippingMethodWorkflow(
container
).run({
input: {
return_id: returnOrder.id,
shipping_option_id: shippingOptionId,
custom_price: 20,
},
})
const { result: orderChangePreview } =
await createReturnShippingMethodWorkflow(container).run({
input: {
return_id: returnOrder.id,
shipping_option_id: shippingOptionId,
custom_price: 20,
},
})
const orderChange = result?.[0]
expect(orderChange).toEqual(
expect(orderChangePreview.shipping_methods[1].actions).toEqual([
expect.objectContaining({
id: expect.any(String),
reference: "order_shipping_method",
reference_id: expect.any(String),
order_id: returnOrder.order_id,
return_id: returnOrder.id,
details: {},
raw_amount: { value: "20", precision: 20 },
applied: false,
action: "SHIPPING_ADD",
amount: 20,
})
)
}),
])
})
})
})