Skip to main content
Skip to main content

NotificationService

constructor

Parameters

containerInjectedDependenciesRequired

Properties

manager_EntityManagerRequired
transactionManager_undefined | EntityManagerRequired
__container__anyRequired
subscribers_objectRequired

Default: {}

attachmentGenerator_unknownRequired

Default: null

container_InjectedDependencies & objectRequired
logger_LoggerRequired
notificationRepository_Repository<Notification>Required
notificationProviderRepository_Repository<NotificationProvider>Required
__configModule__Record<string, unknown>
__moduleDeclaration__Record<string, unknown>

Accessors

activeManager_

Returns

EntityManagerEntityManagerRequired

Methods

withTransaction

Parameters

transactionManagerEntityManager

Returns

thisthisRequired

shouldRetryTransaction_

Parameters

errRecord<string, unknown> | objectRequired

Returns

booleanbooleanRequired

atomicPhase_

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

TResultobjectRequired
TErrorobjectRequired

Parameters

work(transactionManager: EntityManager) => Promise<TResult>Required
the transactional work to be done
isolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>
the isolation level to be used for the work.
maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>
Potential error handler

Returns

PromisePromise<TResult>Required
the result of the transactional work

registerAttachmentGenerator

Registers an attachment generator to the service. The generator can be used to generate on demand invoices or other documents.

Parameters

serviceunknownRequired
the service to assign to the attachmentGenerator

Returns

voidvoidRequired
Registers an attachment generator to the service. The generator can be used to generate on demand invoices or other documents.

registerInstalledProviders

Takes a list of notification provider ids and persists them in the database.

Parameters

providerIdsstring[]Required
a list of provider ids

Returns

PromisePromise<void>Required
Takes a list of notification provider ids and persists them in the database.

list

Retrieves a list of notifications.

Parameters

selectorSelector<Notification>Required
the params to select the notifications by.
configFindConfig<Notification>Required
the configuration to apply to the query

Returns

PromisePromise<Notification[]>Required
the notifications that satisfy the query.

listAndCount

Retrieves a list of notifications and total count.

Parameters

selectorSelector<Notification>Required
the params to select the notifications by.
configFindConfig<Notification>Required
the configuration to apply to the query

Returns

PromisePromise<[Notification[], number]>Required
the notifications that satisfy the query as well as the count.

retrieve

Retrieves a notification with a given id

Parameters

idstringRequired
the id of the notification
configFindConfig<Notification>Required
the configuration to apply to the query

Default: {}

Returns

PromisePromise<Notification>Required
the notification

subscribe

Subscribes a given provider to an event.

Parameters

eventNamestringRequired
the event to subscribe to
providerIdstringRequired
the provider that the event will be sent to

Returns

voidvoidRequired
Subscribes a given provider to an event.

retrieveProvider_

Finds a provider with a given id. Will throw a NOT_FOUND error if the resolution fails.

Parameters

idstringRequired
the id of the provider

Returns

AbstractNotificationServiceobjectRequired
Overview :::note[Prerequisites] 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:
1import { AbstractNotificationService } from "@medusajs/medusa"2import { EntityManager } from "typeorm"3
4class EmailSenderService extends AbstractNotificationService {5  protected manager_: EntityManager6  protected transactionManager_: EntityManager7
8  sendNotification(9    event: string,10    data: unknown,11    attachmentGenerator: unknown12  ): Promise<{13      to: string;14      status: string;15      data: Record<string, unknown>;16    }> {17    throw new Error("Method not implemented.")18  }19  resendNotification(20    notification: unknown,21    config: unknown,22    attachmentGenerator: unknown23  ): Promise<{24      to: string;25      status: string;26      data: Record<string, unknown>;27    }> {28    throw new Error("Method not implemented.")29  }30
31}32
33export 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:
1class EmailSenderService extends AbstractNotificationService {2  static identifier = "email-sender"3  // ...4}

handleEvent

Handles an event by relaying the event data to the subscribing providers. The result of the notification send will be persisted in the database in order to allow for resends. Will log any errors that are encountered.

Parameters

eventNamestringRequired
the event to handle
dataRecord<string, unknown>Required
the data the event was sent with

Returns

PromisePromise<undefined | void | Notification[]>Required
the result of notification subscribed

send

Sends a notification, by calling the given provider's sendNotification method. Persists the Notification in the database.

Parameters

eventstringRequired
the name of the event
eventDataRecord<string, unknown>Required
the data the event was sent with
providerIdstringRequired
the provider that should handle the event.

Returns

PromisePromise<undefined | Notification>Required
the created notification

resend

Resends a notification by retrieving a prior notification and calling the underlying provider's resendNotification method.

Parameters

idstringRequired
the id of the notification
configFindConfig<Notification>Required
any configuration that might override the previous send

Default: {}

Returns

PromisePromise<Notification>Required
the newly created notification
Was this section helpful?