|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Region Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Region 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/regions) to learn how to manage regions using the dashboard. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Medusa has region related features available out-of-the-box through the Region 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 Region 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
|
--- |
|
30
|
|
|
31
|
## Region Features |
|
32
|
|
|
33
|
- [Region Management](/references/region/models/Region): Manage regions in your store. You can create regions with different currencies and settings. |
|
34
|
- [Multi-Currency Support](/references/region/models/Region): Each region has a currency. You can support multiple currencies in your store by creating multiple regions. |
|
35
|
- [Different Settings Per Region](/references/region/models/Region): Each region has its own settings, such as what countries belong to a region or its tax settings. |
|
36
|
|
|
37
|
--- |
|
38
|
|
|
39
|
## How to Use Region Module's Service |
|
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.REGION", "Resolve the module in a step."] |
|
49
|
] |
|
50
|
|
|
51
|
```ts title="src/workflows/create-region.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 createRegionStep = createStep( |
|
61
|
"create-region", |
|
62
|
async ({}, { container }) => { |
|
63
|
const regionModuleService = container.resolve(Modules.REGION) |
|
64
|
|
|
65
|
const region = await regionModuleService.createRegions({ |
|
66
|
name: "Europe", |
|
67
|
currency_code: "eur", |
|
68
|
}) |
|
69
|
|
|
70
|
return new StepResponse({ region }, region.id) |
|
71
|
}, |
|
72
|
async (regionId, { container }) => { |
|
73
|
if (!regionId) { |
|
74
|
return |
|
75
|
} |
|
76
|
const regionModuleService = container.resolve(Modules.REGION) |
|
77
|
|
|
78
|
await regionModuleService.deleteRegions([regionId]) |
|
79
|
} |
|
80
|
) |
|
81
|
|
|
82
|
export const createRegionWorkflow = createWorkflow( |
|
83
|
"create-region", |
|
84
|
() => { |
|
85
|
const { region } = createRegionStep() |
|
86
|
|
|
87
|
return new WorkflowResponse({ |
|
88
|
region, |
|
89
|
}) |
|
90
|
} |
|
91
|
) |
|
92
|
``` |
|
93
|
|
|
94
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
95
|
|
|
96
|
<CodeTabs group="resource-types"> |
|
97
|
<CodeTab label="API Route" value="api-route"> |
|
98
|
|
|
99
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
100
|
import type { |
|
101
|
MedusaRequest, |
|
102
|
MedusaResponse, |
|
103
|
} from "@medusajs/framework/http" |
|
104
|
import { createRegionWorkflow } from "../../workflows/create-region" |
|
105
|
|
|
106
|
export async function GET( |
|
107
|
req: MedusaRequest, |
|
108
|
res: MedusaResponse |
|
109
|
) { |
|
110
|
const { result } = await createRegionWorkflow(req.scope) |
|
111
|
.run() |
|
112
|
|
|
113
|
res.send(result) |
|
114
|
} |
|
115
|
``` |
|
116
|
|
|
117
|
</CodeTab> |
|
118
|
<CodeTab label="Subscriber" value="subscriber"> |
|
119
|
|
|
120
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
121
|
import { |
|
122
|
type SubscriberConfig, |
|
123
|
type SubscriberArgs, |
|
124
|
} from "@medusajs/framework" |
|
125
|
import { createRegionWorkflow } from "../workflows/create-region" |
|
126
|
|
|
127
|
export default async function handleUserCreated({ |
|
128
|
event: { data }, |
|
129
|
container, |
|
130
|
}: SubscriberArgs<{ id: string }>) { |
|
131
|
const { result } = await createRegionWorkflow(container) |
|
132
|
.run() |
|
133
|
|
|
134
|
console.log(result) |
|
135
|
} |
|
136
|
|
|
137
|
export const config: SubscriberConfig = { |
|
138
|
event: "user.created", |
|
139
|
} |
|
140
|
``` |
|
141
|
|
|
142
|
</CodeTab> |
|
143
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
144
|
|
|
145
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
146
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
147
|
import { createRegionWorkflow } from "../workflows/create-region" |
|
148
|
|
|
149
|
export default async function myCustomJob( |
|
150
|
container: MedusaContainer |
|
151
|
) { |
|
152
|
const { result } = await createRegionWorkflow(container) |
|
153
|
.run() |
|
154
|
|
|
155
|
console.log(result) |
|
156
|
} |
|
157
|
|
|
158
|
export const config = { |
|
159
|
name: "run-once-a-day", |
|
160
|
schedule: `0 0 * * *`, |
|
161
|
} |
|
162
|
``` |
|
163
|
|
|
164
|
</CodeTab> |
|
165
|
</CodeTabs> |
|
166
|
|
|
167
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
168
|
|
|
169
|
--- |
|
170
|
|
|
171
|
<CommerceModuleSections /> |