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:
co-authored by
Carlos R. L. Rodrigues
parent
b38c0488be
commit
ffd4b195ee
@@ -225,21 +225,11 @@ medusaIntegrationTestRunner({
|
|||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result.data.return).toEqual(
|
expect(result.data.order.shipping_methods[1]).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
amount: 1000,
|
||||||
order_id: order.id,
|
name: "Return shipping",
|
||||||
display_id: 1,
|
shipping_option_id: returnShippingOption.id,
|
||||||
order_version: 2,
|
|
||||||
status: "requested",
|
|
||||||
items: [],
|
|
||||||
shipping_methods: [
|
|
||||||
expect.objectContaining({
|
|
||||||
amount: 1000,
|
|
||||||
name: "Return shipping",
|
|
||||||
shipping_option_id: returnShippingOption.id,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+126
@@ -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,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
+25
-34
@@ -2,9 +2,10 @@ import {
|
|||||||
beginReturnOrderWorkflow,
|
beginReturnOrderWorkflow,
|
||||||
createReturnShippingMethodWorkflow,
|
createReturnShippingMethodWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
import { OrderDTO, ReturnDTO } from "@medusajs/types"
|
import { IFulfillmentModuleService, OrderDTO, ReturnDTO } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
|
ModuleRegistrationName,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||||
@@ -23,6 +24,7 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
describe("Order change: Create return shipping", () => {
|
describe("Order change: Create return shipping", () => {
|
||||||
let order: OrderDTO
|
let order: OrderDTO
|
||||||
|
let service: IFulfillmentModuleService
|
||||||
let fixtures
|
let fixtures
|
||||||
|
|
||||||
let returnOrder: ReturnDTO
|
let returnOrder: ReturnDTO
|
||||||
@@ -52,6 +54,7 @@ medusaIntegrationTestRunner({
|
|||||||
fields: ["order_id", "id", "status", "order_change_id"],
|
fields: ["order_id", "id", "status", "order_change_id"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
service = container.resolve(ModuleRegistrationName.FULFILLMENT)
|
||||||
;[returnOrder] = await remoteQuery(remoteQueryObject)
|
;[returnOrder] = await remoteQuery(remoteQueryObject)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -59,62 +62,50 @@ medusaIntegrationTestRunner({
|
|||||||
it("should successfully add return shipping to order changes", async () => {
|
it("should successfully add return shipping to order changes", async () => {
|
||||||
const shippingOptionId = fixtures.shippingOption.id
|
const shippingOptionId = fixtures.shippingOption.id
|
||||||
|
|
||||||
const { result } = await createReturnShippingMethodWorkflow(
|
const { result: orderChangePreview } =
|
||||||
container
|
await createReturnShippingMethodWorkflow(container).run({
|
||||||
).run({
|
input: {
|
||||||
input: {
|
return_id: returnOrder.id,
|
||||||
return_id: returnOrder.id,
|
shipping_option_id: shippingOptionId,
|
||||||
shipping_option_id: shippingOptionId,
|
},
|
||||||
},
|
})
|
||||||
})
|
|
||||||
|
|
||||||
const orderChange = result?.[0]
|
expect(orderChangePreview.shipping_methods[1].actions).toEqual([
|
||||||
|
|
||||||
expect(orderChange).toEqual(
|
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
reference: "order_shipping_method",
|
reference: "order_shipping_method",
|
||||||
reference_id: expect.any(String),
|
reference_id: expect.any(String),
|
||||||
order_id: returnOrder.order_id,
|
|
||||||
return_id: returnOrder.id,
|
|
||||||
details: {},
|
|
||||||
raw_amount: { value: "10", precision: 20 },
|
raw_amount: { value: "10", precision: 20 },
|
||||||
applied: false,
|
applied: false,
|
||||||
action: "SHIPPING_ADD",
|
action: "SHIPPING_ADD",
|
||||||
amount: 10,
|
amount: 10,
|
||||||
})
|
}),
|
||||||
)
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should successfully add return shipping with custom price to order changes", async () => {
|
it("should successfully add return shipping with custom price to order changes", async () => {
|
||||||
const shippingOptionId = fixtures.shippingOption.id
|
const shippingOptionId = fixtures.shippingOption.id
|
||||||
|
|
||||||
const { result } = await createReturnShippingMethodWorkflow(
|
const { result: orderChangePreview } =
|
||||||
container
|
await createReturnShippingMethodWorkflow(container).run({
|
||||||
).run({
|
input: {
|
||||||
input: {
|
return_id: returnOrder.id,
|
||||||
return_id: returnOrder.id,
|
shipping_option_id: shippingOptionId,
|
||||||
shipping_option_id: shippingOptionId,
|
custom_price: 20,
|
||||||
custom_price: 20,
|
},
|
||||||
},
|
})
|
||||||
})
|
|
||||||
|
|
||||||
const orderChange = result?.[0]
|
expect(orderChangePreview.shipping_methods[1].actions).toEqual([
|
||||||
|
|
||||||
expect(orderChange).toEqual(
|
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
reference: "order_shipping_method",
|
reference: "order_shipping_method",
|
||||||
reference_id: expect.any(String),
|
reference_id: expect.any(String),
|
||||||
order_id: returnOrder.order_id,
|
|
||||||
return_id: returnOrder.id,
|
|
||||||
details: {},
|
|
||||||
raw_amount: { value: "20", precision: 20 },
|
raw_amount: { value: "20", precision: 20 },
|
||||||
applied: false,
|
applied: false,
|
||||||
action: "SHIPPING_ADD",
|
action: "SHIPPING_ADD",
|
||||||
amount: 20,
|
amount: 20,
|
||||||
})
|
}),
|
||||||
)
|
])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import { OrderChangeDTO, OrderDTO, ReturnDTO } from "@medusajs/types"
|
import { OrderChangeDTO, OrderDTO, ReturnDTO } from "@medusajs/types"
|
||||||
import { ChangeActionType } from "@medusajs/utils"
|
import { ChangeActionType } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
|
WorkflowData,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
WorkflowData,
|
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import { useRemoteQueryStep } from "../../common"
|
import { useRemoteQueryStep } from "../../common"
|
||||||
import { previewOrderChangeStep } from "../steps"
|
|
||||||
import { confirmOrderChanges } from "../steps/confirm-order-changes"
|
import { confirmOrderChanges } from "../steps/confirm-order-changes"
|
||||||
import { createReturnItems } from "../steps/create-return-items"
|
import { createReturnItems } from "../steps/create-return-items"
|
||||||
import {
|
import {
|
||||||
@@ -39,7 +38,7 @@ const validationStep = createStep(
|
|||||||
export const confirmReturnRequestWorkflowId = "confirm-return-request"
|
export const confirmReturnRequestWorkflowId = "confirm-return-request"
|
||||||
export const confirmReturnRequestWorkflow = createWorkflow(
|
export const confirmReturnRequestWorkflow = createWorkflow(
|
||||||
confirmReturnRequestWorkflowId,
|
confirmReturnRequestWorkflowId,
|
||||||
function (input: WorkflowInput): WorkflowData<OrderDTO> {
|
function (input: WorkflowInput): WorkflowData<void> {
|
||||||
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
||||||
entry_point: "return",
|
entry_point: "return",
|
||||||
fields: ["id", "status", "order_id"],
|
fields: ["id", "status", "order_id"],
|
||||||
@@ -87,7 +86,5 @@ export const confirmReturnRequestWorkflow = createWorkflow(
|
|||||||
createReturnItems({ returnId: orderReturn.id, changes: returnItemActions })
|
createReturnItems({ returnId: orderReturn.id, changes: returnItemActions })
|
||||||
|
|
||||||
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
|
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
|
||||||
|
|
||||||
return previewOrderChangeStep(order.id)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
+147
@@ -0,0 +1,147 @@
|
|||||||
|
import {
|
||||||
|
BigNumberInput,
|
||||||
|
OrderChangeDTO,
|
||||||
|
OrderDTO,
|
||||||
|
OrderExchangeDTO,
|
||||||
|
} from "@medusajs/types"
|
||||||
|
import { ChangeActionType } from "@medusajs/utils"
|
||||||
|
import {
|
||||||
|
createStep,
|
||||||
|
createWorkflow,
|
||||||
|
transform,
|
||||||
|
WorkflowData,
|
||||||
|
} from "@medusajs/workflows-sdk"
|
||||||
|
import { useRemoteQueryStep } from "../../common"
|
||||||
|
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
|
||||||
|
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods"
|
||||||
|
import {
|
||||||
|
throwIfOrderChangeIsNotActive,
|
||||||
|
throwIfOrderIsCancelled,
|
||||||
|
} from "../utils/order-validation"
|
||||||
|
|
||||||
|
const validationStep = createStep(
|
||||||
|
"validate-create-exchange-return-shipping-method",
|
||||||
|
async function ({
|
||||||
|
order,
|
||||||
|
orderChange,
|
||||||
|
}: {
|
||||||
|
order: OrderDTO
|
||||||
|
orderChange: OrderChangeDTO
|
||||||
|
}) {
|
||||||
|
throwIfOrderIsCancelled({ order })
|
||||||
|
throwIfOrderChangeIsNotActive({ orderChange })
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export const createExchangeReturnShippingMethodWorkflowId =
|
||||||
|
"create-exchange-return-shipping-method"
|
||||||
|
export const createExchangeReturnShippingMethodWorkflow = createWorkflow(
|
||||||
|
createExchangeReturnShippingMethodWorkflowId,
|
||||||
|
function (input: {
|
||||||
|
exchangeId: string
|
||||||
|
shippingOptionId: string
|
||||||
|
customShippingPrice?: BigNumberInput
|
||||||
|
}): WorkflowData {
|
||||||
|
const orderExchange: OrderExchangeDTO = useRemoteQueryStep({
|
||||||
|
entry_point: "order_exchange",
|
||||||
|
fields: ["id", "status", "order_id", "return_id"],
|
||||||
|
variables: { id: input.exchangeId },
|
||||||
|
list: false,
|
||||||
|
throw_if_key_not_found: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const order: OrderDTO = useRemoteQueryStep({
|
||||||
|
entry_point: "orders",
|
||||||
|
fields: ["id", "status", "currency_code"],
|
||||||
|
variables: { id: orderExchange.order_id },
|
||||||
|
list: false,
|
||||||
|
throw_if_key_not_found: true,
|
||||||
|
}).config({ name: "order-query" })
|
||||||
|
|
||||||
|
const shippingOptions = useRemoteQueryStep({
|
||||||
|
entry_point: "shipping_option",
|
||||||
|
fields: [
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"calculated_price.calculated_amount",
|
||||||
|
"calculated_price.is_calculated_price_tax_inclusive",
|
||||||
|
],
|
||||||
|
variables: {
|
||||||
|
id: input.shippingOptionId,
|
||||||
|
calculated_price: {
|
||||||
|
context: { currency_code: order.currency_code },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}).config({ name: "fetch-shipping-option" })
|
||||||
|
|
||||||
|
const shippingMethodInput = transform(
|
||||||
|
{ orderExchange, shippingOptions, input },
|
||||||
|
(data) => {
|
||||||
|
const option = data.shippingOptions[0]
|
||||||
|
|
||||||
|
return {
|
||||||
|
shipping_option_id: option.id,
|
||||||
|
amount:
|
||||||
|
data.input.customShippingPrice ??
|
||||||
|
option.calculated_price.calculated_amount,
|
||||||
|
is_tax_inclusive:
|
||||||
|
!!option.calculated_price.is_calculated_price_tax_inclusive,
|
||||||
|
data: option.data ?? {},
|
||||||
|
name: option.name,
|
||||||
|
order_id: data.orderExchange.order_id,
|
||||||
|
return_id: data.orderExchange.return_id,
|
||||||
|
exchange_id: data.orderExchange.id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const createdMethods = createOrderShippingMethods({
|
||||||
|
shipping_methods: [shippingMethodInput],
|
||||||
|
})
|
||||||
|
|
||||||
|
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||||
|
entry_point: "order_change",
|
||||||
|
fields: ["id", "status"],
|
||||||
|
variables: { order_id: orderExchange.order_id },
|
||||||
|
list: false,
|
||||||
|
}).config({ name: "order-change-query" })
|
||||||
|
|
||||||
|
validationStep({ order, orderChange })
|
||||||
|
|
||||||
|
const orderChangeActionInput = transform(
|
||||||
|
{
|
||||||
|
orderId: order.id,
|
||||||
|
returnId: orderExchange.return_id,
|
||||||
|
exchangeId: orderExchange.id,
|
||||||
|
shippingOption: shippingOptions[0],
|
||||||
|
methodId: createdMethods[0].id,
|
||||||
|
customPrice: input.customShippingPrice,
|
||||||
|
},
|
||||||
|
({
|
||||||
|
shippingOption,
|
||||||
|
exchangeId,
|
||||||
|
returnId,
|
||||||
|
orderId,
|
||||||
|
methodId,
|
||||||
|
customPrice,
|
||||||
|
}) => {
|
||||||
|
const methodPrice =
|
||||||
|
customPrice ?? shippingOption.calculated_price.calculated_amount
|
||||||
|
|
||||||
|
return {
|
||||||
|
action: ChangeActionType.SHIPPING_ADD,
|
||||||
|
reference: "order_shipping_method",
|
||||||
|
reference_id: methodId,
|
||||||
|
amount: methodPrice,
|
||||||
|
details: {
|
||||||
|
order_id: orderId,
|
||||||
|
return_id: returnId,
|
||||||
|
exchange_id: exchangeId,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return createOrderChangeActionsStep([orderChangeActionInput])
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -4,16 +4,17 @@ import {
|
|||||||
OrderDTO,
|
OrderDTO,
|
||||||
ReturnDTO,
|
ReturnDTO,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { ChangeActionType } from "@medusajs/utils"
|
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
|
WorkflowData,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
WorkflowData,
|
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import { useRemoteQueryStep } from "../../common"
|
import { useRemoteQueryStep } from "../../common"
|
||||||
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
|
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
|
||||||
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods"
|
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods"
|
||||||
|
import { previewOrderChangeStep } from "../steps/preview-order-change"
|
||||||
import {
|
import {
|
||||||
throwIfIsCancelled,
|
throwIfIsCancelled,
|
||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
@@ -77,10 +78,25 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
|||||||
},
|
},
|
||||||
}).config({ name: "fetch-shipping-option" })
|
}).config({ name: "fetch-shipping-option" })
|
||||||
|
|
||||||
|
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||||
|
entry_point: "order_change",
|
||||||
|
fields: ["id", "status", "version"],
|
||||||
|
variables: {
|
||||||
|
filters: {
|
||||||
|
order_id: orderReturn.order_id,
|
||||||
|
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
list: false,
|
||||||
|
}).config({ name: "order-change-query" })
|
||||||
|
|
||||||
|
validationStep({ order, orderReturn, orderChange })
|
||||||
|
|
||||||
const shippingMethodInput = transform(
|
const shippingMethodInput = transform(
|
||||||
{ orderReturn, shippingOptions },
|
{ orderReturn, shippingOptions, orderChange },
|
||||||
(data) => {
|
(data) => {
|
||||||
const option = data.shippingOptions[0]
|
const option = data.shippingOptions[0]
|
||||||
|
const orderChange = data.orderChange
|
||||||
|
|
||||||
return {
|
return {
|
||||||
shipping_option_id: option.id,
|
shipping_option_id: option.id,
|
||||||
@@ -89,6 +105,7 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
|||||||
!!option.calculated_price.is_calculated_price_tax_inclusive,
|
!!option.calculated_price.is_calculated_price_tax_inclusive,
|
||||||
data: option.data ?? {},
|
data: option.data ?? {},
|
||||||
name: option.name,
|
name: option.name,
|
||||||
|
version: orderChange.version,
|
||||||
order_id: data.orderReturn.order_id,
|
order_id: data.orderReturn.order_id,
|
||||||
return_id: data.orderReturn.id,
|
return_id: data.orderReturn.id,
|
||||||
}
|
}
|
||||||
@@ -99,40 +116,42 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
|||||||
shipping_methods: [shippingMethodInput],
|
shipping_methods: [shippingMethodInput],
|
||||||
})
|
})
|
||||||
|
|
||||||
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
|
||||||
entry_point: "order_change",
|
|
||||||
fields: ["id", "status"],
|
|
||||||
variables: {
|
|
||||||
filters: { order_id: orderReturn.order_id, return_id: orderReturn.id },
|
|
||||||
},
|
|
||||||
list: false,
|
|
||||||
}).config({ name: "order-change-query" })
|
|
||||||
|
|
||||||
validationStep({ order, orderReturn, orderChange })
|
|
||||||
|
|
||||||
const orderChangeActionInput = transform(
|
const orderChangeActionInput = transform(
|
||||||
{
|
{
|
||||||
orderId: order.id,
|
order,
|
||||||
returnId: orderReturn.id,
|
orderReturn,
|
||||||
shippingOption: shippingOptions[0],
|
shippingOptions,
|
||||||
methodId: createdMethods[0].id,
|
createdMethods,
|
||||||
customPrice: input.custom_price,
|
customPrice: input.custom_price,
|
||||||
|
orderChange,
|
||||||
},
|
},
|
||||||
({ shippingOption, returnId, orderId, methodId, customPrice }) => {
|
({
|
||||||
|
shippingOptions,
|
||||||
|
orderReturn,
|
||||||
|
order,
|
||||||
|
createdMethods,
|
||||||
|
customPrice,
|
||||||
|
orderChange,
|
||||||
|
}) => {
|
||||||
|
const shippingOption = shippingOptions[0]
|
||||||
|
const createdMethod = createdMethods[0]
|
||||||
const methodPrice =
|
const methodPrice =
|
||||||
customPrice ?? shippingOption.calculated_price.calculated_amount
|
customPrice ?? shippingOption.calculated_price.calculated_amount
|
||||||
|
|
||||||
return {
|
return {
|
||||||
action: ChangeActionType.SHIPPING_ADD,
|
action: ChangeActionType.SHIPPING_ADD,
|
||||||
reference: "order_shipping_method",
|
reference: "order_shipping_method",
|
||||||
reference_id: methodId,
|
reference_id: createdMethod.id,
|
||||||
|
order_change_id: orderChange.id,
|
||||||
amount: methodPrice,
|
amount: methodPrice,
|
||||||
order_id: orderId,
|
order_id: order.id,
|
||||||
return_id: returnId,
|
return_id: orderReturn.id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return createOrderChangeActionsStep([orderChangeActionInput])
|
createOrderChangeActionsStep([orderChangeActionInput])
|
||||||
|
|
||||||
|
return previewOrderChangeStep(order.id)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export * from "./claim-request-item-return"
|
|||||||
export * from "./complete-orders"
|
export * from "./complete-orders"
|
||||||
export * from "./confirm-return-request"
|
export * from "./confirm-return-request"
|
||||||
export * from "./create-complete-return"
|
export * from "./create-complete-return"
|
||||||
|
export * from "./create-exchange-return-shipping-method"
|
||||||
export * from "./create-fulfillment"
|
export * from "./create-fulfillment"
|
||||||
export * from "./create-order-change"
|
export * from "./create-order-change"
|
||||||
export * from "./create-order-change-actions"
|
export * from "./create-order-change-actions"
|
||||||
|
|||||||
@@ -1161,6 +1161,8 @@ export interface OrderExchangeDTO
|
|||||||
no_notification?: boolean
|
no_notification?: boolean
|
||||||
difference_due?: BigNumberValue
|
difference_due?: BigNumberValue
|
||||||
return?: ReturnDTO
|
return?: ReturnDTO
|
||||||
|
return_id?: string
|
||||||
|
order_id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PaymentStatus =
|
export type PaymentStatus =
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
export * from "./events"
|
export * from "./events"
|
||||||
|
export * from "./order-change"
|
||||||
export * from "./order-change-action"
|
export * from "./order-change-action"
|
||||||
export * from "./status"
|
export * from "./status"
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
export enum OrderChangeStatus {
|
||||||
|
/**
|
||||||
|
* The order change is confirmed.
|
||||||
|
*/
|
||||||
|
CONFIRMED = "confirmed",
|
||||||
|
/**
|
||||||
|
* The order change is declined.
|
||||||
|
*/
|
||||||
|
DECLINED = "declined",
|
||||||
|
/**
|
||||||
|
* The order change is requested.
|
||||||
|
*/
|
||||||
|
REQUESTED = "requested",
|
||||||
|
/**
|
||||||
|
* The order change is pending.
|
||||||
|
*/
|
||||||
|
PENDING = "pending",
|
||||||
|
/**
|
||||||
|
* The order change is canceled.
|
||||||
|
*/
|
||||||
|
CANCELED = "canceled",
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum OrderChangeType {
|
||||||
|
RETURN = "return",
|
||||||
|
EXCHANGE = "exchange",
|
||||||
|
CLAIM = "claim",
|
||||||
|
EDIT = "edit",
|
||||||
|
}
|
||||||
@@ -1,8 +1,4 @@
|
|||||||
import { createReturnShippingMethodWorkflow } from "@medusajs/core-flows"
|
import { createReturnShippingMethodWorkflow } from "@medusajs/core-flows"
|
||||||
import {
|
|
||||||
ContainerRegistrationKeys,
|
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
@@ -15,26 +11,13 @@ export const POST = async (
|
|||||||
) => {
|
) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const { result: orderPreview } = await createReturnShippingMethodWorkflow(
|
||||||
|
req.scope
|
||||||
await createReturnShippingMethodWorkflow(req.scope).run({
|
).run({
|
||||||
input: { ...req.validatedBody, return_id: id },
|
input: { ...req.validatedBody, return_id: id },
|
||||||
})
|
})
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
|
||||||
entryPoint: "return",
|
|
||||||
variables: {
|
|
||||||
id,
|
|
||||||
filters: {
|
|
||||||
...req.filterableFields,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
fields: req.remoteQueryConfig.fields,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [orderReturn] = await remoteQuery(queryObject)
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
return: orderReturn,
|
order: orderPreview,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { Migration } from "@mikro-orm/migrations"
|
||||||
|
|
||||||
|
export class Migration20240715102100 extends Migration {
|
||||||
|
async up(): Promise<void> {
|
||||||
|
const sql = `
|
||||||
|
ALTER TABLE "return"
|
||||||
|
ADD COLUMN if NOT exists "location_id" TEXT NULL;
|
||||||
|
`
|
||||||
|
|
||||||
|
this.addSql(sql)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ import { DAL } from "@medusajs/types"
|
|||||||
import {
|
import {
|
||||||
createPsqlIndexStatementHelper,
|
createPsqlIndexStatementHelper,
|
||||||
generateEntityId,
|
generateEntityId,
|
||||||
|
OrderChangeStatus,
|
||||||
|
OrderChangeType,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
@@ -10,14 +12,14 @@ import {
|
|||||||
Entity,
|
Entity,
|
||||||
Enum,
|
Enum,
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
OnInit,
|
|
||||||
OneToMany,
|
OneToMany,
|
||||||
|
OnInit,
|
||||||
OptionalProps,
|
OptionalProps,
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
Property,
|
Property,
|
||||||
Rel,
|
Rel,
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
import { OrderChangeStatus, OrderChangeType } from "@types"
|
import {} from "@types"
|
||||||
import OrderClaim from "./claim"
|
import OrderClaim from "./claim"
|
||||||
import OrderExchange from "./exchange"
|
import OrderExchange from "./exchange"
|
||||||
import Order from "./order"
|
import Order from "./order"
|
||||||
|
|||||||
@@ -116,6 +116,9 @@ export default class Return {
|
|||||||
@Enum({ items: () => ReturnStatus, default: ReturnStatus.REQUESTED })
|
@Enum({ items: () => ReturnStatus, default: ReturnStatus.REQUESTED })
|
||||||
status: ReturnStatus = ReturnStatus.REQUESTED
|
status: ReturnStatus = ReturnStatus.REQUESTED
|
||||||
|
|
||||||
|
@Property({ columnType: "text", nullable: true })
|
||||||
|
location_id: string | null = null
|
||||||
|
|
||||||
@Property({ columnType: "boolean", nullable: true })
|
@Property({ columnType: "boolean", nullable: true })
|
||||||
no_notification: boolean | null = null
|
no_notification: boolean | null = null
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ describe("Order Exchange - Actions", function () {
|
|||||||
shipping_methods: [
|
shipping_methods: [
|
||||||
{
|
{
|
||||||
id: "ship_123",
|
id: "ship_123",
|
||||||
price: 0,
|
amount: 0,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
total: 270,
|
total: 270,
|
||||||
@@ -188,11 +188,11 @@ describe("Order Exchange - Actions", function () {
|
|||||||
expect(changes.order.shipping_methods).toEqual([
|
expect(changes.order.shipping_methods).toEqual([
|
||||||
{
|
{
|
||||||
id: "ship_123",
|
id: "ship_123",
|
||||||
price: 0,
|
amount: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "shipping_345",
|
id: "shipping_345",
|
||||||
price: 5,
|
amount: 5,
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
action: "SHIPPING_ADD",
|
action: "SHIPPING_ADD",
|
||||||
@@ -203,7 +203,7 @@ describe("Order Exchange - Actions", function () {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "return_shipping_345",
|
id: "return_shipping_345",
|
||||||
price: 7.5,
|
amount: 7.5,
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
action: "SHIPPING_ADD",
|
action: "SHIPPING_ADD",
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import {
|
|||||||
import {
|
import {
|
||||||
ChangeActionType,
|
ChangeActionType,
|
||||||
ClaimType,
|
ClaimType,
|
||||||
|
OrderChangeType,
|
||||||
ReturnStatus,
|
ReturnStatus,
|
||||||
getShippingMethodsTotals,
|
getShippingMethodsTotals,
|
||||||
isString,
|
isString,
|
||||||
promiseAll,
|
promiseAll,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { ClaimItem, OrderClaim, Return, ReturnItem } from "@models"
|
import { ClaimItem, OrderClaim, Return, ReturnItem } from "@models"
|
||||||
import { OrderChangeType } from "@types"
|
|
||||||
|
|
||||||
function createClaimAndReturnEntities(em, data, order) {
|
function createClaimAndReturnEntities(em, data, order) {
|
||||||
const claimReference = em.create(OrderClaim, {
|
const claimReference = em.create(OrderClaim, {
|
||||||
|
|||||||
@@ -5,13 +5,13 @@ import {
|
|||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ChangeActionType,
|
ChangeActionType,
|
||||||
|
OrderChangeType,
|
||||||
ReturnStatus,
|
ReturnStatus,
|
||||||
getShippingMethodsTotals,
|
getShippingMethodsTotals,
|
||||||
isString,
|
isString,
|
||||||
promiseAll,
|
promiseAll,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { ExchangeItem, OrderExchange, Return, ReturnItem } from "@models"
|
import { ExchangeItem, OrderExchange, Return, ReturnItem } from "@models"
|
||||||
import { OrderChangeType } from "@types"
|
|
||||||
|
|
||||||
function createExchangeAndReturnEntities(em, data, order) {
|
function createExchangeAndReturnEntities(em, data, order) {
|
||||||
const exchangeReference = em.create(OrderExchange, {
|
const exchangeReference = em.create(OrderExchange, {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ChangeActionType,
|
ChangeActionType,
|
||||||
|
OrderChangeType,
|
||||||
ReturnStatus,
|
ReturnStatus,
|
||||||
getShippingMethodsTotals,
|
getShippingMethodsTotals,
|
||||||
isDefined,
|
isDefined,
|
||||||
@@ -12,7 +13,6 @@ import {
|
|||||||
promiseAll,
|
promiseAll,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { Return, ReturnItem } from "@models"
|
import { Return, ReturnItem } from "@models"
|
||||||
import { OrderChangeType } from "@types"
|
|
||||||
|
|
||||||
function createReturnReference(em, data, order) {
|
function createReturnReference(em, data, order) {
|
||||||
return em.create(Return, {
|
return em.create(Return, {
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ import { Context, OrderTypes } from "@medusajs/types"
|
|||||||
import {
|
import {
|
||||||
ChangeActionType,
|
ChangeActionType,
|
||||||
MathBN,
|
MathBN,
|
||||||
|
OrderChangeType,
|
||||||
ReturnStatus,
|
ReturnStatus,
|
||||||
promiseAll,
|
promiseAll,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { OrderChangeType } from "@types"
|
|
||||||
|
|
||||||
function createReturnItems(data) {
|
function createReturnItems(data) {
|
||||||
return data.items.map((item) => ({
|
return data.items.map((item) => ({
|
||||||
|
|||||||
@@ -21,12 +21,14 @@ import {
|
|||||||
deduplicate,
|
deduplicate,
|
||||||
InjectManager,
|
InjectManager,
|
||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
|
isDefined,
|
||||||
isObject,
|
isObject,
|
||||||
isString,
|
isString,
|
||||||
MathBN,
|
MathBN,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
|
OrderChangeStatus,
|
||||||
OrderStatus,
|
OrderStatus,
|
||||||
promiseAll,
|
promiseAll,
|
||||||
transformPropertiesToBigNumber,
|
transformPropertiesToBigNumber,
|
||||||
@@ -59,7 +61,6 @@ import {
|
|||||||
CreateOrderLineItemTaxLineDTO,
|
CreateOrderLineItemTaxLineDTO,
|
||||||
CreateOrderShippingMethodDTO,
|
CreateOrderShippingMethodDTO,
|
||||||
CreateOrderShippingMethodTaxLineDTO,
|
CreateOrderShippingMethodTaxLineDTO,
|
||||||
OrderChangeStatus,
|
|
||||||
UpdateOrderItemDTO,
|
UpdateOrderItemDTO,
|
||||||
UpdateOrderLineItemDTO,
|
UpdateOrderLineItemDTO,
|
||||||
UpdateOrderLineItemTaxLineDTO,
|
UpdateOrderLineItemTaxLineDTO,
|
||||||
@@ -1193,7 +1194,7 @@ export default class OrderModuleService<
|
|||||||
return_id: dt.return_id,
|
return_id: dt.return_id,
|
||||||
claim_id: dt.claim_id,
|
claim_id: dt.claim_id,
|
||||||
exchange_id: dt.exchange_id,
|
exchange_id: dt.exchange_id,
|
||||||
version: mapOrderVersion[dt.order_id],
|
version: dt.version ?? mapOrderVersion[dt.order_id],
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1979,13 +1980,20 @@ export default class OrderModuleService<
|
|||||||
const calculated = calculatedOrders[order.id]
|
const calculated = calculatedOrders[order.id]
|
||||||
|
|
||||||
const addedItems = {}
|
const addedItems = {}
|
||||||
|
const addedShippingMethods = {}
|
||||||
for (const item of calculated.order.items) {
|
for (const item of calculated.order.items) {
|
||||||
const isExistingItem = item.id === item.detail?.item_id
|
const isExistingItem = item.id === item.detail?.item_id
|
||||||
|
|
||||||
if (!isExistingItem) {
|
if (!isExistingItem) {
|
||||||
addedItems[item.id] = item
|
addedItems[item.id] = item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const sm of calculated.order.shipping_methods) {
|
||||||
|
if (!isDefined(sm.shipping_option_id)) {
|
||||||
|
addedShippingMethods[sm.id] = sm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (Object.keys(addedItems).length > 0) {
|
if (Object.keys(addedItems).length > 0) {
|
||||||
const addedItemDetails = await this.listLineItems(
|
const addedItemDetails = await this.listLineItems(
|
||||||
{ id: Object.keys(addedItems) },
|
{ id: Object.keys(addedItems) },
|
||||||
@@ -1996,22 +2004,61 @@ export default class OrderModuleService<
|
|||||||
)
|
)
|
||||||
|
|
||||||
calculated.order.items.forEach((item, idx) => {
|
calculated.order.items.forEach((item, idx) => {
|
||||||
if (addedItems[item.id]) {
|
if (!addedItems[item.id]) {
|
||||||
const lineItem = addedItemDetails.find((d) => d.id === item.id) as any
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const actions = item.actions
|
const lineItem = addedItemDetails.find((d) => d.id === item.id) as any
|
||||||
delete item.actions
|
|
||||||
|
|
||||||
const newItem = itemsToUpsert.find((d) => d.item_id === item.id)!
|
const actions = item.actions
|
||||||
calculated.order.items[idx] = {
|
delete item.actions
|
||||||
...lineItem,
|
|
||||||
actions,
|
const newItem = itemsToUpsert.find((d) => d.item_id === item.id)!
|
||||||
quantity: newItem.quantity,
|
calculated.order.items[idx] = {
|
||||||
detail: {
|
...lineItem,
|
||||||
...newItem,
|
actions,
|
||||||
...item,
|
quantity: newItem.quantity,
|
||||||
},
|
detail: {
|
||||||
}
|
...newItem,
|
||||||
|
...item,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(addedShippingMethods).length > 0) {
|
||||||
|
const addedShippingDetails = await this.listShippingMethods(
|
||||||
|
{ id: Object.keys(addedShippingMethods) },
|
||||||
|
{
|
||||||
|
relations: ["adjustments", "tax_lines"],
|
||||||
|
},
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
calculated.order.shipping_methods.forEach((sm, idx) => {
|
||||||
|
if (!addedShippingMethods[sm.id]) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const shippingMethod = addedShippingDetails.find(
|
||||||
|
(d) => d.id === sm.id
|
||||||
|
) as any
|
||||||
|
|
||||||
|
const actions = sm.actions
|
||||||
|
delete sm.actions
|
||||||
|
|
||||||
|
const newItem = shippingMethodsToUpsert.find((d) => d.id === sm.id)!
|
||||||
|
|
||||||
|
sm.shipping_method_id = sm.id
|
||||||
|
delete sm.id
|
||||||
|
|
||||||
|
calculated.order.shipping_methods[idx] = {
|
||||||
|
...shippingMethod,
|
||||||
|
actions,
|
||||||
|
detail: {
|
||||||
|
...sm,
|
||||||
|
...newItem,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -2579,7 +2626,9 @@ export default class OrderModuleService<
|
|||||||
shippingMethodsToUpsert,
|
shippingMethodsToUpsert,
|
||||||
summariesToUpsert,
|
summariesToUpsert,
|
||||||
orderToUpdate,
|
orderToUpdate,
|
||||||
} = applyChangesToOrder(orders, actionsMap)
|
} = applyChangesToOrder(orders, actionsMap, {
|
||||||
|
addActionReferenceToObject: true,
|
||||||
|
})
|
||||||
|
|
||||||
await promiseAll([
|
await promiseAll([
|
||||||
orderToUpdate.length
|
orderToUpdate.length
|
||||||
|
|||||||
@@ -1,34 +1,5 @@
|
|||||||
import { OrderTypes } from "@medusajs/types"
|
import { OrderTypes } from "@medusajs/types"
|
||||||
|
import { OrderChangeType } from "@medusajs/utils"
|
||||||
export enum OrderChangeStatus {
|
|
||||||
/**
|
|
||||||
* The order change is confirmed.
|
|
||||||
*/
|
|
||||||
CONFIRMED = "confirmed",
|
|
||||||
/**
|
|
||||||
* The order change is declined.
|
|
||||||
*/
|
|
||||||
DECLINED = "declined",
|
|
||||||
/**
|
|
||||||
* The order change is requested.
|
|
||||||
*/
|
|
||||||
REQUESTED = "requested",
|
|
||||||
/**
|
|
||||||
* The order change is pending.
|
|
||||||
*/
|
|
||||||
PENDING = "pending",
|
|
||||||
/**
|
|
||||||
* The order change is canceled.
|
|
||||||
*/
|
|
||||||
CANCELED = "canceled",
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum OrderChangeType {
|
|
||||||
RETURN = "return",
|
|
||||||
EXCHANGE = "exchange",
|
|
||||||
CLAIM = "claim",
|
|
||||||
EDIT = "edit",
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface CreateOrderChangeDTO extends OrderTypes.CreateOrderChangeDTO {
|
export interface CreateOrderChangeDTO extends OrderTypes.CreateOrderChangeDTO {
|
||||||
change_type?: OrderChangeType
|
change_type?: OrderChangeType
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ export type VirtualOrder = {
|
|||||||
exchange_id?: string
|
exchange_id?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
price: BigNumberInput
|
amount: BigNumberInput
|
||||||
}[]
|
}[]
|
||||||
|
|
||||||
total: BigNumberInput
|
total: BigNumberInput
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
|
|||||||
return_id: action.return_id,
|
return_id: action.return_id,
|
||||||
claim_id: action.claim_id,
|
claim_id: action.claim_id,
|
||||||
exchange_id: action.exchange_id,
|
exchange_id: action.exchange_id,
|
||||||
price: action.amount as number,
|
amount: action.amount as number,
|
||||||
}
|
}
|
||||||
shipping.push(existing)
|
shipping.push(existing)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {
|
|||||||
return_id: action.return_id,
|
return_id: action.return_id,
|
||||||
claim_id: action.claim_id,
|
claim_id: action.claim_id,
|
||||||
exchange_id: action.exchange_id,
|
exchange_id: action.exchange_id,
|
||||||
price: action.amount as number,
|
amount: action.amount as number,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { OrderChangeActionDTO } from "@medusajs/types"
|
import { OrderChangeActionDTO } from "@medusajs/types"
|
||||||
import { createRawPropertiesFromBigNumber } from "@medusajs/utils"
|
import {
|
||||||
|
ChangeActionType,
|
||||||
|
createRawPropertiesFromBigNumber,
|
||||||
|
isDefined,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import { OrderItem, OrderShippingMethod } from "@models"
|
import { OrderItem, OrderShippingMethod } from "@models"
|
||||||
import { calculateOrderChange } from "./calculate-order-change"
|
import { calculateOrderChange } from "./calculate-order-change"
|
||||||
|
|
||||||
@@ -35,7 +39,7 @@ export function applyChangesToOrder(
|
|||||||
|
|
||||||
calculatedOrders[order.id] = calculated
|
calculatedOrders[order.id] = calculated
|
||||||
|
|
||||||
const version = actionsMap[order.id][0].version ?? 1
|
const version = actionsMap[order.id]?.[0]?.version ?? order.version
|
||||||
|
|
||||||
for (const item of calculated.order.items) {
|
for (const item of calculated.order.items) {
|
||||||
const isExistingItem = item.id === item.detail?.item_id
|
const isExistingItem = item.id === item.detail?.item_id
|
||||||
@@ -68,17 +72,36 @@ export function applyChangesToOrder(
|
|||||||
|
|
||||||
if (version > order.version) {
|
if (version > order.version) {
|
||||||
for (const shippingMethod of calculated.order.shipping_methods ?? []) {
|
for (const shippingMethod of calculated.order.shipping_methods ?? []) {
|
||||||
if (!shippingMethod) {
|
const shippingMethod_ = shippingMethod as any
|
||||||
|
const isNewShippingMethod = !isDefined(shippingMethod_?.detail)
|
||||||
|
if (!shippingMethod_) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const sm = {
|
let associatedMethodId
|
||||||
...((shippingMethod as any).detail ?? shippingMethod),
|
let hasShippingMethod = false
|
||||||
version,
|
if (isNewShippingMethod) {
|
||||||
|
associatedMethodId = shippingMethod_.actions?.find((sm) => {
|
||||||
|
return (
|
||||||
|
sm.action === ChangeActionType.SHIPPING_ADD && sm.reference_id
|
||||||
|
)
|
||||||
|
})
|
||||||
|
hasShippingMethod = !!associatedMethodId
|
||||||
|
} else {
|
||||||
|
associatedMethodId = shippingMethod_?.detail?.shipping_method_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sm = {
|
||||||
|
...(isNewShippingMethod ? shippingMethod_ : shippingMethod_.detail),
|
||||||
|
version,
|
||||||
|
shipping_method_id: associatedMethodId,
|
||||||
|
} as any
|
||||||
|
|
||||||
delete sm.id
|
delete sm.id
|
||||||
shippingMethodsToUpsert.push(sm)
|
|
||||||
|
if (!hasShippingMethod) {
|
||||||
|
shippingMethodsToUpsert.push(sm)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
orderToUpdate.push({
|
orderToUpdate.push({
|
||||||
|
|||||||
Reference in New Issue
Block a user