Skip to main content

Workflows

createAttributePossibleValuesWorkflow

This documentation provides a reference to the createAttributePossibleValuesWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow creates one or more possible values for an attribute. Source code

Examples

packages/modules/b2c-core/src/api/admin/attributes/[id]/values/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework";
import { createAttributePossibleValuesWorkflow } from "../../../../../workflows/attribute/workflows/create-attribute-possible-values";
import { CreateAttributeValueDTO } from "@mercurjs/framework";

export const POST = async (
  req: AuthenticatedMedusaRequest<Omit<CreateAttributeValueDTO, 'attribute_id'>>,
  res: MedusaResponse
) => {
  const attributeId = req.params.id;

  const { result } = await createAttributePossibleValuesWorkflow(req.scope).run({
    input: [
      {
        ...req.validatedBody,
        attribute_id: attributeId,
      },
    ],
  });

  res.json({ attribute_value: result[0] });
};

Steps

Input

input
CreateAttributeValueDTO[]
required
The list of attribute values to create.

Output

AttributePossibleValueDTO[]
AttributePossibleValueDTO[]
The created attribute possible values.

createAttributeValueWorkflow

This documentation provides a reference to the createAttributeValueWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow creates an attribute value for a product and links it to the product. It handles the creation of the attribute value and the remote link between the product and the attribute value. It also includes validation to ensure the value is allowed for the attribute and that the product is associated with the attribute’s categories. Source code

Examples

packages/modules/b2c-core/src/api/vendor/products/[id]/options/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework";
import { createAttributeValueWorkflow } from "../../../../../workflows/attribute/workflows/create-attribute-value";
import { CreateProductAttributeValueDTO } from "@mercurjs/framework";

export const POST = async (
  req: AuthenticatedMedusaRequest<Omit<CreateProductAttributeValueDTO, 'product_id'>>,
  res: MedusaResponse
) => {
  const productId = req.params.id;

  const { result } = await createAttributeValueWorkflow(req.scope).run({
    input: {
      ...req.validatedBody,
      product_id: productId,
    },
  });

  res.json({ attribute_value: result });
};

Steps

Input

input
CreateProductAttributeValueDTO
required
The details of the attribute value to create and link.

Output

AttributeValueDTO
AttributeValueDTO
The created attribute value.

createAttributesWorkflow

This documentation provides a reference to the createAttributesWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow creates one or more attributes and optionally links them to product categories. Source code

Examples

packages/modules/b2c-core/src/api/admin/attributes/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework";
import { createAttributesWorkflow } from "../../../workflows/attribute/workflows/create-attributes";
import { CreateAttributeDTO } from "@mercurjs/framework";

export const POST = async (
  req: AuthenticatedMedusaRequest<CreateAttributeDTO>,
  res: MedusaResponse
) => {
  const { result } = await createAttributesWorkflow(req.scope).run({
    input: {
      attributes: [
        {
          ...req.validatedBody,
          product_category_ids: req.validatedBody.product_category_ids,
        },
      ],
    },
  });

  res.json({ attribute: result[0] });
};

Steps

  • createAttributesStep: Creates the attributes.
  • createRemoteLinkStep: Links the created attributes to the specified product categories.

Input

input
object
required

Output

AttributeDTO[]
AttributeDTO[]
The created attributes.

deleteAttributeValueWorkflow

This documentation provides a reference to the deleteAttributeValueWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow deletes one or more attribute values (the actual assigned values to products, not the definition possible values) and cleans up the links to products. Source code

Examples

packages/modules/b2c-core/src/api/vendor/products/[id]/options/[option_id]/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework";
import { deleteAttributeValueWorkflow } from "../../../../../../workflows/attribute/workflows/delete-attribute-value";

export const DELETE = async (
  req: AuthenticatedMedusaRequest,
  res: MedusaResponse
) => {
  const optionId = req.params.option_id;

  await deleteAttributeValueWorkflow(req.scope).run({
    input: optionId,
  });

  res.status(200).json({
    id: optionId,
    object: "attribute_value",
    deleted: true,
  });
};

Steps

  • useQueryGraphStep: Fetches the links between the attribute values and products to be dismissed.
  • deleteAttributeValueStep: Soft deletes the attribute values.
  • dismissRemoteLinkStep: Removes the links between the attribute values and products.

Input

input
string | string[]
required
The ID or IDs of the attribute values to delete.

Output

string[]
string[]
The IDs of the deleted attribute values.

deleteAttributeWorkflow

This documentation provides a reference to the deleteAttributeWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow deletes an attribute definition. Source code

Examples

packages/modules/b2c-core/src/api/admin/attributes/[id]/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework";
import { deleteAttributeWorkflow } from "../../../../workflows/attribute/workflows/delete-attribute";

