Option edits

This commit is contained in:
Sebastian Rindom committed 2020-07-04 19:36:22 +02:00
1 parent e9c80e80a4
commit 45e0338c58
2 files changed
+20 -15

No files matched your search

@@ -4,7 +4,7 @@ export default async (req, res) => {
const { id } = req.params const { id } = req.params
const schema = Validator.object().keys({ const schema = Validator.object().keys({
option_title: Validator.string().required(), title: Validator.string().required(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
if (error) { if (error) {
@@ -13,18 +13,21 @@ export default async (req, res) => {
try { try {
const productService = req.scope.resolve("productService") 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, [ const data = await productService.decorate(
"title", newProduct,
"description", [
"tags", "title",
"handle", "description",
"images", "tags",
"options", "handle",
"variants", "images",
"published", "options",
]) "published",
],
["variants"]
)
res.json({ product: data }) res.json({ product: data })
} catch (err) { } catch (err) {
throw err throw err
+5 -3
View File
@@ -467,7 +467,7 @@ class ProductService extends BaseService {
async updateOption(productId, optionId, data) { async updateOption(productId, optionId, data) {
const product = await this.retrieve(productId) 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) { if (!option) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.NOT_FOUND, MedusaError.Types.NOT_FOUND,
@@ -477,7 +477,8 @@ class ProductService extends BaseService {
const { title, values } = data const { title, values } = data
const titleExists = product.options.some( const titleExists = product.options.some(
o => o.title.toUpperCase() === title.toUpperCase() o =>
o.title.toUpperCase() === title.toUpperCase() && !o._id.equals(optionId)
) )
if (titleExists) { if (titleExists) {
@@ -567,13 +568,14 @@ class ProductService extends BaseService {
) )
.then(result => { .then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result) this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
}) })
.catch(err => { .catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message) throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
}) })
// If we reached this point, we can delete option value from variants // If we reached this point, we can delete option value from variants
if (product.variants) { if (product.variants.length) {
await Promise.all( await Promise.all(
product.variants.map(async variantId => product.variants.map(async variantId =>
this.productVariantService_.deleteOptionValue(variantId, optionId) this.productVariantService_.deleteOptionValue(variantId, optionId)