|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Inventory Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Inventory Module and how to use it in your application. |
|
14
|
|
|
15
|
<Note title="Looking for no-code docs?"> |
|
16
|
|
|
17
|
Refer to the [Medusa Admin User Guide](!user-guide!/inventory) to learn how to manage inventory and related features using the dashboard. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Medusa has inventory related features available out-of-the-box through the Inventory Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Inventory Module. |
|
22
|
|
|
23
|
<Note> |
|
24
|
|
|
25
|
Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation). |
|
26
|
|
|
27
|
</Note> |
|
28
|
|
|
29
|
## Inventory Features |
|
30
|
|
|
31
|
- [Inventory Items Management](./concepts/page.mdx): Store and manage inventory of any stock-kept item, such as product variants. |
|
32
|
- [Inventory Across Locations](./concepts/page.mdx#inventorylevel): Manage inventory levels across different locations, such as warehouses. |
|
33
|
- [Reservation Management](./concepts/page.mdx#reservationitem): Reserve quantities of inventory items at specific locations for orders or other purposes. |
|
34
|
- [Check Inventory Availability](/references/inventory-next/confirmInventory): Check whether an inventory item has the necessary quantity for purchase. |
|
35
|
- [Inventory Kits](./inventory-kit/page.mdx): Create and manage inventory kits for a single product, allowing you to implement use cases like bundled or multi-part products. |
|
36
|
|
|
37
|
--- |
|
38
|
|
|
39
|
## How to Use the Inventory Module |
|
40
|
|
|
41
|
In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism. |
|
42
|
|
|
43
|
You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package. |
|
44
|
|
|
45
|
For example: |
|
46
|
|
|
47
|
export const highlights = [ |
|
48
|
["12", "Modules.INVENTORY", "Resolve the module in a step."] |
|
49
|
] |
|
50
|
|
|
51
|
```ts title="src/workflows/create-inventory-item.ts" highlights={highlights} |
|
52
|
import { |
|
53
|
createWorkflow, |
|
54
|
WorkflowResponse, |
|
55
|
createStep, |
|
56
|
StepResponse, |
|
57
|
} from "@medusajs/framework/workflows-sdk" |
|
58
|
import { Modules } from "@medusajs/framework/utils" |
|
59
|
|
|
60
|
const createInventoryItemStep = createStep( |
|
61
|
"create-inventory-item", |
|
62
|
async ({}, { container }) => { |
|
63
|
const inventoryModuleService = container.resolve(Modules.INVENTORY) |
|
64
|
|
|
65
|
const inventoryItem = await inventoryModuleService.createInventoryItems({ |
|
66
|
sku: "SHIRT", |
|
67
|
title: "Green Medusa Shirt", |
|
68
|
requires_shipping: true, |
|
69
|
}) |
|
70
|
|
|
71
|
return new StepResponse({ inventoryItem }, inventoryItem.id) |
|
72
|
}, |
|
73
|
async (inventoryItemId, { container }) => { |
|
74
|
if (!inventoryItemId) { |
|
75
|
return |
|
76
|
} |
|
77
|
const inventoryModuleService = container.resolve(Modules.INVENTORY) |
|
78
|
|
|
79
|
await inventoryModuleService.deleteInventoryItems([inventoryItemId]) |
|
80
|
} |
|
81
|
) |
|
82
|
|
|
83
|
export const createInventoryItemWorkflow = createWorkflow( |
|
84
|
"create-inventory-item-workflow", |
|
85
|
() => { |
|
86
|
const { inventoryItem } = createInventoryItemStep() |
|
87
|
|
|
88
|
return new WorkflowResponse({ |
|
89
|
inventoryItem, |
|
90
|
}) |
|
91
|
} |
|
92
|
) |
|
93
|
``` |
|
94
|
|
|
95
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
96
|
|
|
97
|
<CodeTabs group="resource-types"> |
|
98
|
<CodeTab label="API Route" value="api-route"> |
|
99
|
|
|
100
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
101
|
import type { |
|
102
|
MedusaRequest, |
|
103
|
MedusaResponse, |
|
104
|
} from "@medusajs/framework/http" |
|
105
|
import { createInventoryItemWorkflow } from "../../workflows/create-inventory-item" |
|
106
|
|
|
107
|
export async function GET( |
|
108
|
req: MedusaRequest, |
|
109
|
res: MedusaResponse |
|
110
|
) { |
|
111
|
const { result } = await createInventoryItemWorkflow(req.scope) |
|
112
|
.run() |
|
113
|
|
|
114
|
res.send(result) |
|
115
|
} |
|
116
|
``` |
|
117
|
|
|
118
|
</CodeTab> |
|
119
|
<CodeTab label="Subscriber" value="subscriber"> |
|
120
|
|
|
121
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
122
|
import { |
|
123
|
type SubscriberConfig, |
|
124
|
type SubscriberArgs, |
|
125
|
} from "@medusajs/framework" |
|
126
|
import { createInventoryItemWorkflow } from "../workflows/create-inventory-item" |
|
127
|
|
|
128
|
export default async function handleUserCreated({ |
|
129
|
event: { data }, |
|
130
|
container, |
|
131
|
}: SubscriberArgs<{ id: string }>) { |
|
132
|
const { result } = await createInventoryItemWorkflow(container) |
|
133
|
.run() |
|
134
|
|
|
135
|
console.log(result) |
|
136
|
} |
|
137
|
|
|
138
|
export const config: SubscriberConfig = { |
|
139
|
event: "user.created", |
|
140
|
} |
|
141
|
``` |
|
142
|
|
|
143
|
</CodeTab> |
|
144
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
145
|
|
|
146
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
147
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
148
|
import { createInventoryItemWorkflow } from "../workflows/create-inventory-item" |
|
149
|
|
|
150
|
export default async function myCustomJob( |
|
151
|
container: MedusaContainer |
|
152
|
) { |
|
153
|
const { result } = await createInventoryItemWorkflow(container) |
|
154
|
.run() |
|
155
|
|
|
156
|
console.log(result) |
|
157
|
} |
|
158
|
|
|
159
|
export const config = { |
|
160
|
name: "run-once-a-day", |
|
161
|
schedule: `0 0 * * *`, |
|
162
|
} |
|
163
|
``` |
|
164
|
|
|
165
|
</CodeTab> |
|
166
|
</CodeTabs> |
|
167
|
|
|
168
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
169
|
|
|
170
|
--- |
|
171
|
|
|
172
|
<CommerceModuleSections /> |