fix(medusa): Upsert product images (#200)

This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2021-03-12 10:09:01 +01:00
1 parent 0601b34765
commit a031f1f338
5 files changed
+84 -6

No files matched your search

+1 -1
View File
@@ -46,7 +46,7 @@ export class Product {
@Column({ default: false })
is_giftcard: boolean
@ManyToMany(() => Image)
@ManyToMany(() => Image, { cascade: ["insert"] })
@JoinTable({
name: "product_images",
joinColumn: {
@@ -0,0 +1,5 @@
import { EntityRepository, Repository } from "typeorm"
import { Image } from "../models/image"
@EntityRepository(Image)
export class ImageRepository extends Repository<Image> {}
+42 -3
View File
@@ -23,6 +23,7 @@ class ProductService extends BaseService {
productCollectionService,
productTypeRepository,
productTagRepository,
imageRepository,
}) {
super()
@@ -52,6 +53,9 @@ class ProductService extends BaseService {
/** @private @const {ProductCollectionService} */
this.productTagRepository_ = productTagRepository
/** @private @const {ImageRepository} */
this.imageRepository_ = imageRepository
}
withTransaction(transactionManager) {
@@ -69,6 +73,7 @@ class ProductService extends BaseService {
productCollectionService: this.productCollectionService_,
productTagRepository: this.productTagRepository_,
productTypeRepository: this.productTypeRepository_,
imageRepository: this.imageRepository_,
})
cloned.transactionManager_ = transactionManager
@@ -233,7 +238,7 @@ class ProductService extends BaseService {
})
if (existing) {
return existing
return existing.id
}
const created = productTypeRepository.create(type)
@@ -277,10 +282,18 @@ class ProductService extends BaseService {
this.productOptionRepository_
)
const { options, tags, type, ...rest } = productObject
const { options, tags, type, images, ...rest } = productObject
if (!rest.thumbnail && images && images.length) {
rest.thumbnail = images[0]
}
let product = productRepo.create(rest)
if (images && images.length) {
product.images = await this.upsertImages_(images)
}
if (tags) {
product.tags = await this.upsertProductTags_(tags)
}
@@ -310,6 +323,28 @@ class ProductService extends BaseService {
})
}
async upsertImages_(images) {
const imageRepository = this.manager_.getCustomRepository(
this.imageRepository_
)
let productImages = []
for (const img of images) {
const existing = await imageRepository.findOne({
where: { url: img },
})
if (existing) {
productImages.push(existing)
} else {
const created = imageRepository.create({ url: img })
productImages.push(created)
}
}
return productImages
}
/**
* Updates a product. Product variant updates should use dedicated methods,
* e.g. `addVariant`, etc. The function will throw errors if metadata or
@@ -327,7 +362,7 @@ class ProductService extends BaseService {
)
const product = await this.retrieve(productId, {
relations: ["variants", "tags"],
relations: ["variants", "tags", "images"],
})
const {
@@ -344,6 +379,10 @@ class ProductService extends BaseService {
product.thumbnail = images[0]
}
if (images && images.length) {
product.images = await this.upsertImages_(images)
}
if (metadata) {
product.metadata = this.setMetadata_(product, metadata)
}