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