@mercurjs/core/workflows and run them with the Medusa container.
completeCartWithSplitOrdersWorkflow
Completes a cart by creating one order per seller under a new order group. Workflow idcomplete-cart-with-split-orders. Mercur’s cart completion route and processPaymentWorkflow both run it.
It returns
{ order_group_id: string }. If a group with the same cart_id already exists, the workflow skips the split and returns that group’s id.
The workflow stores its execution (store: true) and keeps it for 3 days. It holds a lock on the cart id with a 30-second wait timeout and a 2-minute time to live, so concurrent completions of one cart run one at a time.
Hooks
SetOrderStatusHookResult is exported with the workflow. status_by_seller_id is keyed by seller id and takes precedence over status.
Steps
The workflow runs these steps in order. Steps 3 to 6 run only when the cart has noorder_cart link yet, so a cart counts as completed once its orders are linked to it:
acquireLockStepon the cart id, then queries for the cart’sorder_cartlinks, an existing group, and the cart.validateCartItemsStep,validateCartPaymentsStepandcompensatePaymentIfNeededStep, then thevalidateandsetOrderStatushooks.validateSellerCartItemsStepandvalidateSellerCartShippingStep.createOrderGroupStep, thencreateOrdersStep.createRemoteLinkStep,mirrorLineItemOfferLinksToOrderStep,updateCartsStepto setcompleted_at,reserveInventoryStep,registerUsageStep, and twoemitEventStepcalls.authorizePaymentSessionStep, thensplitCapturedCartPaymentWorkflowandrefreshOrderCommissionLinesWorkflowas steps.releaseLockStepon the cart id.
Events
getOrderGroupDetailWorkflow
Retrieves one order group with its child orders and setspayment_status and fulfillment_status on each order. Workflow id get-order-group-detail. Its input type is GetOrderGroupDetailWorkflowInput.
It returns the group. The workflow always adds
id, orders.id, orders.status, orders.version, orders.currency_code, and orders.items.* to your fields, plus the payment collection and fulfillment fields it needs for the statuses. It throws NOT_FOUND when no group has the id.
The workflow removes some data it loaded for itself before it returns:
orders.payment_collections: kept only when a requested field containspayment_collectionsorders.fulfillments: kept only when a requested field containsfulfillmentsorders.cart: kept only when a requested field starts withorders.cart
getOrderGroupsListWorkflow
Lists order groups with their child orders and derived statuses. Workflow idget-order-groups-list. Its input type is GetOrderGroupsListWorkflowInput.
It returns
{ rows, metadata }, where metadata holds count, skip, and take. It adds the same child order fields as the detail workflow and removes the same data, and adds orders.seller.id when you pass sellerId.
sellerId trims orders but doesn’t filter groups. Pass variables.seller_id as well to return only groups where the seller has an order. The filter keys are listed in Filters and sorting.
createOrderGroupStep
Creates oneOrderGroup record, or returns the cart’s existing group when there is one. Step id create-order-group.
It returns the group. On rollback, it deletes the group with
deleteOrderGroups only if the step created it. Use it in your own workflow only when you also create and link the child orders yourself.