node.jsmedusajs

Can't send notification on 'order.placed' event in MedusaJS


After following the documentation, the Notification Provider is not sending the notification on 'order.placed' event in a clean new MedusaJS backend.

System information

Medusa version (including plugins):
"@medusajs/admin": "^6.0.5",
"@medusajs/cache-inmemory": "^1.8.7",
"@medusajs/cache-redis": "^1.8.7",
"@medusajs/event-bus-local": "^1.9.6",
"@medusajs/event-bus-redis": "^1.8.7",
"@medusajs/file-local": "^1.0.1",
"@medusajs/medusa": "^1.14.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^16.1.4",
"express": "^4.17.2",
"medusa-fulfillment-manual": "^1.1.37",
"medusa-interfaces": "^1.3.7",
"medusa-payment-manual": "^1.0.23",
"medusa-payment-stripe": "^6.0.3",
"typeorm": "^0.3.16"

Node.js version: 14.21.3
Database: Postgres
Operating system: Windows 10

Steps to reproduce the behavior

  1. Create Notification Provider in services/custom-email-sender.ts following https://docs.medusajs.com/development/notification/create-notification-provider:
import {
  AbstractNotificationService,
  Logger,
  OrderService,
} from "@medusajs/medusa";

class CustomEmailSenderService extends AbstractNotificationService {
  static identifier = "custom-email-sender";
  static is_installed = true;

  protected orderService_: OrderService;
  protected logger_: Logger;

  constructor(container, _options) {
    super(container);

    this.logger_ = container.logger;
    this.logger_.info("EMAIL SENDER v1");

    this.orderService_ = container.orderService;
  }

  async sendNotification(
    event: string,
    data: unknown,
    attachmentGenerator: unknown
  ): Promise<{
    to: string;
    status: string;
    data: Record<string, unknown>;
  }> {
    this.logger_.info("sendNotification");
    if (event === "order.placed") {
      // retrieve order
      const order = await this.orderService_.retrieve(data.id);
      // TODO send email

      this.logger_.info("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,
        },
      };
    }
  }

  resendNotification(
    notification: unknown,
    config: unknown,
    attachmentGenerator: unknown
  ): Promise<{
    to: string;
    status: string;
    data: Record<string, unknown>;
  }> {
    throw new Error("Method not implemented.");
  }
}

export default CustomEmailSenderService;

2.) Create a subscriber for the notification in subscribers/notification.js:

class NotificationSubscriber {
  constructor({ notificationService  }) {
    notificationService.subscribe(
        "order.placed",
        "custom-email-sender"
    )
  }
}

export default NotificationSubscriber;

3.) Build and run medusa develope:

npm run build npx medusa develop

4.) The provider is present in DB and is installed:

enter image description here

5.) Create and order and watch logs, the sendNotification is never called, there are no notifications created in DB:

Output of BE

I tried other events and the notification provider is not subscribing for anything.


Solution

  • It seems event-bus-local does not handle the wildcard events subscription for the notification service. Works fine with event-bus-redis.