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.

2.5 KB
Raw Blame
---
title: "Product Edit events"
sidebarTitle: "Events"
description: "Events emitted as product changes are staged and resolved, and when an operator requests a revision."
---

Product change workflows emit events after they commit, so you can run side effects, such as notifying a seller or reindexing search, without polling. The change events are in `ProductChangeWorkflowEvents`, and the revision request event is in `ProductWorkflowEvents`, both exported from `@mercurjs/core/workflows`.

## Change events

Each change event carries the id of one change. A workflow that handles several changes emits one event per change.

| Event | Emitted when | Payload |
| --- | --- | --- |
| `product-change.created` | `stageProductChangeWorkflow` or `createProductChangeWorkflow` creates a change | `{ id }` |
| `product-change.confirmed` | `confirmProductChangeWorkflow` confirms a change, including auto-confirm | `{ id }` |
| `product-change.declined` | `rejectProductChangeWorkflow` declines a change | `{ id }` |
| `product-change.canceled` | `cancelProductChangeWorkflow` cancels a change | `{ id }` |

Audit changes from `recordProductAuditChangeWorkflow` emit no change event. An auto-confirmed change emits `product-change.created` and then `product-change.confirmed`.

## Revision request event

The revision request event comes from `requestProductChangeWorkflow`. Its `id` is the product id, because the request is recorded on the product rather than resolving a change.

| Event | Emitted when | Payload |
| --- | --- | --- |
| `product.change-requested` | An operator requests a revision on a `proposed` product | `{ id, message, actor_id }` |

## Subscribe to an event

This subscriber loads a confirmed change with its actions:

```ts title="src/subscribers/product-change-confirmed.ts"
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"

export default async function handler({
  event,
  container,
}: SubscriberArgs<{ id: string }>) {
  const query = container.resolve("query")

  const { data: [change] } = await query.graph({
    entity: "product_change",
    fields: ["product_id", "created_by", "actions.action"],
    filters: { id: event.data.id },
  })

  // notify change.created_by that change.product_id was updated
}

export const config: SubscriberConfig = {
  event: "product-change.confirmed",
}
```

`created_by` tells you who to notify: a seller id for edits from the Vendor panel. To react to declines with the reason, read `declined_reason` the same way from a `product-change.declined` subscriber.