@medusajs/order 2.22.0-snapshot-20261020204851 → 2.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +74 -0
- package/README.md +94 -1
- package/package.json +8 -4
package/AGENTS.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @medusajs/order
|
|
2
|
+
|
|
3
|
+
Order Module (`Modules.ORDER`): orders, draft orders, returns, claims, exchanges and order edits. Every post-placement mutation is recorded as an order change with actions, and confirming a change produces a new order version.
|
|
4
|
+
|
|
5
|
+
## Using this package
|
|
6
|
+
|
|
7
|
+
- Registered by default; consumers import it as `@medusajs/medusa/order`. Resolve with `container.resolve(Modules.ORDER)` (`IOrderModuleService`).
|
|
8
|
+
- Almost all order operations should go through `@medusajs/core-flows` workflows (`createOrderWorkflow`, `beginReturnOrderWorkflow`, `beginOrderEditOrderWorkflow`, ...): they orchestrate payment, fulfillment, inventory and links around this module.
|
|
9
|
+
- Option `generateCustomDisplayId(order, sharedContext) => Promise<string | undefined>`: sets `custom_display_id` on `createOrders`.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
module.exports = defineConfig({
|
|
13
|
+
modules: [
|
|
14
|
+
{
|
|
15
|
+
key: Modules.ORDER,
|
|
16
|
+
options: {
|
|
17
|
+
generateCustomDisplayId: async (order, sharedContext) =>
|
|
18
|
+
`${order.email}-${Date.now()}`,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
})
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Docs: https://docs.medusajs.com/resources/commerce-modules/order.md, https://docs.medusajs.com/resources/commerce-modules/order/order-change.md, https://docs.medusajs.com/resources/commerce-modules/order/order-versioning.md, https://docs.medusajs.com/resources/commerce-modules/order/order-totals.md, https://docs.medusajs.com/resources/commerce-modules/order/return.md, https://docs.medusajs.com/resources/commerce-modules/order/claim.md, https://docs.medusajs.com/resources/commerce-modules/order/exchange.md, https://docs.medusajs.com/resources/commerce-modules/order/edit.md, https://docs.medusajs.com/resources/commerce-modules/order/links-to-other-modules.md
|
|
26
|
+
|
|
27
|
+
## The data model (non-obvious parts)
|
|
28
|
+
|
|
29
|
+
- `Order` (`order_`): `version` (starts at 1), `display_id` autoincrement, `status`, `is_draft_order`, addresses, plain-string `region_id` / `customer_id` / `sales_channel_id`.
|
|
30
|
+
- `OrderLineItem` (`ordli_`) holds the immutable product snapshot and price. `OrderItem` (`orditem_`, exposed as `item.detail`) is the per-version row: `quantity`, `fulfilled_quantity`, `shipped_quantity`, `delivered_quantity`, `return_requested/received/dismissed_quantity`, `written_off_quantity`, keyed by `(order_id, version)`.
|
|
31
|
+
- Versioned per order: `OrderItem`, `OrderShipping` (pivot to `OrderShippingMethod`), `OrderSummary` (`totals` JSON), `OrderTransaction`, `OrderCreditLine`, line item and shipping method adjustments. `src/utils/base-repository-find.ts` filters these to the order's current `version` by default (pass `version` in filters to read another one).
|
|
32
|
+
- `OrderChange` (`ordch_`): `version` = order version + 1, `change_type` (`OrderChangeType`), `status` (`OrderChangeStatus`: pending, requested, confirmed, declined, canceled), optional `return_id` / `claim_id` / `exchange_id`. Has many `OrderChangeAction` (`ordchact_`): `action` (`ChangeActionType`, e.g. `ITEM_ADD`, `RETURN_ITEM`, `FULFILL_ITEM`, `SHIPPING_ADD`, `CREDIT_LINE_ADD`, `PROMOTION_ADD`), `details`, `amount`, `ordering`, `applied`.
|
|
33
|
+
- `Return` (`return_`), `OrderClaim` (`claim_`, `type`: refund / replace), `OrderExchange` (`oexc_`) record `order_version` and own their items, shipping methods and transactions. `ReturnReason` is standalone (nested via `parent_return_reason`).
|
|
34
|
+
|
|
35
|
+
**Lifecycle of a change** (`src/services/order-module-service.ts`):
|
|
36
|
+
|
|
37
|
+
1. `createOrderChange({ order_id, change_type, actions? })` (throws if a pending/requested change already exists), then `addOrderAction(...)` to append actions.
|
|
38
|
+
2. `previewOrderChange(orderId)` applies pending actions in memory (`calculateOrderChange` in `src/utils/calculate-order-change.ts`) without persisting.
|
|
39
|
+
3. `confirmOrderChange(id)` marks it confirmed and calls `applyOrderChanges_` -> `applyChangesToOrder` (`src/utils/apply-order-changes.ts`), which upserts new versioned rows, a new `OrderSummary`, and bumps `Order.version`. `declineOrderChange` / `cancelOrderChange` only update status.
|
|
40
|
+
4. `applyPendingOrderActions(orderId)` applies unapplied actions of the current version; `registerOrderChange` records an already-applied `UPDATE_ORDER_PROPERTIES` change; `undoLastChange` / `revertLastVersion` roll back (require `version >= 2`).
|
|
41
|
+
|
|
42
|
+
Bundled shortcuts in `src/services/actions/` create and immediately confirm a change: `createReturn`, `receiveReturn`, `cancelReturn`, `createClaim`, `cancelClaim`, `createExchange`, `cancelExchange`, `registerFulfillment`, `cancelFulfillment`, `registerShipment`, `registerDelivery`. Also: `completeOrder`, `cancel`, `archive`, `addOrderTransactions`, `set/upsert` tax lines and adjustments for items and shipping methods.
|
|
43
|
+
|
|
44
|
+
**Events**: no custom event builders; `MedusaService` emits `order.<kebab-entity>.<action>` from ORM hooks. Business events (`order.placed`, ...) are emitted by core-flows workflows (`OrderWorkflowEvents`).
|
|
45
|
+
|
|
46
|
+
**Module links** (in `packages/modules/link-modules`): `order_cart`, `order_fulfillment`, `return_fulfillment`, `order_payment_collection` (cascade-deletes the payment collection), `order_promotion`. Read-only: customer, region, sales channel, shipping option, and product / variant via `items.product_id` / `items.variant_id`.
|
|
47
|
+
|
|
48
|
+
## Architecture
|
|
49
|
+
|
|
50
|
+
- Action types are registered with `OrderChangeProcessing.registerActionType(ChangeActionType.X, { operation, validate, isDeduction? })` in `src/utils/actions/*.ts`; they mutate a virtual order copy. A new action type needs a file there, an export in `src/utils/actions/index.ts`, and the enum value in `packages/core/utils/src/order/order-change-action.ts`.
|
|
51
|
+
- `src/utils/transform-order.ts` `formatOrder` reshapes versioned rows (`items[].detail`, `shipping_methods`) into the DTO and decorates totals.
|
|
52
|
+
- `src/repositories/{order,return,claim,exchange}.ts` use the version-aware `base-repository-find.ts`.
|
|
53
|
+
|
|
54
|
+
## Development
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
cd packages/modules/order
|
|
58
|
+
yarn build
|
|
59
|
+
yarn test # src/services/__tests__ (action calculations, transform-order)
|
|
60
|
+
yarn test:integration # needs Postgres
|
|
61
|
+
../../../node_modules/.bin/jest integration-tests/__tests__/order-edit.spec.ts
|
|
62
|
+
yarn migration:create # after any change in src/models
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
- Integration tests: `integration-tests/__tests__/` (`create-order`, `order-edit`, `order-return`, `order-claim`, `order-exchange`, `order-items-shipping`, `returns`, `delete-order`, `index`). End-to-end HTTP flows at the repo root: `integration-tests/http/__tests__/{order,order-changes,order-edits,returns,claims,exchanges,draft-order}`.
|
|
66
|
+
|
|
67
|
+
## Gotchas
|
|
68
|
+
|
|
69
|
+
- Never update `OrderItem` quantities or `Order.version` directly; add actions and confirm a change, or totals and history go out of sync.
|
|
70
|
+
- Totals are computed only if `select` includes a total field (or `*`); then the service force-loads `items`, `shipping_methods`, `credit_lines` with tax lines and adjustments (`shouldIncludeTotals`).
|
|
71
|
+
- `retrieveOrder` attaches the active (pending/requested) change as `order.order_change`.
|
|
72
|
+
- Confirm/cancel/decline throw "Order Change cannot be modified" unless the change is pending or requested.
|
|
73
|
+
- Adjustments are only created (never updated) when a change produces a new version; older versions keep their own rows.
|
|
74
|
+
- All amounts and quantities are `bigNumber`; use `MathBN`.
|
package/README.md
CHANGED
|
@@ -1 +1,94 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://www.medusajs.com">
|
|
3
|
+
<picture>
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/59018053/229103275-b5e482bb-4601-46e6-8142-244f531cebdb.svg">
|
|
5
|
+
<source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
|
|
6
|
+
<img alt="Medusa logo" src="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
|
|
7
|
+
</picture>
|
|
8
|
+
</a>
|
|
9
|
+
</p>
|
|
10
|
+
<h1 align="center">
|
|
11
|
+
@medusajs/order
|
|
12
|
+
</h1>
|
|
13
|
+
|
|
14
|
+
<h4 align="center">
|
|
15
|
+
<a href="https://docs.medusajs.com">Documentation</a> |
|
|
16
|
+
<a href="https://www.medusajs.com">Website</a>
|
|
17
|
+
</h4>
|
|
18
|
+
|
|
19
|
+
<p align="center">
|
|
20
|
+
Building blocks for digital commerce
|
|
21
|
+
</p>
|
|
22
|
+
<p align="center">
|
|
23
|
+
<a href="https://github.com/medusajs/medusa/blob/develop/LICENSE">
|
|
24
|
+
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="Medusa is released under the MIT license." />
|
|
25
|
+
</a>
|
|
26
|
+
<a href="https://github.com/medusajs/medusa/blob/develop/CONTRIBUTING.md">
|
|
27
|
+
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" alt="PRs welcome!" />
|
|
28
|
+
</a>
|
|
29
|
+
<a href="https://discord.gg/xpCwq3Kfn8">
|
|
30
|
+
<img src="https://img.shields.io/badge/chat-on%20discord-7289DA.svg" alt="Discord Chat" />
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://twitter.com/intent/follow?screen_name=medusajs">
|
|
33
|
+
<img src="https://img.shields.io/twitter/follow/medusajs.svg?label=Follow%20@medusajs" alt="Follow @medusajs" />
|
|
34
|
+
</a>
|
|
35
|
+
</p>
|
|
36
|
+
|
|
37
|
+
Order Module for Medusa: manages orders, line items, shipping methods, addresses, returns, claims, exchanges, order changes, transactions, and credit lines. Use it to store placed and draft orders and handle post-purchase flows such as returns, exchanges, and order edits. It ships with `@medusajs/medusa` and is registered by default, so Medusa applications don't install it directly.
|
|
38
|
+
|
|
39
|
+
## At a glance
|
|
40
|
+
|
|
41
|
+
| | |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| Package | `@medusajs/order` |
|
|
44
|
+
| Type | Commerce Module |
|
|
45
|
+
| Module key | `Modules.ORDER` (`"order"`) |
|
|
46
|
+
| Registered by default | Yes |
|
|
47
|
+
| Import from | `@medusajs/medusa/order` |
|
|
48
|
+
|
|
49
|
+
## Features
|
|
50
|
+
|
|
51
|
+
- Store orders with line items, shipping methods, addresses, adjustments, and tax lines.
|
|
52
|
+
- Handle returns, claims, and exchanges, with return reasons.
|
|
53
|
+
- Track order changes and versions for edits and other post-purchase actions.
|
|
54
|
+
- Record payment transactions and credit lines on an order.
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
No installation needed. The module is included in `@medusajs/medusa` and registered by default.
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
Resolve the module's service from the container, for example in a workflow step:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
|
|
66
|
+
import { Modules } from "@medusajs/framework/utils"
|
|
67
|
+
|
|
68
|
+
const createDraftOrderStep = createStep(
|
|
69
|
+
"create-order",
|
|
70
|
+
async ({}, { container }) => {
|
|
71
|
+
const orderModuleService = container.resolve(Modules.ORDER)
|
|
72
|
+
|
|
73
|
+
const draftOrder = await orderModuleService.createOrders({
|
|
74
|
+
currency_code: "usd",
|
|
75
|
+
items: [
|
|
76
|
+
{
|
|
77
|
+
title: "Shirt",
|
|
78
|
+
quantity: 1,
|
|
79
|
+
unit_price: 30,
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
status: "draft",
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return new StepResponse({ draftOrder }, draftOrder.id)
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Resources
|
|
91
|
+
|
|
92
|
+
- [Order Module](https://docs.medusajs.com/resources/commerce-modules/order)
|
|
93
|
+
- [Order Module workflows](https://docs.medusajs.com/resources/commerce-modules/order/workflows)
|
|
94
|
+
- [Modules](https://docs.medusajs.com/learn/fundamentals/modules)
|
package/package.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medusajs/order",
|
|
3
|
-
"version": "2.22.0
|
|
3
|
+
"version": "2.22.0",
|
|
4
4
|
"description": "Medusa Order module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist",
|
|
9
|
+
"AGENTS.md",
|
|
9
10
|
"!dist/**/__tests__",
|
|
10
11
|
"!dist/**/__mocks__",
|
|
11
12
|
"!dist/**/__fixtures__"
|
|
12
13
|
],
|
|
14
|
+
"llms": "https://docs.medusajs.com/llms.txt",
|
|
15
|
+
"llmsFull": "https://docs.medusajs.com/llms-full.txt",
|
|
16
|
+
"mcpServer": "https://docs.medusajs.com/mcp",
|
|
13
17
|
"engines": {
|
|
14
18
|
"node": ">=22.22.0"
|
|
15
19
|
},
|
|
@@ -39,10 +43,10 @@
|
|
|
39
43
|
"orm:cache:clear": "MIKRO_ORM_CLI_CONFIG=./mikro-orm.config.dev.ts MIKRO_ORM_ALLOW_GLOBAL_CLI=true medusa-mikro-orm cache:clear"
|
|
40
44
|
},
|
|
41
45
|
"devDependencies": {
|
|
42
|
-
"@medusajs/framework": "2.22.0
|
|
43
|
-
"@medusajs/test-utils": "2.22.0
|
|
46
|
+
"@medusajs/framework": "2.22.0",
|
|
47
|
+
"@medusajs/test-utils": "2.22.0"
|
|
44
48
|
},
|
|
45
49
|
"peerDependencies": {
|
|
46
|
-
"@medusajs/framework": "2.22.0
|
|
50
|
+
"@medusajs/framework": "2.22.0"
|
|
47
51
|
}
|
|
48
52
|
}
|