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.

fix(product-attribute): mirror upserted attribute values onto shared variant-axis option (#1283)

Viktor Holikdbed8b632fc2
diff --git a/integration-tests/http/product-attribute/admin/product-attribute.spec.ts b/integration-tests/http/product-attribute/admin/product-attribute.spec.ts
index c1fd73ea1..7f3d61459 100644
--- a/integration-tests/http/product-attribute/admin/product-attribute.spec.ts
+++ b/integration-tests/http/product-attribute/admin/product-attribute.spec.ts
@@ -162,6 +162,51 @@ medusaIntegrationTestRunner({
expect(byName.Linen).toBeUndefined()
})
+ it("values upsert: mirrors late-added values onto the global variant-axis option", async () => {
+ const query = appContainer.resolve(ContainerRegistrationKeys.QUERY)
+ const created = await api.post(
+ "/admin/product-attributes",
+ {
+ name: "Weight",
+ type: "multi_select",
+ is_variant_axis: true,
+ values: [{ name: "1g" }],
+ },
+ adminHeaders,
+ )
+ const attrId = created.data.product_attribute.id
+
+ const optionValues = async () => {
+ const {
+ data: [attr],
+ } = await query.graph({
+ entity: "product_attribute",
+ fields: ["product_option_id"],
+ filters: { id: attrId },
+ })
+ const { data } = await query.graph({
+ entity: "product_option",
+ fields: ["values.value"],
+ filters: { id: attr.product_option_id },
+ })
+ return ((data?.[0]?.values ?? []) as { value: string }[]).map(
+ (v) => v.value,
+ )
+ }
+
+ expect(await optionValues()).toEqual(expect.arrayContaining(["1g"]))
+
+ await api.post(
+ `/admin/product-attributes/${attrId}/values`,
+ { values: [{ name: "100g" }, { name: "250g" }] },
+ adminHeaders,
+ )
+
+ expect(await optionValues()).toEqual(
+ expect.arrayContaining(["1g", "100g", "250g"]),
+ )
+ })
+
it("value update + delete sub-routes", async () => {
const created = await api.post(
"/admin/product-attributes",
diff --git a/packages/core/src/workflows/product-attribute/workflows/upsert-product-attribute-values.ts b/packages/core/src/workflows/product-attribute/workflows/upsert-product-attribute-values.ts
index aa52b6dac..728dd21da 100644
--- a/packages/core/src/workflows/product-attribute/workflows/upsert-product-attribute-values.ts
+++ b/packages/core/src/workflows/product-attribute/workflows/upsert-product-attribute-values.ts
@@ -4,9 +4,15 @@ import {
createWorkflow,
StepResponse,
transform,
+ when,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import {
+ updateProductOptionsStep,
+ useQueryGraphStep,
+} from "@medusajs/medusa/core-flows"
+import {
+ AttributeType,
MercurModules,
ProductAttributeValueDTO,
UpsertProductAttributeValueDTO,
@@ -25,7 +31,6 @@ type UpsertStepInput = (UpsertProductAttributeValueDTO & {
attribute_id: string
})[]
-// Known gap: rename-in-place does not re-sync the mirrored option.
const upsertProductAttributeValuesStep = createStep(
upsertProductAttributeValuesStepId,
async (data: UpsertStepInput, { container }) => {
@@ -52,8 +57,72 @@ export const upsertProductAttributeValuesWorkflowId =
export const upsertProductAttributeValuesWorkflow = createWorkflow(
upsertProductAttributeValuesWorkflowId,
function (input: UpsertProductAttributeValuesWorkflowInput) {
- const rows = transform({ input }, ({ input }) =>
- input.values.map((v) => ({ ...v, attribute_id: input.attribute_id })),
+ const attributeQuery = useQueryGraphStep({
+ entity: "product_attribute",
+ filters: { id: input.attribute_id },
+ fields: [
+ "id",
+ "name",
+ "type",
+ "is_variant_axis",
+ "product_option_id",
+ "values.name",
+ ],
+ options: { isList: false },
+ })
+
+ // Global variant-axis attributes back a shared Medusa ProductOption. Values
+ // added or renamed through this endpoint must be mirrored onto it, otherwise
+ // variant create fails with "Option value X does not exist for option Y".
+ const mirroredOption = when({ attributeQuery }, ({ attributeQuery }) => {
+ const attribute = attributeQuery.data
+ return (
+ attribute?.type === AttributeType.MULTI_SELECT &&
+ !!attribute?.is_variant_axis &&
+ !!attribute?.product_option_id
+ )
+ }).then(() => {
+ const optionUpdate = transform(
+ { attributeQuery, input },
+ ({ attributeQuery, input }) => {
+ const attribute = attributeQuery.data
+ const existing = (attribute.values ?? []).map(
+ (v: { name: string }) => v.name,
+ )
+ const incoming = input.values
+ .map((v) => v.name)
+ .filter((name): name is string => !!name)
+ return {
+ selector: { id: attribute.product_option_id },
+ update: { values: Array.from(new Set([...existing, ...incoming])) },
+ }
+ },
+ )
+
+ return updateProductOptionsStep(optionUpdate)
+ })
+
+ const rows = transform(
+ { input, mirroredOption },
+ ({ input, mirroredOption }) => {
+ const option = mirroredOption?.[0]
+ const idByValue = new Map<string, string>(
+ (option?.values ?? []).map((ov: { id: string; value: string }) => [
+ ov.value,
+ ov.id,
+ ]),
+ )
+ return input.values.map((v) => {
+ const product_option_value_id = v.name
+ ? idByValue.get(v.name)
+ : undefined
+ return {
+ ...v,
+ attribute_id: input.attribute_id,
+ ...(product_option_value_id ? { product_option_value_id } : {}),
+ }
+ })
+ },
)
const values = upsertProductAttributeValuesStep(rows)