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