Merge pull request #673 from medusajs/hotfix/contentful-sync

fix: make contentful data sync
This commit is contained in:
Sebastian Rindom
2021-10-25 19:22:45 +02:00
committed by GitHub
2 changed files with 29 additions and 8 deletions

View File

@@ -742,21 +742,35 @@ class ContentfulService extends BaseService {
const environment = await this.getContentfulEnvironment_()
const productEntry = await environment.getEntry(productId)
const product = await this.productService_.retrieve(productId)
const product = await this.productService_.retrieve(productId, {
select: [
"id",
"handle",
"title",
"subtitle",
"description",
"thumbnail",
],
})
const update = {}
const title =
productEntry.fields[this.getCustomField("title", "product")]["en-US"]
productEntry.fields[this.getCustomField("title", "product")]?.["en-US"]
const subtitle =
productEntry.fields[this.getCustomField("subtitle", "product")]["en-US"]
const description =
productEntry.fields[this.getCustomField("description", "product")][
productEntry.fields[this.getCustomField("subtitle", "product")]?.[
"en-US"
]
const description =
productEntry.fields[this.getCustomField("description", "product")]?.[
"en-US"
]
const handle =
productEntry.fields[this.getCustomField("handle", "product")]?.["en-US"]
if (product.title !== title) {
update.title = title
}
@@ -769,6 +783,10 @@ class ContentfulService extends BaseService {
update.description = description
}
if (product.handle !== handle) {
update.handle = handle
}
// Get the thumbnail, if present
if (productEntry.fields.thumbnail) {
const thumb = await environment.getAsset(
@@ -776,7 +794,7 @@ class ContentfulService extends BaseService {
)
if (thumb.fields.file["en-US"].url) {
if (!product.thumbnail.includes(thumb.fields.file["en-US"].url)) {
if (!product.thumbnail?.includes(thumb.fields.file["en-US"].url)) {
update.thumbnail = thumb.fields.file["en-US"].url
}
}
@@ -834,7 +852,6 @@ class ContentfulService extends BaseService {
output.push(transformed)
}
console.log(output)
if (!isArray) {
return output[0]
}

View File

@@ -55,6 +55,10 @@ export class ProductRepository extends Repository<Product> {
return []
}
if (relations.length === 0) {
return this.findByIds(entitiesIds, idsOrOptionsWithoutRelations)
}
const groupedRelations: { [toplevel: string]: string[] } = {}
for (const rel of relations) {
const [topLevel] = rel.split(".")