Option edits
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user