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 module service"
sidebarTitle: "Service"
description: "Resolve the Review module service, its generated CRUD methods, and its three rating helpers."
---

The Review module service, `ReviewModuleService`, reads and writes `Review` records directly and computes average ratings. Use it in subscribers, scheduled jobs, and routes when you don’t need a workflow’s validation, links, or compensation.

## Resolve the service

Resolve the service from the Medusa container with `MercurModules.REVIEW`, which is the string `"review"`:

```ts
import { MercurModules } from "@mercurjs/types"

const reviewService = container.resolve(MercurModules.REVIEW)

const [reviews, count] = await reviewService.listAndCountReviews(
  { status: "published" },
  { order: { created_at: "DESC" }, take: 20 }
)
```

This lists the 20 newest published reviews and the total count. Filters apply to the review’s own fields, so you can’t filter by product or seller here: use `query.graph` from the linked side, as shown in the [Links reference](/platform/review/reference/links).

<Warning>
  Writing through the service skips the workflow checks. `createReviews` doesn’t validate the order, reject duplicates, or write any link, and `updateReviews` doesn’t stop a second store answer.
</Warning>

## Generated methods

`MedusaService` generates these methods for the `Review` model:

| Method | Description |
| --- | --- |
| `retrieveReview(id, config?)` | Retrieve one review. Throws when it doesn’t exist |
| `listReviews(filters?, config?)` | List reviews |
| `listAndCountReviews(filters?, config?)` | List reviews and the total count |
| `createReviews(data)` | Create one review or an array of reviews |
| `updateReviews(data)` | Update by `id`, or by `{ selector, data }` |
| `deleteReviews(ids)` | Delete reviews permanently |
| `softDeleteReviews(ids)` | Set `deleted_at` on reviews |
| `restoreReviews(ids)` | Clear `deleted_at` on reviews |

Each write emits a mutation event, listed in the [Events reference](/platform/review/reference/events).

## getAvgRating

Returns the average rating of one product or one seller.

```ts
getAvgRating(
  type: "seller" | "product",
  id: string
): Promise<string | null>
```

The result is the Postgres `AVG` of `rating` as a numeric string, such as `"4.5000000000000000"`, or `null` when the target has no linked reviews. It counts every linked review whatever its `status`, including soft deleted ones.

## getProductsWithRating

Returns every product with its average rating.

```ts
getProductsWithRating(fields: string[]): Promise<Record<string, unknown>[]>
```

`fields` are column names of the `product` table, such as `["id", "title"]`. Each row gains a `rating` numeric string, or `null`. The query left joins the `product_product_review_review` link table and groups by product, with no filters, pagination, or `deleted_at` checks.

## getSellersWithRating

Returns every seller with its average rating.

```ts
getSellersWithRating(fields: string[]): Promise<Record<string, unknown>[]>
```

`fields` are column names of the `seller` table, such as `["id", "name"]`. It works like `getProductsWithRating`, joining through `seller_seller_review_review`.

<Note>
  The return types above describe the rows. The source declares no explicit return type for the two list helpers, so TypeScript infers Knex’s result type.
</Note>