feat: Add return shipping method workflow (#8106)

This commit is contained in:
Oli Juhl
2024-07-13 13:09:07 +02:00
committed by GitHub
parent 3d7d629e75
commit 7e82f981f9
5 changed files with 302 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
import {
CreateOrderShippingMethodDTO,
IOrderModuleService,
} from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
interface StepInput {
shipping_methods: CreateOrderShippingMethodDTO[]
}
export const createOrderShippingMethods = createStep(
"create-order-shipping-methods",
async (input: StepInput, { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
const created = await service.createShippingMethods(input.shipping_methods)
return new StepResponse(
created,
created.map((c) => c.id)
)
},
async (createdMethodIds, { container }) => {
if (!createdMethodIds) {
return
}
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await service.deleteShippingMethods(createdMethodIds)
}
)

View File

@@ -0,0 +1,138 @@
import {
BigNumberInput,
OrderChangeDTO,
OrderDTO,
ReturnDTO,
} 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 {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive
} from "../utils/order-validation"
const validationStep = createStep(
"validate-create-return-shipping-method",
async function ({
order,
orderChange,
orderReturn,
}: {
order: OrderDTO
orderReturn: ReturnDTO
orderChange: OrderChangeDTO
}) {
throwIfIsCancelled(order, "Order")
throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange })
}
)
export const createReturnShippingMethodWorkflowId =
"create-return-shipping-method"
export const createReturnShippingMethodWorkflow = createWorkflow(
createReturnShippingMethodWorkflowId,
function (input: {
returnId: string
shippingOptionId: string
customShippingPrice?: BigNumberInput
}): WorkflowData {
const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "return",
fields: ["id", "status", "order_id"],
variables: { id: input.returnId },
list: false,
throw_if_key_not_found: true,
})
const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: ["id", "status", "currency_code"],
variables: { id: orderReturn.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(
{ orderReturn, shippingOptions },
(data) => {
const option = data.shippingOptions[0]
return {
shipping_option_id: option.id,
amount: 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.orderReturn.order_id,
return_id: data.orderReturn.id,
}
}
)
const createdMethods = createOrderShippingMethods({
shipping_methods: [shippingMethodInput],
})
const orderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: ["id", "status"],
variables: { order_id: orderReturn.order_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,
customPrice: input.customShippingPrice,
},
({ shippingOption, 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,
},
}
}
)
return createOrderChangeActionsStep([orderChangeActionInput])
}
)

View File

@@ -13,6 +13,7 @@ export * from "./create-fulfillment"
export * from "./create-order-change"
export * from "./create-order-change-actions"
export * from "./create-orders"
export * from "./create-return-shipping-method"
export * from "./create-shipment"
export * from "./decline-order-change"
export * from "./delete-order-change"
@@ -24,3 +25,4 @@ export * from "./receive-return"
export * from "./request-item-return"
export * from "./update-order-change-actions"
export * from "./update-tax-lines"