export const DELETE = async (
  req: AuthenticatedMedusaRequest,
  res: MedusaResponse
) => {
  const attributeId = req.params.id;

  await deleteAttributeWorkflow(req.scope).run({
    input: {
      id: attributeId,
    },
  });

  res.status(200).json({
    id: attributeId,
    object: "attribute",
    deleted: true,
  });
};

Steps

Input

input
object
required

Output

void
void
The workflow does not return a value.

updateAttributePossibleValueWorkflow

This documentation provides a reference to the updateAttributePossibleValueWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow updates a possible value definition of an attribute (e.g., changing “Blue” to “Navy”). Source code

Examples

packages/modules/b2c-core/src/api/admin/attributes/[id]/values/[value_id]/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework";
import { updateAttributePossibleValueWorkflow } from "../../../../../../workflows/attribute/workflows/update-attribute-possible-value";
import { UpdateAttributeValueDTO } from "@mercurjs/framework";

export const POST = async (
  req: AuthenticatedMedusaRequest<UpdateAttributeValueDTO>,
  res: MedusaResponse
) => {
  const valueId = req.params.value_id;

  const { result } = await updateAttributePossibleValueWorkflow(req.scope).run({
    input: {
      ...req.validatedBody,
      id: valueId,
    },
  });

  res.json({ attribute_value: result });
};

Steps

Input

input
UpdateAttributeValueDTO
required
The details to update.

Output

AttributePossibleValueDTO
AttributePossibleValueDTO
The updated attribute possible value.

updateAttributesWorkflow

This documentation provides a reference to the updateAttributesWorkflow. It belongs to the @mercurjs/b2c-core package. This workflow updates one or more attributes, including their details, possible values, and category associations. Source code

Examples

packages/modules/b2c-core/src/api/admin/attributes/[id]/route.ts
import { AuthenticatedMedusaRequest, MedusaResponse } from "@medusajs/framework";
import { updateAttributesWorkflow } from "../../../../workflows/attribute/workflows/update-attributes";
import { AdminUpdateAttributeType } from "../../validators";

export const POST = async (
  req: AuthenticatedMedusaRequest<AdminUpdateAttributeType>,
  res: MedusaResponse
) => {
  const attributeId = req.params.id;

  await updateAttributesWorkflow(req.scope).run({
    input: {
      attributes: [
        {
          ...req.validatedBody,
          id: attributeId,
          product_category_ids:
            req.validatedBody.product_category_ids?.map((id) => ({ id })) ||
            undefined,
        },
      ],
    },
  });

  // ... fetch and return updated attribute
};

Steps

  • updateAttributesStep: Updates the attributes and their possible values.
  • useQueryGraphStep: Fetches current category links to determine what to add or remove.
  • dismissRemoteLinkStep: Removes links to categories that are no longer associated.
  • createRemoteLinkStep: Creates links to newly associated categories.

Input

input
object
required

Output

AttributeDTO[]
AttributeDTO[]
The updated attributes.

Steps

createAttributePossibleValuesStep

Creates one or more attribute possible values. Source code

Input

data
CreateAttributeValueDTO[]
required
List of values to create.

Output

AttributePossibleValueDTO[]
AttributePossibleValueDTO[]
The created values.

createAttributeValueStep

Creates an attribute value (assigned value). Source code

Input

input
Omit<CreateProductAttributeValueDTO, 'product_id'>
required
The value data (excluding product ID which is linked separately).

Output

AttributeValueDTO
AttributeValueDTO
The created value.

createAttributesStep

Creates one or more attributes. It validates that no attribute with the same name exists. Source code

Input

data
Omit<CreateAttributeDTO, 'product_category_ids'>[]
required
List of attributes to create.

Output

AttributeDTO[]
AttributeDTO[]
The created attributes.

deleteAttributeValueStep

Soft deletes attribute values. Source code

Input

ids
string[]
required
IDs of the values to delete.

Output

string[]
string[]
The IDs of deleted values.

deleteAttributeStep

Soft deletes an attribute and removes its links. Source code

Input

input
object
required

Output

void
void
No return value.

updateAttributePossibleValueStep

Updates a single attribute possible value. Source code

Input

payload
UpdateAttributeValueDTO
required
The update data.

Output

AttributePossibleValueDTO
AttributePossibleValueDTO
The updated value.

updateAttributesStep

Updates attributes and their possible values. Uses upsert/replace logic for values. Source code

Input

data
UpdateAttributeDTO[]
required
List of attributes to update.

Output

AttributeDTO[]
AttributeDTO[]
The updated attributes.

validateAttributeValueStep

Validates that a value is allowed for a given attribute and product. It checks:
  1. If the value is in the attribute’s possible_values (if restricted).
  2. If the product belongs to a category allowed by the attribute.
  3. If the product already has a value for this attribute (duplicate check).
Source code

Input

input
CreateProductAttributeValueDTO
required
The value creation payload to validate.

Output

void
void
Throws an error if validation fails.