diff --git a/packages/medusa/src/api/routes/admin/products/add-option.js b/packages/medusa/src/api/routes/admin/products/add-option.js index 550e8e6061..a03ac8f34f 100644 --- a/packages/medusa/src/api/routes/admin/products/add-option.js +++ b/packages/medusa/src/api/routes/admin/products/add-option.js @@ -4,7 +4,7 @@ export default async (req, res) => { const { id } = req.params const schema = Validator.object().keys({ - option_title: Validator.string().required(), + title: Validator.string().required(), }) const { value, error } = schema.validate(req.body) if (error) { @@ -13,18 +13,21 @@ export default async (req, res) => { try { const productService = req.scope.resolve("productService") - const newProduct = await productService.addOption(id, value.option_title) + const newProduct = await productService.addOption(id, value.title) - const data = await productService.decorate(newProduct, [ - "title", - "description", - "tags", - "handle", - "images", - "options", - "variants", - "published", - ]) + const data = await productService.decorate( + newProduct, + [ + "title", + "description", + "tags", + "handle", + "images", + "options", + "published", + ], + ["variants"] + ) res.json({ product: data }) } catch (err) { throw err diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index 61fe653300..d01ecb2dac 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -467,7 +467,7 @@ class ProductService extends BaseService { async updateOption(productId, optionId, data) { const product = await this.retrieve(productId) - const option = product.options.find(o => o._id === optionId) + const option = product.options.find(o => o._id.equals(optionId)) if (!option) { throw new MedusaError( MedusaError.Types.NOT_FOUND, @@ -477,7 +477,8 @@ class ProductService extends BaseService { const { title, values } = data const titleExists = product.options.some( - o => o.title.toUpperCase() === title.toUpperCase() + o => + o.title.toUpperCase() === title.toUpperCase() && !o._id.equals(optionId) ) if (titleExists) { @@ -567,13 +568,14 @@ class ProductService extends BaseService { ) .then(result => { this.eventBus_.emit(ProductService.Events.UPDATED, result) + return result }) .catch(err => { throw new MedusaError(MedusaError.Types.DB_ERROR, err.message) }) // If we reached this point, we can delete option value from variants - if (product.variants) { + if (product.variants.length) { await Promise.all( product.variants.map(async variantId => this.productVariantService_.deleteOptionValue(variantId, optionId)