Product and variant creation
This commit is contained in:
1 parent
ae372de305
commit
ea0c563bd6
3 files changed
+50
-3
No files matched your search
@@ -5,10 +5,32 @@ export default async (req, res) => {
|
|||||||
title: Validator.string().required(),
|
title: Validator.string().required(),
|
||||||
description: Validator.string(),
|
description: Validator.string(),
|
||||||
tags: Validator.string(),
|
tags: Validator.string(),
|
||||||
|
options: Validator.array().items({
|
||||||
|
title: Validator.string().required(),
|
||||||
|
}),
|
||||||
images: Validator.array().items(Validator.string()),
|
images: Validator.array().items(Validator.string()),
|
||||||
variants: Validator.array().items(Validator.string()),
|
variants: Validator.array().items({
|
||||||
|
title: Validator.string().required(),
|
||||||
|
sku: Validator.string(),
|
||||||
|
ean: Validator.string(),
|
||||||
|
prices: Validator.array()
|
||||||
|
.items({
|
||||||
|
currency_code: Validator.string().required(),
|
||||||
|
amount: Validator.number().required(),
|
||||||
|
})
|
||||||
|
.required(),
|
||||||
|
options: Validator.array()
|
||||||
|
.items({
|
||||||
|
value: Validator.string().required(),
|
||||||
|
})
|
||||||
|
.default([]),
|
||||||
|
inventory_quantity: Validator.number().optional(),
|
||||||
|
allow_backorder: Validator.boolean().optional(),
|
||||||
|
manage_inventory: Validator.boolean().optional(),
|
||||||
|
metadata: Validator.object().optional(),
|
||||||
|
}),
|
||||||
metadata: Validator.object(),
|
metadata: Validator.object(),
|
||||||
handle: Validator.string().required(),
|
handle: Validator.string(),
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = schema.validate(req.body)
|
const { value, error } = schema.validate(req.body)
|
||||||
@@ -17,8 +39,31 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const { variants } = value
|
||||||
|
delete value.variants
|
||||||
|
|
||||||
const productService = req.scope.resolve("productService")
|
const productService = req.scope.resolve("productService")
|
||||||
let newProduct = await productService.createDraft(value)
|
let newProduct = await productService.createDraft(value)
|
||||||
|
|
||||||
|
if (variants) {
|
||||||
|
const optionIds = value.options.map(
|
||||||
|
o => newProduct.options.find(newO => newO.title === o.title)._id
|
||||||
|
)
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
variants.map(v => {
|
||||||
|
const variant = {
|
||||||
|
...v,
|
||||||
|
options: v.options.map((o, index) => ({
|
||||||
|
...o,
|
||||||
|
option_id: optionIds[index],
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
return productService.createVariant(newProduct._id, variant)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
newProduct = await productService.decorate(newProduct, [
|
newProduct = await productService.decorate(newProduct, [
|
||||||
"title",
|
"title",
|
||||||
"description",
|
"description",
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ class ProductVariantModel extends BaseModel {
|
|||||||
title: { type: String, required: true },
|
title: { type: String, required: true },
|
||||||
prices: { type: [MoneyAmountSchema], default: [], required: true },
|
prices: { type: [MoneyAmountSchema], default: [], required: true },
|
||||||
options: { type: [OptionValueSchema], default: [] },
|
options: { type: [OptionValueSchema], default: [] },
|
||||||
|
sku: { type: String, unique: true, sparse: true },
|
||||||
|
ean: { type: String, unique: true, sparse: true },
|
||||||
image: { type: String, default: "" },
|
image: { type: String, default: "" },
|
||||||
published: { type: Boolean, default: false },
|
published: { type: Boolean, default: false },
|
||||||
inventory_quantity: { type: Number, default: 0 },
|
inventory_quantity: { type: Number, default: 0 },
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class ProductModel extends BaseModel {
|
|||||||
title: { type: String, required: true },
|
title: { type: String, required: true },
|
||||||
description: { type: String, default: "" },
|
description: { type: String, default: "" },
|
||||||
tags: { type: String, default: "" },
|
tags: { type: String, default: "" },
|
||||||
handle: { type: String, required: true, unique: true },
|
handle: { type: String, unique: true, sparse: true },
|
||||||
images: { type: [String], default: [] },
|
images: { type: [String], default: [] },
|
||||||
options: { type: [OptionSchema], default: [] },
|
options: { type: [OptionSchema], default: [] },
|
||||||
variants: { type: [String], default: [] },
|
variants: { type: [String], default: [] },
|
||||||
|
|||||||
Reference in new issue
Block a user