chore(core-flows): use create order step on complete cart (#8397)
This commit is contained in:
1 parent
dbfc145ab3
commit
5cae91040e
3 files changed
+37
-61
No files matched your search
@@ -1,57 +0,0 @@
|
|||||||
import { CartWorkflowDTO } from "@medusajs/types"
|
|
||||||
import { OrderStatus } from "@medusajs/utils"
|
|
||||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
|
||||||
import { createOrdersWorkflow } from "../../../order/workflows/create-orders"
|
|
||||||
|
|
||||||
interface StepInput {
|
|
||||||
cart: CartWorkflowDTO
|
|
||||||
}
|
|
||||||
|
|
||||||
export const createOrderFromCartStepId = "create-order-from-cart"
|
|
||||||
export const createOrderFromCartStep = createStep(
|
|
||||||
createOrderFromCartStepId,
|
|
||||||
async (input: StepInput, { container }) => {
|
|
||||||
const { cart } = input
|
|
||||||
|
|
||||||
const itemAdjustments = (cart.items || [])
|
|
||||||
?.map((item) => item.adjustments || [])
|
|
||||||
.flat(1)
|
|
||||||
const shippingAdjustments = (cart.shipping_methods || [])
|
|
||||||
?.map((sm) => sm.adjustments || [])
|
|
||||||
.flat(1)
|
|
||||||
|
|
||||||
const promoCodes = [...itemAdjustments, ...shippingAdjustments]
|
|
||||||
.map((adjustment) => adjustment.code)
|
|
||||||
.filter((code) => Boolean) as string[]
|
|
||||||
|
|
||||||
const { transaction, result } = await createOrdersWorkflow(container).run({
|
|
||||||
input: {
|
|
||||||
region_id: cart.region?.id,
|
|
||||||
customer_id: cart.customer?.id,
|
|
||||||
sales_channel_id: cart.sales_channel_id,
|
|
||||||
status: OrderStatus.PENDING,
|
|
||||||
email: cart.email,
|
|
||||||
currency_code: cart.currency_code,
|
|
||||||
shipping_address: cart.shipping_address,
|
|
||||||
billing_address: cart.billing_address,
|
|
||||||
// TODO: This should be handle correctly
|
|
||||||
no_notification: false,
|
|
||||||
items: cart.items,
|
|
||||||
shipping_methods: cart.shipping_methods,
|
|
||||||
metadata: cart.metadata,
|
|
||||||
promo_codes: promoCodes,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new StepResponse(result, { transaction })
|
|
||||||
},
|
|
||||||
async (flow, { container }) => {
|
|
||||||
if (!flow) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
await createOrdersWorkflow(container).cancel({
|
|
||||||
transaction: flow.transaction,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
)
|
|
||||||
@@ -3,7 +3,6 @@ export * from "./confirm-inventory"
|
|||||||
export * from "./create-carts"
|
export * from "./create-carts"
|
||||||
export * from "./create-line-item-adjustments"
|
export * from "./create-line-item-adjustments"
|
||||||
export * from "./create-line-items"
|
export * from "./create-line-items"
|
||||||
export * from "./create-order-from-cart"
|
|
||||||
export * from "./create-shipping-method-adjustments"
|
export * from "./create-shipping-method-adjustments"
|
||||||
export * from "./find-one-or-any-region"
|
export * from "./find-one-or-any-region"
|
||||||
export * from "./find-or-create-customer"
|
export * from "./find-or-create-customer"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { OrderDTO } from "@medusajs/types"
|
import { OrderDTO } from "@medusajs/types"
|
||||||
import { Modules, OrderEvents } from "@medusajs/utils"
|
import { Modules, OrderEvents, OrderStatus } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
@@ -12,8 +12,9 @@ import {
|
|||||||
emitEventStep,
|
emitEventStep,
|
||||||
useRemoteQueryStep,
|
useRemoteQueryStep,
|
||||||
} from "../../../common"
|
} from "../../../common"
|
||||||
|
import { createOrdersStep } from "../../../order/steps/create-orders"
|
||||||
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
|
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
|
||||||
import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps"
|
import { validateCartPaymentsStep } from "../steps"
|
||||||
import { reserveInventoryStep } from "../steps/reserve-inventory"
|
import { reserveInventoryStep } from "../steps/reserve-inventory"
|
||||||
import { completeCartFields } from "../utils/fields"
|
import { completeCartFields } from "../utils/fields"
|
||||||
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
||||||
@@ -78,7 +79,40 @@ export const completeCartWorkflow = createWorkflow(
|
|||||||
}).config({ name: "final-cart" })
|
}).config({ name: "final-cart" })
|
||||||
)
|
)
|
||||||
|
|
||||||
const order = createOrderFromCartStep({ cart: finalCart })
|
const cartToOrder = transform({ input, cart }, ({ cart }) => {
|
||||||
|
const itemAdjustments = (cart.items || [])
|
||||||
|
?.map((item) => item.adjustments || [])
|
||||||
|
.flat(1)
|
||||||
|
const shippingAdjustments = (cart.shipping_methods || [])
|
||||||
|
?.map((sm) => sm.adjustments || [])
|
||||||
|
.flat(1)
|
||||||
|
|
||||||
|
const promoCodes = [...itemAdjustments, ...shippingAdjustments]
|
||||||
|
.map((adjustment) => adjustment.code)
|
||||||
|
.filter((code) => Boolean) as string[]
|
||||||
|
|
||||||
|
return {
|
||||||
|
region_id: cart.region?.id,
|
||||||
|
customer_id: cart.customer?.id,
|
||||||
|
sales_channel_id: cart.sales_channel_id,
|
||||||
|
status: OrderStatus.PENDING,
|
||||||
|
email: cart.email,
|
||||||
|
currency_code: cart.currency_code,
|
||||||
|
shipping_address: cart.shipping_address,
|
||||||
|
billing_address: cart.billing_address,
|
||||||
|
no_notification: false,
|
||||||
|
items: cart.items,
|
||||||
|
shipping_methods: cart.shipping_methods,
|
||||||
|
metadata: cart.metadata,
|
||||||
|
promo_codes: promoCodes,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const createdOrders = createOrdersStep([cartToOrder])
|
||||||
|
const order = transform(
|
||||||
|
{ createdOrders },
|
||||||
|
({ createdOrders }) => createdOrders[0]
|
||||||
|
)
|
||||||
|
|
||||||
createRemoteLinkStep([
|
createRemoteLinkStep([
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user