medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.21k stars 2.66k forks source link

Medusa Development Resources: Issue in Customers in Storefront - Medusa Development Resources #10127

Open omesh845 opened 1 week ago

omesh845 commented 1 week ago

What Medusa version and documentation are you using?

v2

Preliminary Checks

Issue Summary

Bug Report: customer.created Subscriber Not Triggering

Description

I've created a subscriber to trigger when a new customer is created using the customer.created event. It works fine when customers are created through the admin dashboard but does not trigger when customers are created via the API (Storefront).

In contrast, another subscriber I implemented for the order.placed event works as expected for all scenarios.

Code Examples

Here is the code for the customer.created subscriber:

import type { SubscriberArgs, SubscriberConfig } from "@medusajs/medusa";
import { Modules } from "@medusajs/utils";

export default async function customerCreatedHandler({
  event: { data },
  container,
}: SubscriberArgs<{ id: string }>) {
  const customerModuleService = container.resolve(Modules.CUSTOMER);
  const notificationModuleService = container.resolve(Modules.NOTIFICATION);

  const customer = await customerModuleService.retrieveCustomer(data.id);

  // Your email sending logic here
  console.log(`Customer created: ${customer.email}`);
}

export const config: SubscriberConfig = {
  event: "customer.created",
};

Expected Behavior

The customer.created event should trigger the subscriber when a customer is created via:

  1. The admin dashboard.
  2. The Storefront API.

Actual Behavior

The event only triggers when a customer is created using the admin dashboard. When creating a customer via the Storefront API:

Steps to Reproduce

  1. Implement the above customer.created subscriber in subscribers/test-sub.ts.
  2. Create a customer using the admin dashboard → Event triggers correctly.
  3. Create a customer via the Storefront API → Event does not trigger.

Additional Information

The following subscriber for order.placed works as expected in all scenarios:

import { Modules } from "@medusajs/utils";
import { INotificationModuleService, IOrderModuleService } from "@medusajs/types";
import { SubscriberArgs, SubscriberConfig } from "@medusajs/medusa";
import { EmailTemplates } from "../modules/email-notifications/templates";

export default async function orderPlacedHandler({
  event: { data },
  container,
}: SubscriberArgs<any>) {
  const notificationModuleService: INotificationModuleService = container.resolve(Modules.NOTIFICATION);
  const orderModuleService: IOrderModuleService = container.resolve(Modules.ORDER);

  const order = await orderModuleService.retrieveOrder(data.id, {
    relations: ["items", "summary", "shipping_address"],
  });
  const shippingAddress = await (orderModuleService as any).orderAddressService_.retrieve(order.shipping_address.id);

  try {
    await notificationModuleService.createNotifications({
      to: order.email,
      channel: "email",
      template: EmailTemplates.ORDER_PLACED,
      data: {
        emailOptions: {
          replyTo: "info@example.com",
          subject: "Your order has been placed",
        },
        order,
        shippingAddress,
        preview: "Thank you for your order!",
      },
    });
  } catch (error) {
    console.error("Error sending order confirmation notification:", error);
  }
}

export const config: SubscriberConfig = {
  event: "order.placed",
};

Questions

  1. Is there a difference in how the customer.created event is emitted for the admin dashboard vs. Storefront API?
  2. Am I missing any configuration for the subscriber to work with the Storefront API?

Environment

How can this issue be resolved?

  1. Ensure the Event is Emitted Correctly for customer.created via Stormfront API
  2. if I'm Missing Something, please let me know

Are you interested in working on this issue?

tadinski commented 6 days ago

experiencing same issue

tadinski commented 4 hours ago

any updates on this issue?