diff --git a/packages/medusa/src/api/routes/admin/products/delete-option.js b/packages/medusa/src/api/routes/admin/products/delete-option.js index 5ee09ee263..7e450bcd24 100644 --- a/packages/medusa/src/api/routes/admin/products/delete-option.js +++ b/packages/medusa/src/api/routes/admin/products/delete-option.js @@ -3,11 +3,26 @@ export default async (req, res) => { try { const productService = req.scope.resolve("productService") - await productService.deleteOption(id, option_id) + const product = await productService.deleteOption(id, option_id) + const data = await productService.decorate( + product, + [ + "title", + "description", + "tags", + "handle", + "images", + "options", + "published", + ], + ["variants"] + ) + res.json({ option_id, object: "option", deleted: true, + product: data, }) } catch (err) { throw err diff --git a/packages/medusa/src/api/routes/admin/products/delete-variant.js b/packages/medusa/src/api/routes/admin/products/delete-variant.js index 99cbcf2ed2..6b27d24e8b 100644 --- a/packages/medusa/src/api/routes/admin/products/delete-variant.js +++ b/packages/medusa/src/api/routes/admin/products/delete-variant.js @@ -11,14 +11,14 @@ export default async (req, res) => { "handle", "images", "options", - "variants", "published", - ]) + ], ["variants"]) res.json({ variant_id, object: "product-variant", deleted: true, + product: data, }) } catch (err) { throw err diff --git a/packages/medusa/src/api/routes/admin/products/update-product.js b/packages/medusa/src/api/routes/admin/products/update-product.js index 204ad07ce4..9ad7fbf188 100644 --- a/packages/medusa/src/api/routes/admin/products/update-product.js +++ b/packages/medusa/src/api/routes/admin/products/update-product.js @@ -12,7 +12,32 @@ export default async (req, res) => { .items(Validator.string()) .optional(), variants: Validator.array() - .items(Validator.string()) + .items({ + _id: Validator.string(), + title: Validator.string().optional(), + sku: Validator.string().optional(), + ean: Validator.string().optional(), + prices: Validator.array().items( + Validator.object() + .keys({ + region_id: Validator.string(), + currency_code: Validator.string(), + amount: Validator.number().required(), + }) + .xor("region_id", "currency_code") + ), + options: Validator.array().items({ + option_id: Validator.objectId().required(), + value: Validator.alternatives( + Validator.string(), + Validator.number() + ).required(), + }), + inventory_quantity: Validator.number().optional(), + allow_backorder: Validator.boolean().optional(), + manage_inventory: Validator.boolean().optional(), + metadata: Validator.object().optional(), + }) .optional(), metadata: Validator.object().optional(), }) @@ -27,15 +52,19 @@ export default async (req, res) => { const oldProduct = await productService.retrieve(id) await productService.update(oldProduct._id, value) let newProduct = await productService.retrieve(oldProduct._id) - newProduct = await productService.decorate(newProduct, [ - "title", - "description", - "tags", - "handle", - "images", - "options", - "published", - ], ["variants"]) + newProduct = await productService.decorate( + newProduct, + [ + "title", + "description", + "tags", + "handle", + "images", + "options", + "published", + ], + ["variants"] + ) res.json({ product: newProduct }) } catch (err) { throw err diff --git a/packages/medusa/src/api/routes/admin/products/update-variant.js b/packages/medusa/src/api/routes/admin/products/update-variant.js index b25f4d5db2..cc8a23e2a2 100644 --- a/packages/medusa/src/api/routes/admin/products/update-variant.js +++ b/packages/medusa/src/api/routes/admin/products/update-variant.js @@ -5,6 +5,8 @@ export default async (req, res) => { const { id, variant_id } = req.params const schema = Validator.object().keys({ title: Validator.string().optional(), + sku: Validator.string(), + ean: Validator.string(), prices: Validator.array().items( Validator.object() .keys({ diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index bf999adbcc..06c18bfe47 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -119,7 +119,7 @@ class ProductService extends BaseService { * @param {object} update - an object with the update values. * @return {Promise} resolves to the update result. */ - update(productId, update) { + async update(productId, update) { const validatedId = this.validateId_(productId) if (update.metadata) { @@ -130,10 +130,51 @@ class ProductService extends BaseService { } if (update.variants) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - "Use addVariant, reorderVariants, removeVariant to update Product Variants" + update.variants = await Promise.all( + update.variants.map(async variant => { + if (variant._id) { + if (variant.prices && variant.prices.length) { + for (const price of variant.prices) { + if (price.region_id) { + await this.productVariantService_.setRegionPrice( + variant._id, + price.region_id, + price.amount + ) + } else { + await this.productVariantService_.setCurrencyPrice( + variant._id, + price.currency_code, + price.amount + ) + } + } + } + + if (variant.options && variant.options.length) { + for (const option of variant.options) { + await this.updateOptionValue( + productId, + variant._id, + option.option_id, + option.value + ) + } + } + + delete variant.prices + delete variant.options + + if (!_.isEmpty(variant)) { + await this.productVariantService_.update(variant._id, variant) + } + } else { + await this.createVariant(productId, variant).then(res => res._id) + } + }) ) + + delete update.variants } return this.productModel_