docs: documentation for v1.18 (#5652)

* docs: documentation for v.17.5

* fix links

* updated version number
This commit is contained in:
Shahed Nasser
2023-11-21 10:57:11 +02:00
committed by GitHub
parent adc60e519c
commit 9c7f95c3d5
36 changed files with 1499 additions and 1336 deletions

View File

@@ -9,14 +9,12 @@ In this document, youll learn how to handle the order claim event and send a
## Overview
When a guest customer places an order, the order is not associated with a customer. It is associated with an email address.
When a guest customer places an order, the order isn't associated with a customer. It's associated with an email address.
After the customer registers, later on, they can claim that order by providing the orders ID.
When the customer requests to claim the order, the event `order-update-token.created` is triggered on the Medusa backend. This event should be used to send the customer a confirmation email.
### What Youll Learn
In this document, youll learn how to handle the `order-update-token.created` event on the backend to send the customer a confirmation email.
---
@@ -35,170 +33,132 @@ This document has an example using the [SendGrid](../../../plugins/notifications
---
## Step 1: Create a Subscriber
## Method 1: Using a Subscriber
To subscribe to and handle an event, you must create a subscriber.
To subscribe to an event, you must create a subscriber.
:::tip
:::note
You can learn more about subscribers in the [Subscribers](../../../development/events/subscribers.mdx) documentation.
You can learn more about subscribers in the [Subscribers documentation](../../../development/events/subscribers.mdx).
:::
Create the file `src/subscribers/claim-order.ts` with the following content:
Create the file `src/subscribers/order-claim.ts` with the following content:
```ts title=src/subscribers/claim-order.ts
type InjectedDependencies = {
// TODO add necessary dependencies
```ts title=src/subscribers/order-claim.ts
import {
type SubscriberConfig,
type SubscriberArgs,
} from "@medusajs/medusa"
export default async function handleOrderClaim({
data, eventName, container, pluginOptions,
}: SubscriberArgs<Record<string, string>>) {
// TODO: handle event
}
class ClaimOrderSubscriber {
constructor(container: InjectedDependencies) {
// TODO subscribe to event
}
export const config: SubscriberConfig = {
event: "order-update-token.created",
context: {
subscriberId: "customer-created-handler",
},
}
export default ClaimOrderSubscriber
```
Youll be adding in the next step the necessary dependencies to the subscriber.
In this file, you export a configuration object indicating that the subscriber is listening to the `order-update-token.created` event.
:::info
You also export a handler function `handleOrderClaim`. In the parameter it receives, the `data` object is the payload emitted when the event was triggered, which is an object of the following format:
You can learn more about [dependency injection](../../../development/fundamentals/dependency-injection.md) in this documentation.
:::
---
## Step 2: Subscribe to the Event
In this step, youll subscribe to the `order-update-token.created` event to send the customer a notification about their order edit.
There are two ways to do this:
### Method 1: Using the NotificationService
If the notification provider youre using already implements the logic to handle this event, you can subscribe to the event using the `NotificationService`:
```ts title=src/subscribers/claim-order.ts
import { NotificationService } from "@medusajs/medusa"
type InjectedDependencies = {
notificationService: NotificationService
```ts
data = {
// string - email of order
old_email,
// string - ID of customer
new_customer_id,
// array of string - IDs of orders
orders,
// string - token used for verification
token,
}
class ClaimOrderSubscriber {
constructor({ notificationService }: InjectedDependencies) {
notificationService.subscribe(
"order-update-token.created",
"<NOTIFICATION_PROVIDER_IDENTIFIER>"
)
}
}
export default ClaimOrderSubscriber
```
Where `<NOTIFICATION_PROVIDER_IDENTIFIER>` is the identifier for your notification provider.
In this method, you should typically send an email to the customer. You can place any content in the email, but should mainly include the link to confirm claiming the order.
:::info
You can learn more about handling events with the Notification Service using [this documentation](../../../development/notification/create-notification-provider.md).
:::
### Method 2: Using the EventBusService
If the notification provider youre using isnt configured to handle this event, or you want to implement some other custom logic, you can subscribe to the event using the `EventBusService`:
```ts title=src/subscribers/claim-order.ts
import { EventBusService } from "@medusajs/medusa"
type InjectedDependencies = {
eventBusService: EventBusService
}
class ClaimOrderSubscriber {
constructor({ eventBusService }: InjectedDependencies) {
eventBusService.subscribe(
"order-update-token.created",
this.handleRequestClaimOrder
)
}
handleRequestClaimOrder = async (data) => {
// TODO: handle event
}
}
export default ClaimOrderSubscriber
```
When using this method, youll have to handle the logic of sending the confirmation email to the customer inside the handler function, which in this case is `handleRequestClaimOrder`.
The `handleRequestClaimOrder` event receives a `data` object as a parameter. This object holds the following properties:
1. `old_email`: The email associated with the orders.
2. `new_customer_id`: The ID of the customer claiming the orders.
3. `orders`: An array of the order IDs that the customer is requesting to claim.
4. `token`: A verification token. This token is used to later verify the claim request and associate the order with the customer.
In this method, you should typically send an email to the customers old email. In the email, you should link to a page in your storefront and pass the `token` as a parameter.
The page would then send a request to the backend to verify that the `token` is valid and associate the order with the customer. You can read more about how to implement this in your storefront in [this documentation](../storefront/implement-claim-order.mdx).
---
## Example: Using SendGrid
### Example: Using SendGrid
For example, you can implement this subscriber to send emails using SendGrid:
<!-- eslint-disable max-len -->
```ts title=src/subscribers/order-claim.ts
import {
type SubscriberConfig,
type SubscriberArgs,
} from "@medusajs/medusa"
```ts title=src/subscribers/claim-order.ts
import { EventBusService } from "@medusajs/medusa"
export default async function handleOrderClaim({
data, eventName, container, pluginOptions,
}: SubscriberArgs<Record<string, string>>) {
const sendGridService = container.resolve("sendgridService")
type InjectedDependencies = {
eventBusService: EventBusService,
sendgridService: any
sendGridService.sendEmail({
templateId: "order-claim-confirmation",
from: "hello@medusajs.com",
to: data.old_email,
dynamic_template_data: {
link:
`http://example.com/confirm-order-claim/${data.token}`,
// other data...
},
})
}
class ClaimOrderSubscriber {
protected sendGridService: any
constructor({
eventBusService,
sendgridService,
}: InjectedDependencies) {
this.sendGridService = sendgridService
eventBusService.subscribe(
"order-update-token.created",
this.handleRequestClaimOrder
)
}
handleRequestClaimOrder = async (data) => {
this.sendGridService.sendEmail({
templateId: "order-claim-confirmation",
from: "hello@medusajs.com",
to: data.old_email,
dynamic_template_data: {
link: `http://example.com/confirm-order-claim/${data.token}`,
// other data...
},
})
}
export const config: SubscriberConfig = {
event: "order-update-token.created",
context: {
subscriberId: "customer-created-handler",
},
}
export default ClaimOrderSubscriber
```
Notice how the `token` is passed to the storefront link as a parameter.
---
## Method 2: Using the NotificationService
If the notification provider youre using already implements the logic to handle this event, you can create a [Loader](../../../development/loaders/overview.mdx) to subscribe the Notification provider to the `order-update-token.created` event.
For example:
```ts title=src/loaders/order-claim.ts
import {
MedusaContainer,
NotificationService,
} from "@medusajs/medusa"
export default async (
container: MedusaContainer
): Promise<void> => {
const notificationService = container.resolve<
NotificationService
>("notificationService")
notificationService.subscribe(
"order-update-token.created",
"<NOTIFICATION_PROVIDER_IDENTIFIER>"
)
}
```
Where `<NOTIFICATION_PROVIDER_IDENTIFIER>` is the identifier for your notification provider. For example, `sendgrid`.
:::note
You can learn more about handling events with the Notification Service using [this documentation](../../../development/notification/create-notification-provider.md).
:::
---
## See Also
- [Implement claim-order flow in your storefront](../storefront/implement-claim-order.mdx)

View File

@@ -11,22 +11,12 @@ In this document, youll learn how to send an order confirmation email to the
When an order is placed, the `order.placed` event is triggered. You can listen to this event in a subscriber to perform an action, such as send the customer an order confirmation email.
This guide explains how to create the subscriber and how to use SendGrid to send the confirmation email. SendGrid is only used to illustrate how the process works, but youre free to use any other notification service.
:::note
SendGrid is already configured to send emails when an order has been placed. So, by installing and configuring the plugin, you don't need to actually handle sending the order confirmation email. It's used as an example here to illustrate the process only.
:::
This guide explains how you can listen to the `order.placed` event to send an email to the customer.
---
## Prerequisites
### Medusa Backend
Its assumed you already have the Medusa backend installed. If not, you can either use the [create-medusa-app command](../../../create-medusa-app.mdx) to install different Medusa tools, including the backend, or [install the backend only](../../../development/backend/install.mdx).
### Event Bus Module
The event bus module trigger the event to the listening subscribers. So, its required to have an event bus module installed and configured on your Medusa backend.
@@ -41,221 +31,128 @@ You can also find other available Notification provider plugins in the [Plugins
---
## Step 1: Create the Subscriber
## Method 1: Using a Subscriber
To subscribe to and handle an event, you must create a subscriber.
To subscribe to an event, you must create a [subscriber](../../../development/events/subscribers.mdx).
:::note
Create the file `src/subscribers/order-placed.ts` with the following content:
You can learn more about subscribers in the [Subscribers documentation](../../../development/events/subscribers.mdx).
```ts title=src/subscribers/order-placed.ts
import {
type SubscriberConfig,
type SubscriberArgs,
OrderService,
} from "@medusajs/medusa"
:::
Create the file `src/subscribers/order-confirmation.ts` with the following content:
```ts title=src/subscribers/order-confirmation.ts
type InjectedDependencies = {
// TODO add necessary dependencies
export default async function handleOrderPlaced({
data, eventName, container, pluginOptions,
}: SubscriberArgs<Record<string, string>>) {
// TODO: handle event
}
class OrderConfirmationSubscriber {
constructor(container: InjectedDependencies) {
// TODO subscribe to event
}
export const config: SubscriberConfig = {
event: OrderService.Events.PLACED,
context: {
subscriberId: "order-placed-handler",
},
}
export default OrderConfirmationSubscriber
```
Youll be adding in the next step the necessary dependencies to the subscriber.
In this file, you export a configuration object indicating that the subscriber is listening to the `OrderService.Events.PLACED` (or `order.placed`) event.
You also export a handler function `handleCustomerConfirmation`. In the parameter it receives, the `data` object is the payload emitted when the event was triggered, which is an object that includes the ID of the order in the `id` property.
In this method, you should typically send an email to the customer. You can place any content in the email, such as the order's items and total.
### Example: Using SendGrid
:::note
You can learn more about dependency injection in [this documentation](../../../development/fundamentals/dependency-injection.md).
This example is only used to illustrate how the functionality can be implemented. The SendGrid plugin automatically handles sending an email when an order is placed once you install and configure the plugin in your backend.
:::
For example, you can implement this subscriber to send emails using [SendGrid](../../../plugins/notifications/sendgrid.mdx):
```ts title=src/subscribers/order-placed.ts
import {
type SubscriberConfig,
type SubscriberArgs,
OrderService,
} from "@medusajs/medusa"
export default async function handleOrderPlaced({
data, eventName, container, pluginOptions,
}: SubscriberArgs<Record<string, string>>) {
const sendGridService = container.resolve("sendgridService")
const orderService: OrderService = container.resolve(
"orderService"
)
const order = await orderService.retrieve(data.id, {
// you can include other relations as well
relations: ["items"],
})
sendGridService.sendEmail({
templateId: "order-confirmation",
from: "hello@medusajs.com",
to: order.email,
dynamic_template_data: {
// any data necessary for your template...
items: order.items,
status: order.status,
},
})
}
export const config: SubscriberConfig = {
event: OrderService.Events.PLACED,
context: {
subscriberId: "order-placed-handler",
},
}
```
Notice that you should replace the values in the object passed to the `sendEmail` method:
- `templateId`: Should be the ID of your confirmation email template in SendGrid.
- `from`: Should be the from email.
- `to`: Should be the customers email.
- `data`: Should be an object holding any data that should be passed to your SendGrid email template.
---
## Step 2: Subscribe to the Event
## Method 2: Using the NotificationService
In this step, youll subscribe to the `order.placed` event to send the customer an order confirmation email.
If the notification provider youre using already implements the logic to handle this event, you can create a [Loader](../../../development/loaders/overview.mdx) to subscribe the Notification provider to the `order.placed` event.
There are two ways to do this:
For example:
### Method 1: Using the NotificationService
```ts title=src/loaders/customer-confirmation.ts
import {
MedusaContainer,
NotificationService,
} from "@medusajs/medusa"
If the notification provider youre using already implements the logic to handle this event, you can subscribe to the event using the `NotificationService`:
export default async (
container: MedusaContainer
): Promise<void> => {
const notificationService = container.resolve<
NotificationService
>("notificationService")
```ts title=src/subscribers/order-confirmation.ts
import { NotificationService } from "@medusajs/medusa"
type InjectedDependencies = {
notificationService: NotificationService
notificationService.subscribe(
"order.placed",
"<NOTIFICATION_PROVIDER_IDENTIFIER>"
)
}
class OrderConfirmationSubscriber {
constructor({ notificationService }: InjectedDependencies) {
notificationService.subscribe(
"order.placed",
"<NOTIFICATION_PROVIDER_IDENTIFIER>"
)
}
}
export default OrderConfirmationSubscriber
```
Where `<NOTIFICATION_PROVIDER_IDENTIFIER>` is the identifier for your notification provider.
Where `<NOTIFICATION_PROVIDER_IDENTIFIER>` is the identifier for your notification provider. For example, `sendgrid`.
:::note
You can learn more about handling events with the Notification Service using [this documentation](../../../development/notification/create-notification-provider.md).
:::
### Method 2: Using the EventBusService
If the notification provider youre using isnt configured to handle this event, or you want to implement some other custom logic, you can subscribe to the event using the `EventBusService`:
```ts title=src/subscribers/order-confirmation.ts
import { EventBusService } from "@medusajs/medusa"
type InjectedDependencies = {
eventBusService: EventBusService
}
class OrderConfirmationSubscriber {
constructor({ eventBusService }: InjectedDependencies) {
eventBusService.subscribe(
"order.placed",
this.handleOrderConfirmation
)
}
handleOrderConfirmation = async (
data: Record<string, any>
) => {
// TODO: handle event
}
}
export default OrderConfirmationSubscriber
```
When using this method, youll have to handle the logic of sending the order confirmation email to the customer inside the handler function, which in this case is `handleOrderConfirmation`.
## Step 3: Handle the Event
The `handleOrderConfirmation` event receives a `data` object as a parameter. This object holds two properties:
- `id`: the ID of the order that was placed.
- `no_notification`: a boolean value indicating whether the customer should receive notifications about the order or not.
In this method, you should typically send an email to the customer if `no_notification` is enabled.
To retrieve the order's details, you can add the `OrderService` into `InjectedDependencies` and use it within `handleOrderConfirmation`. For example:
```ts title=src/subscribers/order-confirmation.ts
import { EventBusService, OrderService } from "@medusajs/medusa"
type InjectedDependencies = {
eventBusService: EventBusService
orderService: OrderService
}
class OrderConfirmationSubscriber {
protected readonly orderService_: OrderService
constructor({
eventBusService,
orderService,
}: InjectedDependencies) {
this.orderService_ = orderService
eventBusService.subscribe(
"order.placed",
this.handleOrderConfirmation
)
}
handleOrderConfirmation = async (
data: Record<string, any>
) => {
const order = await this.orderService_.retrieve(data.id, {
// you can include other relations as well
relations: ["items"],
})
// TODO: handle event
}
}
export default OrderConfirmationSubscriber
```
After retrieving the order, you can add the logic necessary to send the email. In the email, you can include any content you want. For example, you can show the order's items or the order's status.
### Example: Using SendGrid
:::note
This example is only used to illustrate how the functionality can be implemented. As mentioned in the introduction, there's actually no need to implement this subscriber if you have the SendGrid plugin installed and configured, as it will automatically handle it.
:::
For example, you can implement this subscriber to send emails using SendGrid:
```ts title=src/subscribers/order-confirmation.ts
import { EventBusService, OrderService } from "@medusajs/medusa"
type InjectedDependencies = {
eventBusService: EventBusService
orderService: OrderService
sendgridService: any
}
class OrderConfirmationSubscriber {
protected readonly orderService_: OrderService
protected readonly sendgridService_: any
constructor({
eventBusService,
orderService,
sendgridService,
}: InjectedDependencies) {
this.orderService_ = orderService
this.sendgridService_ = sendgridService
eventBusService.subscribe(
"order.placed",
this.handleOrderConfirmation
)
}
handleOrderConfirmation = async (
data: Record<string, any>
) => {
const order = await this.orderService_.retrieve(data.id, {
// you can include other relations as well
relations: ["items"],
})
this.sendgridService_.sendEmail({
templateId: "order-confirmation",
from: "hello@medusajs.com",
to: order.email,
dynamic_template_data: {
// any data necessary for your template...
items: order.items,
status: order.status,
},
})
}
}
export default OrderConfirmationSubscriber
```
Notice that you should replace the values in the object passed to the `sendEmail` method:
- `templateId`: Should be the ID of your order confirmation email template in SendGrid.
- `from`: Should be the from email.
- `to`: Should be the email associated with the order.
- `data`: Should be an object holding any data that should be passed to your SendGrid email template.