docs: rename data to event in subscribers across docs (#8149)
This commit is contained in:
@@ -12,12 +12,12 @@ In this chapter, you'll learn how subscribers receive an event's data payload.
|
||||
|
||||
When events are emitted, they’re emitted with a data payload.
|
||||
|
||||
The object that the subscriber function receives as a parameter has a `data` property, which is an object holding the event payload with additional context.
|
||||
The object that the subscriber function receives as a parameter has an `event` property, which is an object holding the event payload in a `data` property with additional context.
|
||||
|
||||
For example:
|
||||
|
||||
export const highlights = [
|
||||
["7", "", "The event's data payload."],
|
||||
["7", "event", "The event details."],
|
||||
["8", "{ id: string }", "The type of expected data payloads."],
|
||||
]
|
||||
|
||||
@@ -28,9 +28,9 @@ import type {
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
data,
|
||||
event,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const productId = data.data.id
|
||||
const productId = event.data.id
|
||||
console.log(`The product ${productId} was created`)
|
||||
}
|
||||
|
||||
@@ -39,16 +39,16 @@ export const config: SubscriberConfig = {
|
||||
}
|
||||
```
|
||||
|
||||
The `data` object has the following properties:
|
||||
The `event` object has the following properties:
|
||||
|
||||
<TypeList types={[
|
||||
{
|
||||
name: "data",
|
||||
type: "`object`",
|
||||
description: "The data payload of the event. Its properties depend on the event."
|
||||
description: "The data payload of the event. Its properties are different for each event."
|
||||
},
|
||||
{
|
||||
name: "eventName",
|
||||
name: "name",
|
||||
type: "string",
|
||||
description: "The name of the triggered event."
|
||||
},
|
||||
|
||||
@@ -92,13 +92,13 @@ import { IProductModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
data,
|
||||
event: { data },
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const productModuleService: IProductModuleService =
|
||||
container.resolve(ModuleRegistrationName.PRODUCT)
|
||||
|
||||
const productId = data.data.id
|
||||
const productId = data.id
|
||||
|
||||
const product = await productModuleService.retrieveProduct(
|
||||
productId
|
||||
|
||||
@@ -130,10 +130,10 @@ To execute the workflow, invoke it passing the Medusa container as a parameter.
|
||||
import { IUserModuleService } from "@medusajs/types"
|
||||
|
||||
export default async function handleCustomerCreate({
|
||||
data,
|
||||
event: { data },
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const userId = data.data.id
|
||||
const userId = data.id
|
||||
const userModuleService: IUserModuleService = container.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
|
||||
@@ -14,19 +14,19 @@ In your resource, such as a subscriber, resolve the Notification Module's main s
|
||||
|
||||
export const highlights = [
|
||||
["12", "notificationModuleService", "Resolve the Notification Module."],
|
||||
["17", "create", "Create the notification to be sent."],
|
||||
["15", "create", "Create the notification to be sent."],
|
||||
[
|
||||
"19",
|
||||
"17",
|
||||
'"email"',
|
||||
"Use the module provider defined for the `email` channel to send an email.",
|
||||
],
|
||||
[
|
||||
"20",
|
||||
"18",
|
||||
'"product-created"',
|
||||
"The ID of the template defined in the third-party service, such as SendGrid.",
|
||||
],
|
||||
[
|
||||
"21",
|
||||
"19",
|
||||
"data",
|
||||
"The data to pass to the template defined in the third-party service.",
|
||||
],
|
||||
@@ -41,7 +41,7 @@ import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { INotificationModuleService } from "@medusajs/types"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
data,
|
||||
event: { data },
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const notificationModuleService: INotificationModuleService =
|
||||
@@ -51,7 +51,7 @@ export default async function productCreateHandler({
|
||||
to: "shahednasser@gmail.com",
|
||||
channel: "email",
|
||||
template: "product-created",
|
||||
data: data.data,
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -116,14 +116,14 @@ To test the module out, create a simple subscriber at `src/subscribers/product-c
|
||||
|
||||
export const highlights = [
|
||||
["12", "notificationModuleService", "Resolve the Notification Module."],
|
||||
["17", "create", "Create the notification to be sent."],
|
||||
["15", "create", "Create the notification to be sent."],
|
||||
[
|
||||
"19",
|
||||
"17",
|
||||
'"email"',
|
||||
"By specifying the `email` channel, SendGrid will be used to send the notification.",
|
||||
],
|
||||
["20", '"product-created"', "The ID of the template defined in SendGrid."],
|
||||
["21", "data", "The data to pass to the template defined in SendGrid."],
|
||||
["18", '"product-created"', "The ID of the template defined in SendGrid."],
|
||||
["19", "data", "The data to pass to the template defined in SendGrid."],
|
||||
]
|
||||
|
||||
```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||
@@ -135,7 +135,7 @@ import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { INotificationModuleService } from "@medusajs/types"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
data,
|
||||
event: { data },
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const notificationModuleService: INotificationModuleService =
|
||||
@@ -145,7 +145,7 @@ export default async function productCreateHandler({
|
||||
to: "test@gmail.com",
|
||||
channel: "email",
|
||||
template: "product-created",
|
||||
data: data.data,
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -795,10 +795,10 @@ The `order.placed` event is currently not emitted.
|
||||
} from "@medusajs/types"
|
||||
|
||||
export default async function orderCreatedHandler({
|
||||
data,
|
||||
event: { data },
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const orderId = data.data.id
|
||||
const orderId = data.id
|
||||
|
||||
const orderModuleService: IOrderModuleService = container
|
||||
.resolve(ModuleRegistrationName.ORDER)
|
||||
@@ -886,15 +886,15 @@ You can also create a scheduled job that checks whether the number of new produc
|
||||
For example, create the file `src/subscribers/send-products-newsletter.ts` with the following content:
|
||||
|
||||
export const newsletterHighlights = [
|
||||
["33", "store", "Retrieve the first store in the application."],
|
||||
["35", "products", "Retrieve the products created since the last newsletter send date."],
|
||||
["41", "", "Check whether more than 10 products have been created before proceeding."],
|
||||
["45", "customers", "Retrieve all customers, assuming they're considered subscribed."],
|
||||
["47", "createNotifications", "Send a notification (newsletter) to each customer using the Notification Module."],
|
||||
["50", '"email"', "Send the notification through the email channel."],
|
||||
["51", '"newsletter_template"', "Specify the template name in the third-party service (for example, SendGrid)."],
|
||||
["53", "products", "Pass the created products to the template."],
|
||||
["58", "updateStores", "Update the store's `last_newsletter_send_date` property with the current date."]
|
||||
["32", "store", "Retrieve the first store in the application."],
|
||||
["34", "products", "Retrieve the products created since the last newsletter send date."],
|
||||
["40", "", "Check whether more than 10 products have been created before proceeding."],
|
||||
["44", "customers", "Retrieve all customers, assuming they're considered subscribed."],
|
||||
["46", "createNotifications", "Send a notification (newsletter) to each customer using the Notification Module."],
|
||||
["49", '"email"', "Send the notification through the email channel."],
|
||||
["50", '"newsletter_template"', "Specify the template name in the third-party service (for example, SendGrid)."],
|
||||
["52", "products", "Pass the created products to the template."],
|
||||
["57", "updateStores", "Update the store's `last_newsletter_send_date` property with the current date."]
|
||||
]
|
||||
|
||||
```ts title="src/subscribers/send-products-newsletter.ts" highlights={newsletterHighlights} collapsibleLines="1-14" expandButtonLabel="Show Imports"
|
||||
@@ -913,7 +913,6 @@ export const newsletterHighlights = [
|
||||
} from "@medusajs/types"
|
||||
|
||||
export default async function productCreateHandler({
|
||||
data,
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const productModuleService: IProductModuleService =
|
||||
@@ -926,7 +925,7 @@ export const newsletterHighlights = [
|
||||
container.resolve(ModuleRegistrationName.CUSTOMER)
|
||||
|
||||
const notificationModuleService:
|
||||
INotificationModuleService = container.resolve(
|
||||
INotificationModuleService = container.resolve(
|
||||
ModuleRegistrationName.NOTIFICATION
|
||||
)
|
||||
|
||||
|
||||
@@ -264,12 +264,13 @@ export const workflowHighlights = [
|
||||
import createProductWorkflow from "../workflows/create-product"
|
||||
|
||||
export default async function handleProductUpdate({
|
||||
data, container
|
||||
event: { data },
|
||||
container
|
||||
}: SubscriberArgs<{id: string}>) {
|
||||
createProductWorkflow(container)
|
||||
.run({
|
||||
input: {
|
||||
productId: data.data.id
|
||||
productId: data.id
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
|
||||
Reference in New Issue
Block a user