docs: add section on testing providers (#9453)
This commit is contained in:
@@ -184,3 +184,61 @@ module.exports = defineConfig({
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Make sure to specify the correct channels for your provider in the `channels` option.
|
||||
|
||||
---
|
||||
|
||||
## 5. Test it Out
|
||||
|
||||
### Create Subscriber
|
||||
|
||||
To test out the provider, create a subscriber at `src/subscribers/user-created.ts` with the following content:
|
||||
|
||||
```ts title="src/subscribers/user-created.ts"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
import {
|
||||
SubscriberArgs,
|
||||
type SubscriberConfig,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export default async function userCreatedHandler({
|
||||
event: { data },
|
||||
container,
|
||||
}: SubscriberArgs<{ id: string }>) {
|
||||
const notificationModuleService = container.resolve(
|
||||
Modules.NOTIFICATION
|
||||
)
|
||||
const userModule = container.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
const user = await userModule.retrieveUser(data.id)
|
||||
|
||||
await notificationModuleService.createNotifications({
|
||||
to: user.email,
|
||||
channel: "email",
|
||||
template: "new-user"
|
||||
})
|
||||
}
|
||||
|
||||
export const config: SubscriberConfig = {
|
||||
event: "user.created",
|
||||
}
|
||||
```
|
||||
|
||||
In the subscriber, you resolve the Notification and User modules. Then, you use the User Module's main service to retrieve the user's details.
|
||||
|
||||
Finally, you use the Notification Module's main service to send a notification to the user's email through the `email` channel (assuming that's your provider's channel).
|
||||
|
||||
Make sure to replace the value of `template` to the ID of the template in your provider.
|
||||
|
||||
### Create User
|
||||
|
||||
Use the following command to create a user:
|
||||
|
||||
```bash
|
||||
npx medusa user -e admin@test.com -p supersecret
|
||||
```
|
||||
|
||||
After the user is created, the subscriber is executed, sending the notification using your provider.
|
||||
|
||||
Reference in New Issue
Block a user