product editing

This commit is contained in:
Sebastian Rindom
2020-07-04 12:16:28 +02:00
parent 04d2d707e9
commit c4cebce250
5 changed files with 104 additions and 17 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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({

View File

@@ -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_