product editing
This commit is contained in:
1 parent
04d2d707e9
commit
c4cebce250
5 files changed
+104
-17
No files matched your search
@@ -3,11 +3,26 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const productService = req.scope.resolve("productService")
|
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({
|
res.json({
|
||||||
option_id,
|
option_id,
|
||||||
object: "option",
|
object: "option",
|
||||||
deleted: true,
|
deleted: true,
|
||||||
|
product: data,
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ export default async (req, res) => {
|
|||||||
"handle",
|
"handle",
|
||||||
"images",
|
"images",
|
||||||
"options",
|
"options",
|
||||||
"variants",
|
|
||||||
"published",
|
"published",
|
||||||
])
|
], ["variants"])
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
variant_id,
|
variant_id,
|
||||||
object: "product-variant",
|
object: "product-variant",
|
||||||
deleted: true,
|
deleted: true,
|
||||||
|
product: data,
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -12,7 +12,32 @@ export default async (req, res) => {
|
|||||||
.items(Validator.string())
|
.items(Validator.string())
|
||||||
.optional(),
|
.optional(),
|
||||||
variants: Validator.array()
|
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(),
|
.optional(),
|
||||||
metadata: Validator.object().optional(),
|
metadata: Validator.object().optional(),
|
||||||
})
|
})
|
||||||
@@ -27,15 +52,19 @@ export default async (req, res) => {
|
|||||||
const oldProduct = await productService.retrieve(id)
|
const oldProduct = await productService.retrieve(id)
|
||||||
await productService.update(oldProduct._id, value)
|
await productService.update(oldProduct._id, value)
|
||||||
let newProduct = await productService.retrieve(oldProduct._id)
|
let newProduct = await productService.retrieve(oldProduct._id)
|
||||||
newProduct = await productService.decorate(newProduct, [
|
newProduct = await productService.decorate(
|
||||||
"title",
|
newProduct,
|
||||||
"description",
|
[
|
||||||
"tags",
|
"title",
|
||||||
"handle",
|
"description",
|
||||||
"images",
|
"tags",
|
||||||
"options",
|
"handle",
|
||||||
"published",
|
"images",
|
||||||
], ["variants"])
|
"options",
|
||||||
|
"published",
|
||||||
|
],
|
||||||
|
["variants"]
|
||||||
|
)
|
||||||
res.json({ product: newProduct })
|
res.json({ product: newProduct })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ export default async (req, res) => {
|
|||||||
const { id, variant_id } = req.params
|
const { id, variant_id } = req.params
|
||||||
const schema = Validator.object().keys({
|
const schema = Validator.object().keys({
|
||||||
title: Validator.string().optional(),
|
title: Validator.string().optional(),
|
||||||
|
sku: Validator.string(),
|
||||||
|
ean: Validator.string(),
|
||||||
prices: Validator.array().items(
|
prices: Validator.array().items(
|
||||||
Validator.object()
|
Validator.object()
|
||||||
.keys({
|
.keys({
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ class ProductService extends BaseService {
|
|||||||
* @param {object} update - an object with the update values.
|
* @param {object} update - an object with the update values.
|
||||||
* @return {Promise} resolves to the update result.
|
* @return {Promise} resolves to the update result.
|
||||||
*/
|
*/
|
||||||
update(productId, update) {
|
async update(productId, update) {
|
||||||
const validatedId = this.validateId_(productId)
|
const validatedId = this.validateId_(productId)
|
||||||
|
|
||||||
if (update.metadata) {
|
if (update.metadata) {
|
||||||
@@ -130,10 +130,51 @@ class ProductService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (update.variants) {
|
if (update.variants) {
|
||||||
throw new MedusaError(
|
update.variants = await Promise.all(
|
||||||
MedusaError.Types.INVALID_DATA,
|
update.variants.map(async variant => {
|
||||||
"Use addVariant, reorderVariants, removeVariant to update Product Variants"
|
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_
|
return this.productModel_
|
||||||
|
|||||||
Reference in new issue
Block a user