www/apps/resources/app/commerce-modules/store-credit/links-to-other-modules/page.mdx
import { CodeTabs, CodeTab, Table, Prerequisites } from "docs-ui"
export const metadata = {
title: Links between Store Credit Module and Other Modules,
}
This document showcases the module links defined between the Store Credit Module and other Commerce Modules.
<Prerequisites items={[ { text: "Loyalty Plugin Installed", link: "../page.mdx#1-install-the-loyalty-plugin", } ]} />
The Store Credit Module has the following links to other modules:
<Table> <Table.Header> <Table.Row> <Table.HeaderCell> First Data Model </Table.HeaderCell> <Table.HeaderCell> Second Data Model </Table.HeaderCell> <Table.HeaderCell> Type </Table.HeaderCell> <Table.HeaderCell> Description </Table.HeaderCell> </Table.Row> </Table.Header> <Table.Body> <Table.Row> <Table.Cell> `StoreCreditAccount` in [Store Credit Module](../page.mdx) </Table.Cell> <Table.Cell> `Customer` in [Customer Module](../../customer/page.mdx) </Table.Cell> <Table.Cell> Read-only </Table.Cell> <Table.Cell> [Learn more](#customer-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> `GiftCard` in [Loyalty Module](../../loyalty/page.mdx) </Table.Cell> <Table.Cell> `StoreCreditAccount` in [Store Credit Module](../page.mdx) </Table.Cell> <Table.Cell> Stored - many-to-one </Table.Cell> <Table.Cell> [Learn more](#loyalty-module) </Table.Cell> </Table.Row> </Table.Body> </Table>Medusa defines a read-only link between the StoreCreditAccount data model and the Customer Module'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.
To retrieve the customer of a store credit account with Query, pass customer.* in fields:
const { data: storeCreditAccounts } = await query.graph({
entity: "store_credit_account",
fields: [
"customer.*",
],
})
// storeCreditAccounts[0].customer
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: storeCreditAccounts } = useQueryGraphStep({
entity: "store_credit_account",
fields: [
"customer.*",
],
})
// storeCreditAccounts[0].customer
Medusa defines a link between the GiftCard in the Loyalty Module 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.
This allows you to retrieve the store credit account associated with a gift card.
To retrieve the store credit account of a gift card with Query, pass store_credit_account.* in fields:
const { data: giftCards } = await query.graph({
entity: "gift_card",
fields: [
"store_credit_account.*",
],
})
// giftCards[0].store_credit_account
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: giftCards } = useQueryGraphStep({
entity: "gift_card",
fields: [
"store_credit_account.*",
],
})
// giftCards[0].store_credit_account
To manage the store credit account of a gift card, use Link:
<CodeTabs group="relation-link"> <CodeTab label="link.create" value="method">// ...
await link.create({
loyalty: {
gift_card_id: "gcard_123",
},
store_credit: {
store_credit_account_id: "sc_acc_123",
},
})
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
loyalty: {
gift_card_id: "gcard_123",
},
store_credit: {
store_credit_account_id: "sc_acc_123",
},
})