createReviewWorkflow to create a review outside the built-in POST /store/reviews route, for example in a custom checkout follow-up, an import script, or a seed. The workflow validates the order, creates the review as pending, and writes its four links.
Before you begin
You need the following:- A customer and an order: the order’s
customer_idmust match the customer you pass - A target id: a product id for a product review, or a seller id for a store review
- Mercur core installed: the Review module ships in
@mercurjs/core, so there’s nothing else to register
1
Add the route
Create a route that reads the signed-in customer and runs the workflow:The route always creates a product review. Pass
src/api/store/order-reviews/route.ts
reference: "seller" with a seller id in reference_id to rate a store instead. customer_note is optional and defaults to null.2
Require a signed-in customer
Custom This is the same middleware the built-in Store review routes use.
/store routes aren’t authenticated by default. Add the authenticate middleware so auth_context.actor_id holds the customer id:src/api/middlewares.ts
3
Call the route
Send a request as the customer who placed the order:The response holds the new review with
status: "pending".Validate the rating and the target
createReviewWorkflow checks only that the order belongs to the customer and that the order hasn’t reviewed this target yet. It stores any rating you pass and doesn’t check that the product or seller was part of the order. Validate both before you run it:
src/api/store/order-reviews/route.ts
MedusaError and ContainerRegistrationKeys from @medusajs/framework/utils. Put this block before the workflow call.
Handle a rejected submission
The workflow throws aMedusaError with type INVALID_DATA in two cases, and Medusa returns them as 400 responses:
- “Order not found!”: the order doesn’t exist or belongs to another customer
- “Review already exists”: this order already has a review with the same
referenceand target id