From 548f6c7138d9a08b6c2113ebdda27f13dee848ac Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Mon, 25 Oct 2021 18:48:56 +0200 Subject: [PATCH] fix: make contentful data sync --- .../src/services/contentful.js | 33 ++++++++++++++----- packages/medusa/src/repositories/product.ts | 4 +++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index 210abfd082..edb59143a9 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -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] } diff --git a/packages/medusa/src/repositories/product.ts b/packages/medusa/src/repositories/product.ts index dae618da4e..e6e5df9724 100644 --- a/packages/medusa/src/repositories/product.ts +++ b/packages/medusa/src/repositories/product.ts @@ -55,6 +55,10 @@ export class ProductRepository extends Repository { return [] } + if (relations.length === 0) { + return this.findByIds(entitiesIds, idsOrOptionsWithoutRelations) + } + const groupedRelations: { [toplevel: string]: string[] } = {} for (const rel of relations) { const [topLevel] = rel.split(".")