1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Guide: Create Brand Workflow`,
5 }
6
7 # {metadata.title}
8
9 This chapter builds on the work from the [previous chapter](../module/page.mdx) where you created a Brand Module.
10
11 After adding custom modules to your application, you build commerce features around them using workflows. A workflow is a series of queries and actions, called steps, that complete a task spanning across modules. You construct a workflow similar to a regular function, but it's a special function that allows you to define roll-back logic, retry configurations, and more advanced features.
12
13 The workflow you'll create in this chapter will use the Brand Module's service to implement the feature of creating a brand. In the [next chapter](../api-route/page.mdx), you'll expose an API route that allows admin users to create a brand, and you'll use this workflow in the route's implementation.
14
15 <Note>
16
17 Learn more about workflows in [this chapter](../../../fundamentals/workflows/page.mdx).
18
19 </Note>
20
21 <Prerequisites
22 items={[
23 {
24 text: "Brand Module",
25 link: "/learn/customization/custom-features/module"
26 }
27 ]}
28 />
29
30 ---
31
32 ## 1. Create createBrandStep
33
34 A workflow consists of a series of steps, each step created in a TypeScript or JavaScript file under the `src/workflows` directory. A step is defined using `createStep` from the Workflows SDK
35
36 The workflow you're creating in this guide has one step to create the brand. So, create the file `src/workflows/create-brand.ts` with the following content:
37
38 ![Directory structure in the Medusa project after adding the file for createBrandStep](https://res.cloudinary.com/dza7lstvk/image/upload/v1732869184/Medusa%20Book/brand-workflow-dir-overview-1_fjvf5j.jpg)
39
40 ```ts title="src/workflows/create-brand.ts"
41 import {
42 createStep,
43 StepResponse,
44 } from "@medusajs/framework/workflows-sdk"
45 import { BRAND_MODULE } from "../modules/brand"
46 import BrandModuleService from "../modules/brand/service"
47
48 export type CreateBrandStepInput = {
49 name: string
50 }
51
52 export const createBrandStep = createStep(
53 "create-brand-step",
54 async (input: CreateBrandStepInput, { container }) => {
55 const brandModuleService: BrandModuleService = container.resolve(
56 BRAND_MODULE
57 )
58
59 const brand = await brandModuleService.createBrands(input)
60
61 return new StepResponse(brand, brand.id)
62 }
63 )
64 ```
65
66 You create a `createBrandStep` using the `createStep` function. It accepts the step's unique name as a first parameter, and the step's function as a second parameter.
67
68 The step function receives two parameters: input passed to the step when it's invoked, and an object of general context and configurations. This object has a `container` property, which is the Medusa container.
69
70 The [Medusa container](../../../fundamentals/medusa-container/page.mdx) is a registry of Framework and commerce tools accessible in your customizations, such as a workflow's step. The Medusa application registers the services of core and custom modules in the container, allowing you to resolve and use them.
71
72 So, In the step function, you use the Medusa container to resolve the Brand Module's service and use its generated `createBrands` method, which accepts an object of brands to create.
73
74 <Note>
75
76 Learn more about the generated `create` method's usage in [this reference](!resources!/service-factory-reference/methods/create).
77
78 </Note>
79
80 A step must return an instance of `StepResponse`. Its first parameter is the data returned by the step, and the second is the data passed to the compensation function, which you'll learn about next.
81
82 ### Add Compensation Function to Step
83
84 You define for each step a compensation function that's executed when an error occurs in the workflow. The compensation function defines the logic to roll-back the changes made by the step. This ensures your data remains consistent if an error occurs, which is especially useful when you integrate third-party services.
85
86 <Note>
87
88 Learn more about the compensation function in [this chapter](../../../fundamentals/workflows/compensation-function/page.mdx).
89
90 </Note>
91
92 To add a compensation function to the `createBrandStep`, pass it as a third parameter to `createStep`:
93
94 ```ts title="src/workflows/create-brand.ts"
95 export const createBrandStep = createStep(
96 // ...
97 async (id: string, { container }) => {
98 const brandModuleService: BrandModuleService = container.resolve(
99 BRAND_MODULE
100 )
101
102 await brandModuleService.deleteBrands(id)
103 }
104 )
105 ```
106
107 The compensation function's first parameter is the brand's ID which you passed as a second parameter to the step function's returned `StepResponse`. It also accepts a context object with a `container` property as a second parameter, similar to the step function.
108
109 In the compensation function, you resolve the Brand Module's service from the Medusa container, then use its generated `deleteBrands` method to delete the brand created by the step. This method accepts the ID of the brand to delete.
110
111 <Note>
112
113 Learn more about the generated `delete` method's usage in [this reference](!resources!/service-factory-reference/methods/delete).
114
115 </Note>
116
117 So, if an error occurs during the workflow's execution, the brand that was created by the step is deleted to maintain data consistency.
118
119 ---
120
121 ## 2. Create createBrandWorkflow
122
123 You can now create the workflow that runs the `createBrandStep`. A workflow is created in a TypeScript or JavaScript file under the `src/workflows` directory. In the file, you use `createWorkflow` from the Workflows SDK to create the workflow.
124
125 Add the following content in the same `src/workflows/create-brand.ts` file:
126
127 ```ts title="src/workflows/create-brand.ts"
128 // other imports...
129 import {
130 // ...
131 createWorkflow,
132 WorkflowResponse,
133 } from "@medusajs/framework/workflows-sdk"
134
135 // ...
136
137 type CreateBrandWorkflowInput = {
138 name: string
139 }
140
141 export const createBrandWorkflow = createWorkflow(
142 "create-brand",
143 (input: CreateBrandWorkflowInput) => {
144 const brand = createBrandStep(input)
145
146 return new WorkflowResponse(brand)
147 }
148 )
149 ```
150
151 You create the `createBrandWorkflow` using the `createWorkflow` function. This function accepts two parameters: the workflow's unique name, and the workflow's constructor function holding the workflow's implementation.
152
153 The constructor function accepts the workflow's input as a parameter. In the function, you invoke the `createBrandStep` you created in the previous step to create a brand.
154
155 A workflow must return an instance of `WorkflowResponse`. It accepts as a parameter the data to return to the workflow's executor.
156
157 ---
158
159 ## Next Steps: Expose Create Brand API Route
160
161 You now have a `createBrandWorkflow` that you can execute to create a brand.
162
163 In the next chapter, you'll add an API route that allows admin users to create a brand. You'll learn how to create the API route, and execute in it the workflow you implemented in this chapter.