Skip to main content

Workflows

createReviewWorkflow

This documentation provides a reference to the createReviewWorkflow. It belongs to the @mercurjs/reviews package. This workflow creates a review for a product or seller. It validates that the customer owns the order and hasn’t already reviewed the same reference, creates the review, links it to the product or seller and customer, and emits an event for search index updates. It exposes a hook for custom extensions. Source code

Examples

packages/modules/reviews/src/api/store/reviews/route.ts
import {
  AuthenticatedMedusaRequest,
  MedusaResponse,
} from "@medusajs/framework";
import { ContainerRegistrationKeys } from "@medusajs/framework/utils";
import { createReviewWorkflow } from "../../../workflows/review/workflows";
import { StoreCreateReviewType } from "./validators";

export const POST = async (
  req: AuthenticatedMedusaRequest<StoreCreateReviewType>,
  res: MedusaResponse
) => {
  const { result } = await createReviewWorkflow.run({
    container: req.scope,
    input: {
      ...req.validatedBody,
      customer_id: req.auth_context.actor_id,
    },
  });

  const query = req.scope.resolve(ContainerRegistrationKeys.QUERY);

  const {
    data: [review],
  } = await query.graph({
    entity: "review",
    fields: req.queryConfig.fields,
    filters: {
      id: result.id,
    },
  });

  res.status(201).json({ review });
};

Steps

  • validateReviewStep: Validates that the customer owns the order and hasn’t already reviewed this reference.
  • createReviewStep: Creates the review record and links it to customer and order.
  • createRemoteLinkStep: Links the review to the product or seller based on the reference type.
  • emitEventStep: Emits a review.changed event for search index updates.

Hooks

  • reviewCreated: Hook that fires after a review is created, providing the review_id.

Input

input
CreateReviewDTO
required
The review creation details.

Output

ReviewDTO
ReviewDTO
The created review.

deleteReviewWorkflow

This documentation provides a reference to the deleteReviewWorkflow. It belongs to the @mercurjs/reviews package. This workflow soft deletes a review. It exposes a hook for custom extensions. Source code

Examples

packages/modules/reviews/src/api/store/reviews/[id]/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from '@medusajs/framework'
import { deleteReviewWorkflow } from '../../../../workflows/review/workflows'

export const DELETE = async (
  req: AuthenticatedMedusaRequest,
  res: MedusaResponse
) => {
  const { id } = req.params
  await deleteReviewWorkflow.run({
    container: req.scope,
    input: id
  })

  res.json({
    id,
    object: 'review',
    deleted: true
  })
}

Steps

Hooks

  • reviewDeleted: Hook that fires after a review is deleted, providing the review_id.

Input

id
string
required
The ID of the review to delete.

Output

string
string
The ID of the deleted review.

updateReviewWorkflow

This documentation provides a reference to the updateReviewWorkflow. It belongs to the @mercurjs/reviews package. This workflow updates a review’s rating, customer note, or seller note. It emits an event for search index updates and exposes a hook for custom extensions. Source code

Examples

packages/modules/reviews/src/api/store/reviews/[id]/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from '@medusajs/framework'
import { ContainerRegistrationKeys } from '@medusajs/framework/utils'
import { updateReviewWorkflow } from '../../../../workflows/review/workflows'
import { StoreUpdateReviewType } from '../validators'

export const POST = async (
  req: AuthenticatedMedusaRequest<StoreUpdateReviewType>,
  res: MedusaResponse
) => {
  const { id } = req.params
  const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)

  await updateReviewWorkflow.run({
    container: req.scope,
    input: { id, ...req.validatedBody }
  })

  const {
    data: [review]
  } = await query.graph({
    entity: 'review',
    fields: req.queryConfig.fields,
    filters: {
      id
    }
  })

  res.json({
    review
  })
}

Steps

  • updateReviewStep: Updates the review record.
  • emitEventStep: Emits a review.changed event for search index updates.

Hooks

  • reviewUpdated: Hook that fires after a review is updated, providing the review_id.

Input

input
UpdateReviewDTO
required
The review update details.

Output

ReviewDTO
ReviewDTO
The updated review.

Steps

createReviewStep

Creates a review record and links it to the customer and order. The step creates the links using the Link service to establish relationships with the customer and order entities. Source code

Input

input
CreateReviewDTO
required
The review creation details.

Output

ReviewDTO
ReviewDTO
The created review.

deleteReviewStep

Soft deletes a review record from the database. Source code

Input

id
string
required
The ID of the review to delete.

Output

string
string
The ID of the deleted review.

updateReviewStep

Updates a review record with new rating, customer note, or seller note. Source code

Input

input
UpdateReviewDTO
required
The review update details.

Output

ReviewDTO
ReviewDTO
The updated review.

validateReviewStep

Validates a review creation by checking:
  1. The customer owns the order being reviewed
  2. The customer hasn’t already reviewed the same product or seller for this order
Source code

Input

reviewToCreate
CreateReviewDTO
required
The review data to validate.

Output

void
void
Throws an INVALID_DATA error if:
  • Order doesn’t belong to the customer
  • Review already exists for this reference on this order