Repository: medusajs/medusa
File: packages/core/core-flows/src/product/workflows/update-products.ts (TypeScript)

Code before:
      fields: ["variants.id"],
      variables: variantIdsSelector,
    }).config({ name: "get-previous-products-variants-step" })

    const previousVariantIds = transform(
      { previousProductsWithVariants },
      (data) => {
        return data.previousProductsWithVariants.flatMap((p) =>
          p.variants?.map((v) => v.id)
        )
      }
    )

    const toUpdateInput = transform({ input }, prepareUpdateProductInput)
    const updatedProducts = updateProductsStep(toUpdateInput)

    const variantsToDismissInventory = transform(
      { input, updatedProducts },
      (data) => {
        const variantIds: string[] = []

        if ("products" in data.input) {
          for (const product of data.input.products) {
            if (product.variants?.length) {
              variantIds.push(
                ...product.variants.filter((v) => v.id).map((v) => v.id!)
              )
            }
          }
        } else if (data.input.update?.variants?.length) {
          variantIds.push(
            ...data.input.update.variants
              .filter((v) => v.id && v.manage_inventory === false)
              .map((v) => v.id!)
          )
        }

        return variantIds
      }
    )

    dismissProductVariantsInventoryStep({
      variantIds: variantsToDismissInventory,
    })

    const salesChannelLinks = transform(
      { input, updatedProducts },
      prepareSalesChannelLinks
    )

    const shippingProfileLinks = transform(

Review comment (suggestion):
todo: should filter for `manage_inventory === false` here as well otherwise we will unlink all product variants from inventory on variant update

Code after:
      fields: ["variants.id"],
      variables: variantIdsSelector,
    }).config({ name: "get-previous-products-variants-step" })

    const previousVariantIds = transform(
      { previousProductsWithVariants },
      (data) => {
        return data.previousProductsWithVariants.flatMap((p) =>
          p.variants?.map((v) => v.id)
        )
      }
    )

    const toUpdateInput = transform({ input }, prepareUpdateProductInput)
    const updatedProducts = updateProductsStep(toUpdateInput)

    const variantsToDismissInventory = transform(
      { input, updatedProducts },
      (data) => {
        const variantIds: string[] = []

        if ("products" in data.input) {
          for (const product of data.input.products) {
            for (const variant of product.variants ?? []) {
              if (variant.id && variant.manage_inventory === false) {
                variantIds.push(variant.id)
              }
            }
          }
        } else if (data.input.update?.variants?.length) {
          for (const variant of data.input.update.variants) {
            if (variant.id && variant.manage_inventory === false) {
              variantIds.push(variant.id)
            }
          }
        }

        return variantIds
      }
    )

    dismissProductVariantsInventoryStep({
      variantIds: variantsToDismissInventory,
    })

    const salesChannelLinks = transform(
      { input, updatedProducts },
      prepareSalesChannelLinks
    )

    const shippingProfileLinks = transform(