INotificationService
Overview
Before creating a Notification Provider, install an event bus module.
A Notification Provider is a provider that handles sending and resending of notifications.
To create a Notification Provider, create a TypeScript or JavaScript file in src/services
. The name of the file is the name of the provider
(for example, sendgrid.ts
). The file must export a class that extends the AbstractNotificationService
class imported from @medusajs/medusa
.
For example, create the file src/services/email-sender.ts
with the following content:
import { AbstractNotificationService } from "@medusajs/medusa"
import { EntityManager } from "typeorm"
class EmailSenderService extends AbstractNotificationService {
protected manager_: EntityManager
protected transactionManager_: EntityManager
sendNotification(
event: string,
data: unknown,
attachmentGenerator: unknown
): Promise<{
to: string;
status: string;
data: Record<string, unknown>;
}> {
throw new Error("Method not implemented.")
}
resendNotification(
notification: unknown,
config: unknown,
attachmentGenerator: unknown
): Promise<{
to: string;
status: string;
data: Record<string, unknown>;
}> {
throw new Error("Method not implemented.")
}
}
export default EmailSenderService
Identifier Property
The NotificationProvider
entity has 2 properties: identifier
and is_installed
. The value of the identifier
property in the notification provider
class is used when the Notification Provider is created in the database.
The value of this property is also used later when you want to subscribe the Notification Provider to events in a Loader.
For example:
Properties
manager_
EntityManagerRequiredtransactionManager_
undefined | EntityManagerRequired__container__
anyRequired__configModule__
Record<string, unknown>__moduleDeclaration__
Record<string, unknown>Accessors
activeManager_
Returns
EntityManager
EntityManagerRequiredMethods
sendNotification
When an event is triggered that your Notification Provider is registered as a handler for, the NotificationService
in the Medusa backend executes this method of your Notification Provider.
In this method, you can perform the necessary operation to send the Notification. For example, you can send an email to the customer when they place an order.
Example
class EmailSenderService extends AbstractNotificationService {
// ...
async sendNotification(
event: string,
data: any,
attachmentGenerator: unknown
): Promise<{
to: string;
status: string;
data: Record<string, unknown>;
}> {
if (event === "order.placed") {
// retrieve order
const order = await this.orderService.retrieve(data.id)
// TODO send email
console.log("Notification sent")
return {
to: order.email,
status: "done",
data: {
// any data necessary to send the email
// for example:
subject: "You placed a new order!",
items: order.items,
},
}
}
}
// ...
}
Parameters
event
stringRequiredorder.placed
.data
unknownRequiredorder.placed
event is triggered,
the eventData
object contains the property id
which is the ID of the order that was placed. You can refer to the
Events reference for information on all events and their payloads.attachmentGenerator
unknownRequiredNotificationService
using the
registerAttachmentGenerator
method,
you have access to it here. You can use the attachmentGenerator
to generate on-demand invoices or other documents. The default value of this parameter is null
.Returns
The sending details.
resendNotification
This method is used to resend notifications, which is typically triggered by the Resend Notification API Route.
Example
class EmailSenderService extends AbstractNotificationService {
// ...
async resendNotification(
notification: any,
config: any,
attachmentGenerator: unknown
): Promise<{
to: string;
status: string;
data: Record<string, unknown>;
}> {
// check if the receiver should be changed
const to: string = config.to || notification.to
// TODO resend the notification using the same data
// that is saved under notification.data
console.log("Notification resent")
return {
to,
status: "done",
data: notification.data, // make changes to the data
}
}
}
Parameters
notification
unknownRequiredsendNotification
. It includes the to
and data
attributes which are populated originally using the to
and data
properties of
the object you return in sendNotification.config
unknownRequiredto
field. If specified, it will be available in this config object.attachmentGenerator
unknownRequiredNotificationService
using the
registerAttachmentGenerator
method,
you have access to it here. You can use the attachmentGenerator
to generate on-demand invoices or other documents. The default value of this parameter is null
.Returns
The resend details.
withTransaction
Parameters
transactionManager
EntityManagerReturns
this
thisRequiredshouldRetryTransaction_
Parameters
err
Record<string, unknown> | objectRequiredReturns
boolean
booleanRequiredatomicPhase_
Wraps some work within a transactional block. If the service already has a transaction manager attached this will be reused, otherwise a new transaction manager is created.
Type Parameters
TResult
objectRequiredTError
objectRequiredParameters
work
(transactionManager: EntityManager) => Promise<TResult>RequiredisolationOrErrorHandler
IsolationLevel | (error: TError) => Promise<void | TResult>maybeErrorHandlerOrDontFail
(error: TError) => Promise<void | TResult>Returns
Promise
Promise<TResult>Requiredthe result of the transactional work
Promise
Promise<TResult>Required