This commit is contained in:
Sebastian Rindom committed 2020-05-16 11:01:29 +02:00
1 parent 420c1d8cdb
commit 36c12fa66d
7 files changed
+32 -22

No files matched your search

@@ -4,8 +4,8 @@ export default async (req, res) => {
const { id } = req.params const { id } = req.params
const schema = Validator.object().keys({ const schema = Validator.object().keys({
optionId: Validator.objectId().required(), option_id: Validator.objectId().required(),
optionValue: Validator.string().required(), value: Validator.string().required(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -17,8 +17,8 @@ export default async (req, res) => {
const productVariantService = req.scope.resolve("productVariantService") const productVariantService = req.scope.resolve("productVariantService")
const productVariant = await productVariantService.addOptionValue( const productVariant = await productVariantService.addOptionValue(
id, id,
value.optionId, value.option_id,
value.optionValue value.value
) )
res.status(200).json(productVariant) res.status(200).json(productVariant)
@@ -4,9 +4,15 @@ export default async (req, res) => {
const schema = Validator.object().keys({ const schema = Validator.object().keys({
title: Validator.string().required(), title: Validator.string().required(),
prices: Validator.array() prices: Validator.array()
.items({}) .items({
currency_code: Validator.string().required(),
amount: Validator.number().required(),
})
.required(), .required(),
options: Validator.array().items({}), options: Validator.array().items({
option_id: Validator.objectId().required(),
value: Validator.string().required(),
}),
image: Validator.string().optional(), image: Validator.string().optional(),
inventory_quantity: Validator.number().optional(), inventory_quantity: Validator.number().optional(),
allow_backorder: Validator.boolean().optional(), allow_backorder: Validator.boolean().optional(),
@@ -6,10 +6,16 @@ export default async (req, res) => {
const schema = Validator.object().keys({ const schema = Validator.object().keys({
title: Validator.string().optional(), title: Validator.string().optional(),
prices: Validator.array() prices: Validator.array()
.items({}) .items({
currency_code: Validator.string().required(),
amount: Validator.number().required(),
})
.optional(), .optional(),
options: Validator.array() options: Validator.array()
.items({}) .items({
option_id: Validator.objectId().required(),
value: Validator.string().required(),
})
.optional(), .optional(),
image: Validator.string().optional(), image: Validator.string().optional(),
inventory_quantity: Validator.number().optional(), inventory_quantity: Validator.number().optional(),
@@ -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({
optionTitle: Validator.string().required(), option_title: Validator.string().required(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
if (error) { if (error) {
@@ -13,10 +13,9 @@ export default async (req, res) => {
try { try {
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const product = await productService.retrieve(id) const newProduct = await productService.addOption(id, value.option_title)
await productService.addOption(product._id, value.optionTitle)
let newProduct = await productService.retrieve(product._id) const data = await productService.decorate(newProduct, [
newProduct = await productService.decorate(newProduct, [
"title", "title",
"description", "description",
"tags", "tags",
@@ -26,7 +25,7 @@ export default async (req, res) => {
"variants", "variants",
"published", "published",
]) ])
res.json(newProduct) res.json(data)
} catch (err) { } catch (err) {
console.log(err) console.log(err)
throw err throw err
@@ -3,10 +3,8 @@ export default async (req, res) => {
try { try {
const productService = req.scope.resolve("productService") const productService = req.scope.resolve("productService")
const product = await productService.retrieve(id) const product = await productService.addVariant(id, variantId)
await productService.addVariant(product._id, variantId) const data = await productService.decorate(product, [
let newProduct = await productService.retrieve(product._id)
newProduct = await productService.decorate(newProduct, [
"title", "title",
"description", "description",
"tags", "tags",
@@ -16,7 +14,7 @@ export default async (req, res) => {
"variants", "variants",
"published", "published",
]) ])
res.json(newProduct) res.json(data)
} catch (err) { } catch (err) {
throw err throw err
} }
@@ -61,6 +61,7 @@ class ProductVariantService extends BaseService {
return variant return variant
} }
// TODO: Validate productVariant
/** /**
* Creates an unpublished product variant. * Creates an unpublished product variant.
* @param {object} variant - the variant to create * @param {object} variant - the variant to create
+3 -3
View File
@@ -184,7 +184,7 @@ class ProductService extends BaseService {
} }
product.options.forEach(option => { product.options.forEach(option => {
if (!variant.options.find(vo => vo.option_id === option._id)) { if (!variant.options.find(vo => option._id.equals(vo.option_id))) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, MedusaError.Types.INVALID_DATA,
`Variant options do not contain value for ${option.title}` `Variant options do not contain value for ${option.title}`
@@ -414,11 +414,11 @@ class ProductService extends BaseService {
async deleteOption(productId, optionId) { async deleteOption(productId, optionId) {
const product = await this.retrieve(productId) const product = await this.retrieve(productId)
if (!product.options.find(o => o._id === optionId)) { if (!product.options.find(o => o._id.equals(optionId))) {
return Promise.resolve() return Promise.resolve()
} }
if (product.variants) { if (product.variants.length) {
// For the option we want to delete, make sure that all variants have the // For the option we want to delete, make sure that all variants have the
// same option values. The reason for doing is, that we want to avoid // same option values. The reason for doing is, that we want to avoid
// duplicate variants. For example, if we have a product with size and // duplicate variants. For example, if we have a product with size and