Adds thumbnail to products
This commit is contained in:
@@ -23,6 +23,7 @@ export default async (req, res) => {
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"thumbnail",
|
||||
"options",
|
||||
"published",
|
||||
],
|
||||
|
||||
@@ -9,6 +9,7 @@ export default async (req, res) => {
|
||||
title: Validator.string().required(),
|
||||
}),
|
||||
images: Validator.array().items(Validator.string()),
|
||||
thumbnail: Validator.string().optional(),
|
||||
variants: Validator.array().items({
|
||||
title: Validator.string().required(),
|
||||
sku: Validator.string(),
|
||||
@@ -45,6 +46,7 @@ export default async (req, res) => {
|
||||
const productService = req.scope.resolve("productService")
|
||||
const shippingProfileService = req.scope.resolve("shippingProfileService")
|
||||
|
||||
value.thumbnail = value.thumbnail || value.images[0]
|
||||
let newProduct = await productService.createDraft(value)
|
||||
|
||||
if (variants) {
|
||||
@@ -70,16 +72,20 @@ export default async (req, res) => {
|
||||
const { _id } = await shippingProfileService.retrieveDefault()
|
||||
await shippingProfileService.addProduct(_id, newProduct._id)
|
||||
|
||||
newProduct = await productService.decorate(newProduct, [
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
"variants",
|
||||
"published",
|
||||
])
|
||||
newProduct = await productService.decorate(
|
||||
newProduct,
|
||||
[
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"thumbnail",
|
||||
"options",
|
||||
"published",
|
||||
],
|
||||
["variants"]
|
||||
)
|
||||
res.json({ product: newProduct })
|
||||
} catch (err) {
|
||||
throw err
|
||||
|
||||
@@ -31,16 +31,20 @@ export default async (req, res) => {
|
||||
try {
|
||||
const productService = req.scope.resolve("productService")
|
||||
const product = await productService.createVariant(id, value)
|
||||
const data = await productService.decorate(product, [
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
"variants",
|
||||
"published",
|
||||
])
|
||||
const data = await productService.decorate(
|
||||
product,
|
||||
[
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"thumbnail",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
"published",
|
||||
],
|
||||
["variants"]
|
||||
)
|
||||
res.json({ product: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
|
||||
@@ -11,6 +11,7 @@ export default async (req, res) => {
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"thumbnail",
|
||||
"images",
|
||||
"options",
|
||||
"published",
|
||||
|
||||
@@ -4,15 +4,20 @@ export default async (req, res) => {
|
||||
try {
|
||||
const productService = req.scope.resolve("productService")
|
||||
const product = await productService.deleteVariant(id, variant_id)
|
||||
const data = await productService.decorate(product, [
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
"published",
|
||||
], ["variants"])
|
||||
const data = await productService.decorate(
|
||||
product,
|
||||
[
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"thumbnail",
|
||||
"options",
|
||||
"published",
|
||||
],
|
||||
["variants"]
|
||||
)
|
||||
|
||||
res.json({
|
||||
variant_id,
|
||||
|
||||
@@ -10,6 +10,7 @@ export default async (req, res) => {
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"thumbnail",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
|
||||
@@ -11,6 +11,7 @@ export default async (req, res) => {
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"thumbnail",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
|
||||
@@ -6,16 +6,20 @@ export default async (req, res) => {
|
||||
const product = await productService.retrieve(id)
|
||||
await productService.publish(product._id)
|
||||
let publishedProduct = await productService.retrieve(product._id)
|
||||
publishedProduct = await productService.decorate(publishedProduct, [
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
"variants",
|
||||
"published",
|
||||
])
|
||||
publishedProduct = await productService.decorate(
|
||||
publishedProduct,
|
||||
[
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"thumbnail",
|
||||
"images",
|
||||
"options",
|
||||
"published",
|
||||
],
|
||||
["variants"]
|
||||
)
|
||||
res.json({ product: publishedProduct })
|
||||
} catch (error) {
|
||||
throw error
|
||||
|
||||
@@ -25,8 +25,8 @@ export default async (req, res) => {
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"thumbnail",
|
||||
"options",
|
||||
"variants",
|
||||
"published",
|
||||
],
|
||||
["variants"]
|
||||
|
||||
@@ -11,6 +11,7 @@ export default async (req, res) => {
|
||||
images: Validator.array()
|
||||
.items(Validator.string())
|
||||
.optional(),
|
||||
thumbnail: Validator.string().optional(),
|
||||
variants: Validator.array()
|
||||
.items({
|
||||
_id: Validator.string(),
|
||||
@@ -50,9 +51,13 @@ export default async (req, res) => {
|
||||
try {
|
||||
const productService = req.scope.resolve("productService")
|
||||
const oldProduct = await productService.retrieve(id)
|
||||
await productService.update(oldProduct._id, value)
|
||||
let newProduct = await productService.retrieve(oldProduct._id)
|
||||
newProduct = await productService.decorate(
|
||||
|
||||
if (!oldProduct.thumbnail && value.images) {
|
||||
value.thumbnail = value.thumbnail || value.images[0]
|
||||
}
|
||||
|
||||
const product = await productService.update(oldProduct._id, value)
|
||||
const data = await productService.decorate(
|
||||
newProduct,
|
||||
[
|
||||
"title",
|
||||
@@ -60,12 +65,13 @@ export default async (req, res) => {
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"thumbnail",
|
||||
"options",
|
||||
"published",
|
||||
],
|
||||
["variants"]
|
||||
)
|
||||
res.json({ product: newProduct })
|
||||
res.json({ product: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
@@ -76,16 +76,21 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
const product = await productService.retrieve(id)
|
||||
const data = await productService.decorate(product, [
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
"variants",
|
||||
"published",
|
||||
])
|
||||
const data = await productService.decorate(
|
||||
product,
|
||||
[
|
||||
"title",
|
||||
"description",
|
||||
"tags",
|
||||
"handle",
|
||||
"images",
|
||||
"options",
|
||||
"thumbnail",
|
||||
"variants",
|
||||
"published",
|
||||
],
|
||||
["variants"]
|
||||
)
|
||||
res.json({ product: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
|
||||
@@ -4,11 +4,8 @@ export default async (req, res) => {
|
||||
try {
|
||||
const fileService = req.scope.resolve("fileService")
|
||||
|
||||
console.log(fileService)
|
||||
|
||||
const result = await Promise.all(
|
||||
req.files.map(async f => {
|
||||
console.log(f)
|
||||
return fileService.upload(f).then(result => {
|
||||
fs.unlinkSync(f.path)
|
||||
return result
|
||||
|
||||
@@ -15,6 +15,7 @@ class ProductModel extends BaseModel {
|
||||
tags: { type: String, default: "" },
|
||||
handle: { type: String, unique: true, sparse: true },
|
||||
images: { type: [String], default: [] },
|
||||
thumbnail: { type: String, default: "" },
|
||||
options: { type: [OptionSchema], default: [] },
|
||||
variants: { type: [String], default: [] },
|
||||
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
|
||||
|
||||
Reference in New Issue
Block a user