diff --git a/packages/medusa-plugin-contentful/src/api/routes/contentful.js b/packages/medusa-plugin-contentful/src/api/routes/hooks/contentful.js similarity index 81% rename from packages/medusa-plugin-contentful/src/api/routes/contentful.js rename to packages/medusa-plugin-contentful/src/api/routes/hooks/contentful.js index 9eeb548550..7fb1392af5 100644 --- a/packages/medusa-plugin-contentful/src/api/routes/contentful.js +++ b/packages/medusa-plugin-contentful/src/api/routes/hooks/contentful.js @@ -2,16 +2,17 @@ export default async (req, res) => { try { const contentfulService = req.scope.resolve("contentfulService") - const contentfulType = req.body.contentType.sys.id + const contentfulType = req.body.sys.contentType.sys.id + const entryId = req.body.sys.id let updated = {} switch (contentfulType) { case "product": - updated = await contentfulService.sendContentfulProductToAdmin(req.body) + updated = await contentfulService.sendContentfulProductToAdmin(entryId) break case "productVariant": updated = await contentfulService.sendContentfulProductVariantToAdmin( - req.body + entryId ) break default: diff --git a/packages/medusa-plugin-contentful/src/api/routes/index.js b/packages/medusa-plugin-contentful/src/api/routes/hooks/index.js similarity index 100% rename from packages/medusa-plugin-contentful/src/api/routes/index.js rename to packages/medusa-plugin-contentful/src/api/routes/hooks/index.js diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index 35888782ae..b2b1f30068 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -32,10 +32,6 @@ class ContentfulService extends BaseService { return reject(err) } - if (reply) { - return reject("Missing key") - } - return resolve(JSON.parse(reply)) }) }) @@ -88,7 +84,7 @@ class ContentfulService extends BaseService { "en-US": product.title, }, variants: { - "en-US": this.getVariantLinks_(variantEntries), + "en-US": [], }, objectId: { "en-US": product._id, @@ -126,6 +122,17 @@ class ContentfulService extends BaseService { async updateProductInContentful(product) { try { + const ignoreIds = (await this.getIgnoreIds_("product")) || [] + + if (ignoreIds.includes(product._id)) { + const newIgnoreIds = ignoreIds.filter((id) => id !== product._id) + this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds)) + return + } else { + ignoreIds.push(product._id) + this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds)) + } + const environment = await this.getContentfulEnvironment_() // check if product exists let productEntry = undefined @@ -135,25 +142,18 @@ class ContentfulService extends BaseService { return this.createProductInContentful(product) } - const variantEntries = await this.getVariantEntries_(product.variants) + // const variantEntries = await this.getVariantEntries_(product.variants) productEntry.fields = _.assignIn(productEntry.fields, { title: { "en-US": product.title, }, variants: { - "en-US": this.getVariantLinks_(variantEntries), + "en-US": [], }, }) - await productEntry.update() - const publishedEntry = await productEntry.publish() - - const ignoreIds = await this.getIgnoreIds_("product") - if (ignoreIds.includes(publishedEntry.sys.id)) { - ignoreIds.filter((id) => id !== publishedEntry.sys.id) - } else { - this.eventBus_.emit("product.updated", publishedEntry) - } + const updatedEntry = await productEntry.update() + const publishedEntry = await updatedEntry.publish() return publishedEntry } catch (error) { @@ -191,6 +191,7 @@ class ContentfulService extends BaseService { const publishedEntry = await variantEntry.publish() const ignoreIds = await this.getIgnoreIds_("product_variant") + if (ignoreIds.includes(publishedEntry.sys.id)) { ignoreIds.filter((id) => id !== publishedEntry.sys.id) } else { @@ -203,25 +204,29 @@ class ContentfulService extends BaseService { } } - async sendContentfulProductToAdmin(product) { + async sendContentfulProductToAdmin(productId) { try { const environment = await this.getContentfulEnvironment_() - const productEntry = await environment.getEntry(product.sys.id) + const productEntry = await environment.getEntry(productId) - const ignoreIds = await this.getIgnoreIds_("product") - ignoreIds.push(product.sys.id) - this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds)) + const ignoreIds = (await this.getIgnoreIds_("product")) || [] + if (ignoreIds.includes(productId)) { + const newIgnoreIds = ignoreIds.filter((id) => id !== productId) + this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds)) + return + } else { + ignoreIds.push(productId) + this.redis_.set("product_ignore_ids", JSON.stringify([productId])) + } - const updatedProduct = await this.productService_.update( - productEntry.objectId, - { - title: productEntry.fields.title["en-US"], - variants: productEntry.fields.variants["en-US"], - } - ) + const updatedProduct = await this.productService_.update(productId, { + title: productEntry.fields.title["en-US"], + // variants: productEntry.fields.variants["en-US"], + }) return updatedProduct } catch (error) { + console.log(error) throw error } } diff --git a/packages/medusa-plugin-contentful/src/subscribers/contentful.js b/packages/medusa-plugin-contentful/src/subscribers/contentful.js index d6a696bdb3..7add5c474b 100644 --- a/packages/medusa-plugin-contentful/src/subscribers/contentful.js +++ b/packages/medusa-plugin-contentful/src/subscribers/contentful.js @@ -12,7 +12,7 @@ class ContentfulSubscriber { }) this.eventBus_.subscribe("product.created", async (data) => { - await this.contentfulService_.createProductVariantInContentful(data) + await this.contentfulService_.createProductInContentful(data) }) } }