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:
@@ -1,13 +1,12 @@
|
||||
import { OrderChangeDTO, OrderDTO, ReturnDTO } from "@medusajs/types"
|
||||
import { ChangeActionType } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
WorkflowData,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { previewOrderChangeStep } from "../steps"
|
||||
import { confirmOrderChanges } from "../steps/confirm-order-changes"
|
||||
import { createReturnItems } from "../steps/create-return-items"
|
||||
import {
|
||||
@@ -39,7 +38,7 @@ const validationStep = createStep(
|
||||
export const confirmReturnRequestWorkflowId = "confirm-return-request"
|
||||
export const confirmReturnRequestWorkflow = createWorkflow(
|
||||
confirmReturnRequestWorkflowId,
|
||||
function (input: WorkflowInput): WorkflowData<OrderDTO> {
|
||||
function (input: WorkflowInput): WorkflowData<void> {
|
||||
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
||||
entry_point: "return",
|
||||
fields: ["id", "status", "order_id"],
|
||||
@@ -87,7 +86,5 @@ export const confirmReturnRequestWorkflow = createWorkflow(
|
||||
createReturnItems({ returnId: orderReturn.id, changes: returnItemActions })
|
||||
|
||||
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
|
||||
|
||||
return previewOrderChangeStep(order.id)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
ReturnDTO,
|
||||
} from "@medusajs/types"
|
||||
import { ChangeActionType } from "@medusajs/utils"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
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 { previewOrderChangeStep } from "../steps/preview-order-change"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
@@ -77,10 +78,25 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
||||
},
|
||||
}).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(
|
||||
{ orderReturn, shippingOptions },
|
||||
{ orderReturn, shippingOptions, orderChange },
|
||||
(data) => {
|
||||
const option = data.shippingOptions[0]
|
||||
const orderChange = data.orderChange
|
||||
|
||||
return {
|
||||
shipping_option_id: option.id,
|
||||
@@ -89,6 +105,7 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
||||
!!option.calculated_price.is_calculated_price_tax_inclusive,
|
||||
data: option.data ?? {},
|
||||
name: option.name,
|
||||
version: orderChange.version,
|
||||
order_id: data.orderReturn.order_id,
|
||||
return_id: data.orderReturn.id,
|
||||
}
|
||||
@@ -99,40 +116,42 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
||||
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(
|
||||
{
|
||||
orderId: order.id,
|
||||
returnId: orderReturn.id,
|
||||
shippingOption: shippingOptions[0],
|
||||
methodId: createdMethods[0].id,
|
||||
order,
|
||||
orderReturn,
|
||||
shippingOptions,
|
||||
createdMethods,
|
||||
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 =
|
||||
customPrice ?? shippingOption.calculated_price.calculated_amount
|
||||
|
||||
return {
|
||||
action: ChangeActionType.SHIPPING_ADD,
|
||||
reference: "order_shipping_method",
|
||||
reference_id: methodId,
|
||||
reference_id: createdMethod.id,
|
||||
order_change_id: orderChange.id,
|
||||
amount: methodPrice,
|
||||
order_id: orderId,
|
||||
return_id: returnId,
|
||||
order_id: order.id,
|
||||
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 "./confirm-return-request"
|
||||
export * from "./create-complete-return"
|
||||
export * from "./create-exchange-return-shipping-method"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-order-change"
|
||||
export * from "./create-order-change-actions"
|
||||
|
||||
Reference in New Issue
Block a user