|
1
|
import { CodeTabs, CodeTab, Table, Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Links between Store Credit Module and Other Modules`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
This document showcases the module links defined between the Store Credit Module and other Commerce Modules. |
|
10
|
|
|
11
|
<Prerequisites |
|
12
|
items={[ |
|
13
|
{ |
|
14
|
text: "Loyalty Plugin Installed", |
|
15
|
link: "../page.mdx#1-install-the-loyalty-plugin", |
|
16
|
} |
|
17
|
]} |
|
18
|
/> |
|
19
|
|
|
20
|
## Summary |
|
21
|
|
|
22
|
The Store Credit Module has the following links to other modules: |
|
23
|
|
|
24
|
<Table> |
|
25
|
<Table.Header> |
|
26
|
<Table.Row> |
|
27
|
<Table.HeaderCell> |
|
28
|
First Data Model |
|
29
|
</Table.HeaderCell> |
|
30
|
<Table.HeaderCell> |
|
31
|
Second Data Model |
|
32
|
</Table.HeaderCell> |
|
33
|
<Table.HeaderCell> |
|
34
|
Type |
|
35
|
</Table.HeaderCell> |
|
36
|
<Table.HeaderCell> |
|
37
|
Description |
|
38
|
</Table.HeaderCell> |
|
39
|
</Table.Row> |
|
40
|
</Table.Header> |
|
41
|
<Table.Body> |
|
42
|
<Table.Row> |
|
43
|
<Table.Cell> |
|
44
|
`StoreCreditAccount` in [Store Credit Module](../page.mdx) |
|
45
|
</Table.Cell> |
|
46
|
<Table.Cell> |
|
47
|
`Customer` in [Customer Module](../../customer/page.mdx) |
|
48
|
</Table.Cell> |
|
49
|
<Table.Cell> |
|
50
|
Read-only |
|
51
|
</Table.Cell> |
|
52
|
<Table.Cell> |
|
53
|
[Learn more](#customer-module) |
|
54
|
</Table.Cell> |
|
55
|
</Table.Row> |
|
56
|
<Table.Row> |
|
57
|
<Table.Cell> |
|
58
|
`GiftCard` in [Loyalty Module](../../loyalty/page.mdx) |
|
59
|
</Table.Cell> |
|
60
|
<Table.Cell> |
|
61
|
`StoreCreditAccount` in [Store Credit Module](../page.mdx) |
|
62
|
</Table.Cell> |
|
63
|
<Table.Cell> |
|
64
|
Stored - many-to-one |
|
65
|
</Table.Cell> |
|
66
|
<Table.Cell> |
|
67
|
[Learn more](#loyalty-module) |
|
68
|
</Table.Cell> |
|
69
|
</Table.Row> |
|
70
|
</Table.Body> |
|
71
|
</Table> |
|
72
|
|
|
73
|
--- |
|
74
|
|
|
75
|
## Customer Module |
|
76
|
|
|
77
|
Medusa defines a read-only link between the `StoreCreditAccount` data model and the [Customer Module](../../customer/page.mdx)'s `Customer` data model. This means you can retrieve the customer associated with a store credit account, but you don't manage the links in a pivot table in the database. The customer of a store credit account is determined by the `customer_id` property of the `StoreCreditAccount` data model. |
|
78
|
|
|
79
|
### Retrieve with Query |
|
80
|
|
|
81
|
To retrieve the customer of a store credit account with [Query](!docs!/learn/fundamentals/query), pass `customer.*` in `fields`: |
|
82
|
|
|
83
|
<CodeTabs group="relation-query"> |
|
84
|
<CodeTab label="query.graph" value="method"> |
|
85
|
|
|
86
|
```ts |
|
87
|
const { data: storeCreditAccounts } = await query.graph({ |
|
88
|
entity: "store_credit_account", |
|
89
|
fields: [ |
|
90
|
"customer.*", |
|
91
|
], |
|
92
|
}) |
|
93
|
|
|
94
|
// storeCreditAccounts[0].customer |
|
95
|
``` |
|
96
|
|
|
97
|
</CodeTab> |
|
98
|
<CodeTab label="useQueryGraphStep" value="step"> |
|
99
|
|
|
100
|
```ts |
|
101
|
import { useQueryGraphStep } from "@medusajs/medusa/core-flows" |
|
102
|
|
|
103
|
// ... |
|
104
|
|
|
105
|
const { data: storeCreditAccounts } = useQueryGraphStep({ |
|
106
|
entity: "store_credit_account", |
|
107
|
fields: [ |
|
108
|
"customer.*", |
|
109
|
], |
|
110
|
}) |
|
111
|
|
|
112
|
// storeCreditAccounts[0].customer |
|
113
|
``` |
|
114
|
|
|
115
|
</CodeTab> |
|
116
|
</CodeTabs> |
|
117
|
|
|
118
|
--- |
|
119
|
|
|
120
|
## Loyalty Module |
|
121
|
|
|
122
|
Medusa defines a link between the `GiftCard` in the [Loyalty Module](../../loyalty/page.mdx) and `StoreCreditAccount` data models. This link is created when a gift card is created (alongside its store credit account) or when a gift card is redeemed. |
|
123
|
|
|
124
|
This allows you to retrieve the store credit account associated with a gift card. |
|
125
|
|
|
126
|
### Retrieve with Query |
|
127
|
|
|
128
|
To retrieve the store credit account of a gift card with [Query](!docs!/learn/fundamentals/query), pass `store_credit_account.*` in `fields`: |
|
129
|
|
|
130
|
<CodeTabs group="relation-query"> |
|
131
|
<CodeTab label="query.graph" value="method"> |
|
132
|
|
|
133
|
```ts |
|
134
|
const { data: giftCards } = await query.graph({ |
|
135
|
entity: "gift_card", |
|
136
|
fields: [ |
|
137
|
"store_credit_account.*", |
|
138
|
], |
|
139
|
}) |
|
140
|
|
|
141
|
// giftCards[0].store_credit_account |
|
142
|
``` |
|
143
|
|
|
144
|
</CodeTab> |
|
145
|
<CodeTab label="useQueryGraphStep" value="step"> |
|
146
|
|
|
147
|
```ts |
|
148
|
import { useQueryGraphStep } from "@medusajs/medusa/core-flows" |
|
149
|
|
|
150
|
// ... |
|
151
|
|
|
152
|
const { data: giftCards } = useQueryGraphStep({ |
|
153
|
entity: "gift_card", |
|
154
|
fields: [ |
|
155
|
"store_credit_account.*", |
|
156
|
], |
|
157
|
}) |
|
158
|
|
|
159
|
// giftCards[0].store_credit_account |
|
160
|
``` |
|
161
|
|
|
162
|
</CodeTab> |
|
163
|
</CodeTabs> |
|
164
|
|
|
165
|
### Manage with Link |
|
166
|
|
|
167
|
To manage the store credit account of a gift card, use [Link](!docs!/learn/fundamentals/module-links/link): |
|
168
|
|
|
169
|
<CodeTabs group="relation-link"> |
|
170
|
<CodeTab label="link.create" value="method"> |
|
171
|
|
|
172
|
```ts |
|
173
|
// ... |
|
174
|
|
|
175
|
await link.create({ |
|
176
|
loyalty: { |
|
177
|
gift_card_id: "gcard_123", |
|
178
|
}, |
|
179
|
store_credit: { |
|
180
|
store_credit_account_id: "sc_acc_123", |
|
181
|
}, |
|
182
|
}) |
|
183
|
``` |
|
184
|
|
|
185
|
</CodeTab> |
|
186
|
<CodeTab label="createRemoteLinkStep" value="step"> |
|
187
|
|
|
188
|
```ts |
|
189
|
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows" |
|
190
|
|
|
191
|
// ... |
|
192
|
|
|
193
|
createRemoteLinkStep({ |
|
194
|
loyalty: { |
|
195
|
gift_card_id: "gcard_123", |
|
196
|
}, |
|
197
|
store_credit: { |
|
198
|
store_credit_account_id: "sc_acc_123", |
|
199
|
}, |
|
200
|
}) |
|
201
|
``` |
|
202
|
|
|
203
|
</CodeTab> |
|
204
|
</CodeTabs> |