|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Stock Location Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Stock Location 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!/settings/locations-and-shipping) to learn how to manage stock locations using the dashboard. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Medusa has stock location related features available out-of-the-box through the Stock Location 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 Stock Location 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
|
## Stock Location Features |
|
30
|
|
|
31
|
- [Stock Location Management](/references/stock-location-next/models): Store and manage stock locations. Medusa links stock locations with data models of other modules that require a location, such as the [Inventory Module's InventoryLevel](./links-to-other-modules/page.mdx). |
|
32
|
- [Address Management](/references/stock-location-next/models/StockLocationAddress): Manage the address of each stock location. |
|
33
|
|
|
34
|
--- |
|
35
|
|
|
36
|
## How to Use Stock Location Module's Service |
|
37
|
|
|
38
|
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. |
|
39
|
|
|
40
|
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. |
|
41
|
|
|
42
|
For example: |
|
43
|
|
|
44
|
export const highlights = [ |
|
45
|
["12", "Modules.STOCK_LOCATION", "Resolve the module in a step."] |
|
46
|
] |
|
47
|
|
|
48
|
```ts title="src/workflows/create-stock-location.ts" highlights={highlights} |
|
49
|
import { |
|
50
|
createWorkflow, |
|
51
|
WorkflowResponse, |
|
52
|
createStep, |
|
53
|
StepResponse, |
|
54
|
} from "@medusajs/framework/workflows-sdk" |
|
55
|
import { Modules } from "@medusajs/framework/utils" |
|
56
|
|
|
57
|
const createStockLocationStep = createStep( |
|
58
|
"create-stock-location", |
|
59
|
async ({}, { container }) => { |
|
60
|
const stockLocationModuleService = container.resolve(Modules.STOCK_LOCATION) |
|
61
|
|
|
62
|
const stockLocation = await stockLocationModuleService.createStockLocations({ |
|
63
|
name: "Warehouse 1", |
|
64
|
}) |
|
65
|
|
|
66
|
return new StepResponse({ stockLocation }, stockLocation.id) |
|
67
|
}, |
|
68
|
async (stockLocationId, { container }) => { |
|
69
|
if (!stockLocationId) { |
|
70
|
return |
|
71
|
} |
|
72
|
const stockLocationModuleService = container.resolve(Modules.STOCK_LOCATION) |
|
73
|
|
|
74
|
await stockLocationModuleService.deleteStockLocations([stockLocationId]) |
|
75
|
} |
|
76
|
) |
|
77
|
|
|
78
|
export const createStockLocationWorkflow = createWorkflow( |
|
79
|
"create-stock-location", |
|
80
|
() => { |
|
81
|
const { stockLocation } = createStockLocationStep() |
|
82
|
|
|
83
|
return new WorkflowResponse({ stockLocation }) |
|
84
|
} |
|
85
|
) |
|
86
|
``` |
|
87
|
|
|
88
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
89
|
|
|
90
|
<CodeTabs group="resource-types"> |
|
91
|
<CodeTab label="API Route" value="api-route"> |
|
92
|
|
|
93
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
94
|
import type { |
|
95
|
MedusaRequest, |
|
96
|
MedusaResponse, |
|
97
|
} from "@medusajs/framework/http" |
|
98
|
import { createStockLocationWorkflow } from "../../workflows/create-stock-location" |
|
99
|
|
|
100
|
export async function GET( |
|
101
|
req: MedusaRequest, |
|
102
|
res: MedusaResponse |
|
103
|
) { |
|
104
|
const { result } = await createStockLocationWorkflow(req.scope) |
|
105
|
.run() |
|
106
|
|
|
107
|
res.send(result) |
|
108
|
} |
|
109
|
``` |
|
110
|
|
|
111
|
</CodeTab> |
|
112
|
<CodeTab label="Subscriber" value="subscriber"> |
|
113
|
|
|
114
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
115
|
import { |
|
116
|
type SubscriberConfig, |
|
117
|
type SubscriberArgs, |
|
118
|
} from "@medusajs/framework" |
|
119
|
import { createStockLocationWorkflow } from "../workflows/create-stock-location" |
|
120
|
|
|
121
|
export default async function handleUserCreated({ |
|
122
|
event: { data }, |
|
123
|
container, |
|
124
|
}: SubscriberArgs<{ id: string }>) { |
|
125
|
const { result } = await createStockLocationWorkflow(container) |
|
126
|
.run() |
|
127
|
|
|
128
|
console.log(result) |
|
129
|
} |
|
130
|
|
|
131
|
export const config: SubscriberConfig = { |
|
132
|
event: "user.created", |
|
133
|
} |
|
134
|
``` |
|
135
|
|
|
136
|
</CodeTab> |
|
137
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
138
|
|
|
139
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
140
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
141
|
import { createStockLocationWorkflow } from "../workflows/create-stock-location" |
|
142
|
|
|
143
|
export default async function myCustomJob( |
|
144
|
container: MedusaContainer |
|
145
|
) { |
|
146
|
const { result } = await createStockLocationWorkflow(container) |
|
147
|
.run() |
|
148
|
|
|
149
|
console.log(result) |
|
150
|
} |
|
151
|
|
|
152
|
export const config = { |
|
153
|
name: "run-once-a-day", |
|
154
|
schedule: `0 0 * * *`, |
|
155
|
} |
|
156
|
``` |
|
157
|
|
|
158
|
</CodeTab> |
|
159
|
</CodeTabs> |
|
160
|
|
|
161
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
162
|
|
|
163
|
--- |
|
164
|
|
|
165
|
<CommerceModuleSections /> |