Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

3.3 KB
Raw Blame
---
title: "Review events"
sidebarTitle: "Events"
description: "The mutation events the Review module service emits, and how to subscribe to them."
---

Mercur defines no review events of its own, and the review workflows don’t call `emitEventStep`. The Review module service emits Medusa’s standard mutation events instead, because every method `MedusaService` generates publishes an event when it changes a `Review` row. Subscribe to these to notify a store, refresh a search index, or recompute a cached rating.

## Event list

Event names follow Medusa’s `<module>.<model>.<action>` pattern, with the module and the model both named `review`:

| Event | Emitted when | Payload |
| --- | --- | --- |
| `review.review.created` | `createReviewWorkflow` or `createReviews` creates a review | `{ id }` |
| `review.review.updated` | `updateReviewWorkflow`, `respondReviewWorkflow`, or `updateReviews` changes a review | `{ id }` |
| `review.review.deleted` | `deleteReviewWorkflow` or `softDeleteReviews` soft deletes a review, or `deleteReviews` removes one | `{ id }` |
| `review.review.restored` | `restoreReviews` restores a soft deleted review, including a delete rollback | `{ id }` |

The payload holds only the review id. Load the review in the subscriber when you need its fields or links.

<Note>
  `review.review.updated` doesn’t say which field changed. A moderation, a customer edit, and a store answer all emit the same event, so compare against your own state when you need to tell them apart.
</Note>

## Subscribe to an event

Create a subscriber that loads the review and its links:

```ts title="src/subscribers/review-created.ts"
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"

export default async function reviewCreated({
  event,
  container,
}: SubscriberArgs<{ id: string }>) {
  const query = container.resolve(ContainerRegistrationKeys.QUERY)
  const { data: [review] } = await query.graph({
    entity: "review",
    fields: ["rating", "reference", "seller.email", "product.title"],
    filters: { id: event.data.id },
  })

  const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
  logger.info(`Review ${event.data.id} rated ${review.rating}`)
}

export const config: SubscriberConfig = { event: "review.review.created" }
```

This logs each new review. Replace the log with your side effect, such as a notification to `review.seller.email` for a store review.

## Emit a named event

To give a moderation step its own event, wrap `updateReviewWorkflow` in your own workflow and add Medusa’s `emitEventStep`:

```ts title="src/workflows/publish-review.ts"
import {
  createWorkflow,
  WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "@medusajs/medusa/core-flows"
import { updateReviewWorkflow } from "@mercurjs/core/workflows"

export const publishReviewWorkflow = createWorkflow(
  "publish-review",
  (input: { id: string }) => {
    const review = updateReviewWorkflow.runAsStep({
      input: { id: input.id, status: "published" },
    })
    emitEventStep({ eventName: "review.published", data: { id: input.id } })

    return new WorkflowResponse(review)
  }
)
```

`review.published` is a name you define, so subscribe to the same string.