Skip to content

fix(types,product): fix update product option value type #10883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/core/types/src/product/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,11 @@ export interface UpdateProductOptionDTO {
}

export interface UpdateProductOptionValueDTO {
/**
* The ID of the product option value.
*/
id: string

/**
* The value of the product option value.
*/
Expand Down
34 changes: 16 additions & 18 deletions packages/core/types/src/product/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1498,14 +1498,14 @@ export interface IProductModuleService extends IModuleService {
): Promise<ProductOptionValueDTO[]>

/**
* This method is used to create a product option.
* This method is used to create a product option value.
*
* @param {CreateProductOptionValueDTO} data - The product option to be created.
* @param {CreateProductOptionValueDTO} data - The product option value to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductOptionValueDTO>} The created product option.
* @returns {Promise<ProductOptionValueDTO>} The created product option value.
*
* @example
* const option = await productModuleService.createProductOptionValues({
* const optionValue = await productModuleService.createProductOptionValues({
* value: "Blue",
* option_id: "opt_123",
* })
Expand All @@ -1517,48 +1517,46 @@ export interface IProductModuleService extends IModuleService {
): Promise<ProductOptionValueDTO>

/**
* This method is used to update a option.
* This method is used to update an option value.
*
* @param {string} id - The ID of the option to be updated.
* @param {UpdateProductOptionValueDTO} data - The attributes of the option to be updated
* @param {UpdateProductOptionValueDTO} data - The option value data to be updated
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductOptionValueDTO>} The updated option.
* @returns {Promise<ProductOptionValueDTO>} The updated options values.
*
* @example
* const option = await productModuleService.updateProductOptionValues(
* "optval_123",
* const updatedOptionValue = await productModuleService.updateProductOptionValues(
* {
* id: "optval_test1",
* value: "Green",
* }
* )
*/
updateProductOptionValues(
id: string,
data: UpdateProductOptionValueDTO,
sharedContext?: Context
): Promise<ProductOptionValueDTO>

/**
* This method is used to update a list of options matching the specified filters.
* This method is used to update a list of option values
*
* @param {FilterableProductOptionValueProps} selector - The filters specifying which options to update.
* @param {UpdateProductOptionValueDTO} data - The attributes to be updated on the selected options
* @param {UpdateProductOptionValueDTO} data - An array of option value data to be updated
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductOptionValueDTO[]>} The updated options.
* @returns {Promise<ProductOptionValueDTO[]>} The updated options values.
*
* @example
* const options = await productModuleService.updateProductOptionValues(
* const updatedOptionValues = await productModuleService.updateProductOptionValues(
* {
* id: "optval_test1",
* value: "Green",
* },
* {
* id: "optval_test2",
* value: ["Red"],
* }
* )
*/
updateProductOptionValues(
selector: FilterableProductOptionValueProps,
data: UpdateProductOptionValueDTO,
data: UpdateProductOptionValueDTO[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this conflicting with the general convention for update* methods?

IIRC, we have:

update(id, data, context)
update(selector, data, context)

and

upsert(data[], context)
upsert(data, context)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/medusajs/medusa/blob/develop/packages/core/utils/src/modules-sdk/medusa-internal-service.ts#L230-L245

This is based on this ^ here from the internal service shapes

if we're exposing the selector shape, it would look more like this:

update(
  {
    selector: { id: "test" },
    data: { ... }
  },
  context
)

unless they're overridden in the service layer, which in this case its not

Copy link
Contributor

@olivermrbl olivermrbl Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand. This change is to the public interface of the product module and in those we typically have the conventions I described above. What makes the updateProductOptionValues method different from the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't different from the others. On the contrary, this is how most module methods handle updates:

update(data[], context)
update(data, context)

The convention you mention have been used in some places, but not everywhere. In the places where it has been used, the module service has been updated to accept this shape, not the abstract service.

If the intention is for the update to have this shape:

update(id, data, context)
update(selector, data, context)

Then we need to:

  1. Update the abstract public module service to accept this shape
  2. Refactor everywhere that uses this currently or accept both with one deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @olivermrbl should I close this PR?

sharedContext?: Context
): Promise<ProductOptionValueDTO[]>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
ProductStatus,
} from "@medusajs/framework/utils"
import {
ProductImage,
Product,
ProductCategory,
ProductCollection,
ProductImage,
ProductType,
} from "@models"

Expand Down Expand Up @@ -1312,12 +1312,15 @@ moduleIntegrationTestRunner<IProductModuleService>({
relations: ["images"],
})

const retrievedProductAgain = await service.retrieveProduct(product.id, {
relations: ["images"],
})
const retrievedProductAgain = await service.retrieveProduct(
product.id,
{
relations: ["images"],
}
)

expect(retrievedProduct.images).toEqual(retrievedProductAgain.images)

expect(retrievedProduct.images).toEqual(
Array.from({ length: 1000 }, (_, i) =>
expect.objectContaining({
Expand All @@ -1332,7 +1335,9 @@ moduleIntegrationTestRunner<IProductModuleService>({
// Explicitly verify sequential order
retrievedProduct.images.forEach((img, idx) => {
if (idx > 0) {
expect(img.rank).toBeGreaterThan(retrievedProduct.images[idx - 1].rank)
expect(img.rank).toBeGreaterThan(
retrievedProduct.images[idx - 1].rank
)
}
})
})
Expand Down Expand Up @@ -1384,6 +1389,39 @@ moduleIntegrationTestRunner<IProductModuleService>({
])
})
})

describe("updateProductOptionValues", function () {
it("should update product option value", async () => {
const data = buildProductAndRelationsData({
images: [],
tags: [],
})

const [createdProduct] = await service.createProducts([data])

const [product] = await service.listProducts(
{ id: createdProduct.id },
{
relations: ["variants.options.id"],
}
)

const optionValueId = product.variants[0].options[0].id

const productOptionValue = await service.updateProductOptionValues({
id: optionValueId,
metadata: { rank: 0 },
value: "test update",
})

expect(productOptionValue).toEqual(
expect.objectContaining({
metadata: { rank: 0 },
value: "test update",
})
)
})
})
})
},
})
Loading