From eff9f4c6f91ee8826c58f4a07ce384425f152440 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Sun, 7 May 2023 13:01:12 +0200 Subject: [PATCH] chore(medusa-plugin-contentful): Cleanup plugin (#3990) --- .../src/services/contentful.js | 907 ++++++++---------- 1 file changed, 422 insertions(+), 485 deletions(-) diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index 6290f03ba6..a67ce55251 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -1,5 +1,5 @@ -import _ from "lodash" import { BaseService } from "medusa-interfaces" +import _ from "lodash" import { createClient } from "contentful-management" const IGNORE_THRESHOLD = 2 // seconds @@ -51,32 +51,24 @@ class ContentfulService extends BaseService { } async getContentfulEnvironment_() { - try { - const space = await this.contentful_.getSpace(this.options_.space_id) - const environment = await space.getEnvironment(this.options_.environment) - return environment - } catch (error) { - throw error - } + const space = await this.contentful_.getSpace(this.options_.space_id) + const environment = await space.getEnvironment(this.options_.environment) + return environment } async getVariantEntries_(variants, config = { publish: false }) { - try { - const contentfulVariants = await Promise.all( - variants.map(async (variant) => { - let updated = await this.updateProductVariantInContentful(variant) - if (config.publish) { - updated = updated.publish() - } + const contentfulVariants = await Promise.all( + variants.map(async (variant) => { + let updated = await this.updateProductVariantInContentful(variant) + if (config.publish) { + updated = updated.publish() + } - return updated - }) - ) + return updated + }) + ) - return contentfulVariants - } catch (error) { - throw error - } + return contentfulVariants } getLink_(fromEntry) { @@ -147,135 +139,123 @@ class ContentfulService extends BaseService { } async createProductInContentful(product) { - try { - const p = await this.productService_.retrieve(product.id, { - relations: [ - "variants", - "options", - "tags", - "type", - "collection", - "images", - ], - }) + const p = await this.productService_.retrieve(product.id, { + relations: [ + "variants", + "options", + "tags", + "type", + "collection", + "images", + ], + }) - const environment = await this.getContentfulEnvironment_() - const variantEntries = await this.getVariantEntries_(p.variants, { - publish: true, - }) - const variantLinks = this.getVariantLinks_(variantEntries) + const environment = await this.getContentfulEnvironment_() + const variantEntries = await this.getVariantEntries_(p.variants, { + publish: true, + }) + const variantLinks = this.getVariantLinks_(variantEntries) - const fields = { - [this.getCustomField("title", "product")]: { - "en-US": p.title, - }, - [this.getCustomField("subtitle", "product")]: { - "en-US": p.subtitle, - }, - [this.getCustomField("description", "product")]: { - "en-US": p.description, - }, - [this.getCustomField("variants", "product")]: { - "en-US": variantLinks, - }, - [this.getCustomField("options", "product")]: { - "en-US": this.transformMedusaIds(p.options), - }, - [this.getCustomField("medusaId", "product")]: { - "en-US": p.id, - }, - } - - if (p.images.length > 0) { - const imageLinks = await this.createImageAssets(product) - if (imageLinks) { - fields.images = { - "en-US": imageLinks, - } - } - } - - if (p.thumbnail) { - const thumbnailAsset = await environment.createAsset({ - fields: { - title: { - "en-US": `${p.title}`, - }, - description: { - "en-US": "", - }, - file: { - "en-US": { - contentType: "image/xyz", - fileName: p.thumbnail, - upload: p.thumbnail, - }, - }, - }, - }) - - await thumbnailAsset.processForAllLocales().then((a) => a.publish()) - - const thumbnailLink = { - sys: { - type: "Link", - linkType: "Asset", - id: thumbnailAsset.sys.id, - }, - } - - fields.thumbnail = { - "en-US": thumbnailLink, - } - } - - await this.createSpecialProductFields(fields, p) - - const result = await environment.createEntryWithId("product", p.id, { - fields, - }) - - return result - } catch (error) { - throw error + const fields = { + [this.getCustomField("title", "product")]: { + "en-US": p.title, + }, + [this.getCustomField("subtitle", "product")]: { + "en-US": p.subtitle, + }, + [this.getCustomField("description", "product")]: { + "en-US": p.description, + }, + [this.getCustomField("variants", "product")]: { + "en-US": variantLinks, + }, + [this.getCustomField("options", "product")]: { + "en-US": this.transformMedusaIds(p.options), + }, + [this.getCustomField("medusaId", "product")]: { + "en-US": p.id, + }, } + + if (p.images.length > 0) { + const imageLinks = await this.createImageAssets(product) + if (imageLinks) { + fields.images = { + "en-US": imageLinks, + } + } + } + + if (p.thumbnail) { + const thumbnailAsset = await environment.createAsset({ + fields: { + title: { + "en-US": `${p.title}`, + }, + description: { + "en-US": "", + }, + file: { + "en-US": { + contentType: "image/xyz", + fileName: p.thumbnail, + upload: p.thumbnail, + }, + }, + }, + }) + + await thumbnailAsset.processForAllLocales().then((a) => a.publish()) + + const thumbnailLink = { + sys: { + type: "Link", + linkType: "Asset", + id: thumbnailAsset.sys.id, + }, + } + + fields.thumbnail = { + "en-US": thumbnailLink, + } + } + + await this.createSpecialProductFields(fields, p) + + const result = await environment.createEntryWithId("product", p.id, { + fields, + }) + + return result } async createProductVariantInContentful(variant) { - try { - const v = await this.productVariantService_.retrieve(variant.id, { - relations: ["prices", "options"], - }) + const v = await this.productVariantService_.retrieve(variant.id, { + relations: ["prices", "options"], + }) - const environment = await this.getContentfulEnvironment_() - const result = await environment.createEntryWithId( - "productVariant", - v.id, - { - fields: { - [this.getCustomField("title", "variant")]: { - "en-US": v.title, - }, - [this.getCustomField("sku", "variant")]: { - "en-US": v.sku, - }, - [this.getCustomField("prices", "variant")]: { - "en-US": this.transformMedusaIds(v.prices), - }, - [this.getCustomField("options", "variant")]: { - "en-US": this.transformMedusaIds(v.options), - }, - [this.getCustomField("medusaId", "variant")]: { - "en-US": v.id, - }, - }, - } - ) + const environment = await this.getContentfulEnvironment_() + const result = await environment.createEntryWithId("productVariant", v.id, { + fields: { + [this.getCustomField("title", "variant")]: { + "en-US": v.title, + }, + [this.getCustomField("sku", "variant")]: { + "en-US": v.sku, + }, + [this.getCustomField("prices", "variant")]: { + "en-US": this.transformMedusaIds(v.prices), + }, + [this.getCustomField("options", "variant")]: { + "en-US": this.transformMedusaIds(v.options), + }, + [this.getCustomField("medusaId", "variant")]: { + "en-US": v.id, + }, + }, + }) - return result - } catch (error) { - throw error - } + return result } async createRegionInContentful(region) { @@ -285,39 +265,35 @@ class ContentfulService extends BaseService { if (!hasType) { return Promise.resolve() } - try { - const r = await this.regionService_.retrieve(region.id, { - relations: ["countries", "payment_providers", "fulfillment_providers"], - }) + const r = await this.regionService_.retrieve(region.id, { + relations: ["countries", "payment_providers", "fulfillment_providers"], + }) - const environment = await this.getContentfulEnvironment_() + const environment = await this.getContentfulEnvironment_() - const fields = { - [this.getCustomField("medusaId", "region")]: { - "en-US": r.id, - }, - [this.getCustomField("name", "region")]: { - "en-US": r.name, - }, - [this.getCustomField("countries", "region")]: { - "en-US": r.countries, - }, - [this.getCustomField("paymentProviders", "region")]: { - "en-US": r.payment_providers, - }, - [this.getCustomField("fulfillmentProviders", "region")]: { - "en-US": r.fulfillment_providers, - }, - } - - const result = await environment.createEntryWithId("region", r.id, { - fields, - }) - - return result - } catch (error) { - throw error + const fields = { + [this.getCustomField("medusaId", "region")]: { + "en-US": r.id, + }, + [this.getCustomField("name", "region")]: { + "en-US": r.name, + }, + [this.getCustomField("countries", "region")]: { + "en-US": r.countries, + }, + [this.getCustomField("paymentProviders", "region")]: { + "en-US": r.payment_providers, + }, + [this.getCustomField("fulfillmentProviders", "region")]: { + "en-US": r.fulfillment_providers, + }, } + + const result = await environment.createEntryWithId("region", r.id, { + fields, + }) + + return result } async updateRegionInContentful(data) { @@ -341,53 +317,49 @@ class ContentfulService extends BaseService { return } - try { - const ignore = await this.shouldIgnore_(data.id, "contentful") - if (ignore) { - return - } - - const r = await this.regionService_.retrieve(data.id, { - relations: ["countries", "payment_providers", "fulfillment_providers"], - }) - - const environment = await this.getContentfulEnvironment_() - - // check if region exists - let regionEntry = undefined - try { - regionEntry = await environment.getEntry(data.id) - } catch (error) { - return this.createRegionInContentful(r) - } - - const regionEntryFields = { - ...regionEntry.fields, - [this.getCustomField("name", "region")]: { - "en-US": r.name, - }, - [this.getCustomField("countries", "region")]: { - "en-US": r.countries, - }, - [this.getCustomField("paymentProviders", "region")]: { - "en-US": r.payment_providers, - }, - [this.getCustomField("fulfillmentProviders", "region")]: { - "en-US": r.fulfillment_providers, - }, - } - - regionEntry.fields = regionEntryFields - - const updatedEntry = await regionEntry.update() - const publishedEntry = await updatedEntry.publish() - - await this.addIgnore_(data.id, "medusa") - - return publishedEntry - } catch (error) { - throw error + const ignore = await this.shouldIgnore_(data.id, "contentful") + if (ignore) { + return } + + const r = await this.regionService_.retrieve(data.id, { + relations: ["countries", "payment_providers", "fulfillment_providers"], + }) + + const environment = await this.getContentfulEnvironment_() + + // check if region exists + let regionEntry = undefined + try { + regionEntry = await environment.getEntry(data.id) + } catch (error) { + return this.createRegionInContentful(r) + } + + const regionEntryFields = { + ...regionEntry.fields, + [this.getCustomField("name", "region")]: { + "en-US": r.name, + }, + [this.getCustomField("countries", "region")]: { + "en-US": r.countries, + }, + [this.getCustomField("paymentProviders", "region")]: { + "en-US": r.payment_providers, + }, + [this.getCustomField("fulfillmentProviders", "region")]: { + "en-US": r.fulfillment_providers, + }, + } + + regionEntry.fields = regionEntryFields + + const updatedEntry = await regionEntry.update() + const publishedEntry = await updatedEntry.publish() + + await this.addIgnore_(data.id, "medusa") + + return publishedEntry } async createCollectionInContentful(collection) { @@ -529,110 +501,106 @@ class ContentfulService extends BaseService { return Promise.resolve() } + const ignore = await this.shouldIgnore_(data.id, "contentful") + if (ignore) { + return Promise.resolve() + } + + const p = await this.productService_.retrieve(data.id, { + relations: [ + "options", + "variants", + "type", + "collection", + "tags", + "images", + ], + }) + + const environment = await this.getContentfulEnvironment_() + + // check if product exists + let productEntry = undefined try { - const ignore = await this.shouldIgnore_(data.id, "contentful") - if (ignore) { - return Promise.resolve() + productEntry = await environment.getEntry(data.id) + } catch (error) { + return this.createProductInContentful(p) + } + + const variantEntries = await this.getVariantEntries_(p.variants) + const variantLinks = this.getVariantLinks_(variantEntries) + + const productEntryFields = { + ...productEntry.fields, + [this.getCustomField("title", "product")]: { + "en-US": p.title, + }, + [this.getCustomField("subtitle", "product")]: { + "en-US": p.subtitle, + }, + [this.getCustomField("description", "product")]: { + "en-US": p.description, + }, + [this.getCustomField("options", "product")]: { + "en-US": this.transformMedusaIds(p.options), + }, + [this.getCustomField("variants", "product")]: { + "en-US": variantLinks, + }, + [this.getCustomField("medusaId", "product")]: { + "en-US": p.id, + }, + } + + if (data.fields.includes("thumbnail") && p.thumbnail) { + let url = p.thumbnail + if (p.thumbnail.startsWith("//")) { + url = `https:${url}` } - const p = await this.productService_.retrieve(data.id, { - relations: [ - "options", - "variants", - "type", - "collection", - "tags", - "images", - ], + const thumbnailAsset = await environment.createAsset({ + fields: { + title: { + "en-US": `${p.title}`, + }, + description: { + "en-US": "", + }, + file: { + "en-US": { + contentType: "image/xyz", + fileName: url, + upload: url, + }, + }, + }, }) - const environment = await this.getContentfulEnvironment_() + await thumbnailAsset.processForAllLocales().then((a) => a.publish()) - // check if product exists - let productEntry = undefined - try { - productEntry = await environment.getEntry(data.id) - } catch (error) { - return this.createProductInContentful(p) - } - - const variantEntries = await this.getVariantEntries_(p.variants) - const variantLinks = this.getVariantLinks_(variantEntries) - - const productEntryFields = { - ...productEntry.fields, - [this.getCustomField("title", "product")]: { - "en-US": p.title, - }, - [this.getCustomField("subtitle", "product")]: { - "en-US": p.subtitle, - }, - [this.getCustomField("description", "product")]: { - "en-US": p.description, - }, - [this.getCustomField("options", "product")]: { - "en-US": this.transformMedusaIds(p.options), - }, - [this.getCustomField("variants", "product")]: { - "en-US": variantLinks, - }, - [this.getCustomField("medusaId", "product")]: { - "en-US": p.id, + const thumbnailLink = { + sys: { + type: "Link", + linkType: "Asset", + id: thumbnailAsset.sys.id, }, } - if (data.fields.includes("thumbnail") && p.thumbnail) { - let url = p.thumbnail - if (p.thumbnail.startsWith("//")) { - url = `https:${url}` - } - - const thumbnailAsset = await environment.createAsset({ - fields: { - title: { - "en-US": `${p.title}`, - }, - description: { - "en-US": "", - }, - file: { - "en-US": { - contentType: "image/xyz", - fileName: url, - upload: url, - }, - }, - }, - }) - - await thumbnailAsset.processForAllLocales().then((a) => a.publish()) - - const thumbnailLink = { - sys: { - type: "Link", - linkType: "Asset", - id: thumbnailAsset.sys.id, - }, - } - - productEntryFields.thumbnail = { - "en-US": thumbnailLink, - } + productEntryFields.thumbnail = { + "en-US": thumbnailLink, } - - await this.createSpecialProductFields(productEntryFields, p) - - productEntry.fields = productEntryFields - - const updatedEntry = await productEntry.update() - const publishedEntry = await updatedEntry.publish() - - await this.addIgnore_(data.id, "medusa") - - return publishedEntry - } catch (error) { - throw error } + + await this.createSpecialProductFields(productEntryFields, p) + + productEntry.fields = productEntryFields + + const updatedEntry = await productEntry.update() + const publishedEntry = await updatedEntry.publish() + + await this.addIgnore_(data.id, "medusa") + + return publishedEntry } async createSpecialProductFields(fields, p) { @@ -711,124 +679,112 @@ class ContentfulService extends BaseService { } } - try { - const ignore = await this.shouldIgnore_(variant.id, "contentful") - if (ignore) { - return Promise.resolve() - } - - const environment = await this.getContentfulEnvironment_() - // check if product exists - let variantEntry = undefined - // if not, we create a new one - try { - variantEntry = await environment.getEntry(variant.id) - } catch (error) { - return this.createProductVariantInContentful(variant) - } - - const v = await this.productVariantService_.retrieve(variant.id, { - relations: ["prices", "options"], - }) - - const variantEntryFields = { - ...variantEntry.fields, - [this.getCustomField("title", "variant")]: { - "en-US": v.title, - }, - [this.getCustomField("sku", "variant")]: { - "en-US": v.sku, - }, - [this.getCustomField("options", "variant")]: { - "en-US": this.transformMedusaIds(v.options), - }, - [this.getCustomField("prices", "variant")]: { - "en-US": this.transformMedusaIds(v.prices), - }, - [this.getCustomField("medusaId", "variant")]: { - "en-US": v.id, - }, - } - - variantEntry.fields = variantEntryFields - - const updatedEntry = await variantEntry.update() - const publishedEntry = await updatedEntry.publish() - - await this.addIgnore_(variant.id, "medusa") - - return publishedEntry - } catch (error) { - throw error + const ignore = await this.shouldIgnore_(variant.id, "contentful") + if (ignore) { + return Promise.resolve() } + + const environment = await this.getContentfulEnvironment_() + // check if product exists + let variantEntry = undefined + // if not, we create a new one + try { + variantEntry = await environment.getEntry(variant.id) + } catch (error) { + return this.createProductVariantInContentful(variant) + } + + const v = await this.productVariantService_.retrieve(variant.id, { + relations: ["prices", "options"], + }) + + const variantEntryFields = { + ...variantEntry.fields, + [this.getCustomField("title", "variant")]: { + "en-US": v.title, + }, + [this.getCustomField("sku", "variant")]: { + "en-US": v.sku, + }, + [this.getCustomField("options", "variant")]: { + "en-US": this.transformMedusaIds(v.options), + }, + [this.getCustomField("prices", "variant")]: { + "en-US": this.transformMedusaIds(v.prices), + }, + [this.getCustomField("medusaId", "variant")]: { + "en-US": v.id, + }, + } + + variantEntry.fields = variantEntryFields + + const updatedEntry = await variantEntry.update() + const publishedEntry = await updatedEntry.publish() + + await this.addIgnore_(variant.id, "medusa") + + return publishedEntry } async archiveProductVariantInContentful(variant) { let variantEntity - try { - const ignore = await this.shouldIgnore_(variant.id, "contentful") - if (ignore) { - return Promise.resolve() - } - - try { - variantEntity = await this.productVariantService_.retrieve(variant.id) - } catch (err) { - // ignore - } - - if (variantEntity) { - return Promise.resolve() - } - - return await this.archiveEntryWidthId(variant.id) - } catch (error) { - throw error + const ignore = await this.shouldIgnore_(variant.id, "contentful") + if (ignore) { + return Promise.resolve() } + + try { + variantEntity = await this.productVariantService_.retrieve(variant.id) + } catch (err) { + // ignore + } + + if (variantEntity) { + return Promise.resolve() + } + + return await this.archiveEntryWidthId(variant.id) } async archiveProductInContentful(product) { let productEntity - try { - const ignore = await this.shouldIgnore_(product.id, "contentful") - if (ignore) { - return Promise.resolve() - } - - try { - productEntity = await this.productService_.retrieve(product.id) - } catch (err) {} - - if (productEntity) { - return Promise.resolve() - } - - return await this.archiveEntryWidthId(product.id) - } catch (error) { - throw error + const ignore = await this.shouldIgnore_(product.id, "contentful") + if (ignore) { + return Promise.resolve() } + + try { + productEntity = await this.productService_.retrieve(product.id) + } catch (err) { + // noop + } + + if (productEntity) { + return Promise.resolve() + } + + return await this.archiveEntryWidthId(product.id) } async archiveRegionInContentful(region) { let regionEntity - try { - const ignore = await this.shouldIgnore_(region.id, "contentful") - if (ignore) { - return Promise.resolve() - } - - try { - regionEntity = await this.regionService_.retrieve(region.id) - } catch (err) {} - - if (regionEntity) { - return Promise.resolve() - } - - return await this.archiveEntryWidthId(region.id) - } catch (error) { - throw error + const ignore = await this.shouldIgnore_(region.id, "contentful") + if (ignore) { + return Promise.resolve() } + + try { + regionEntity = await this.regionService_.retrieve(region.id) + } catch (err) { + // noop + } + + if (regionEntity) { + return Promise.resolve() + } + + return await this.archiveEntryWidthId(region.id) } async archiveEntryWidthId(id) { @@ -855,75 +811,62 @@ class ContentfulService extends BaseService { return } - try { - const environment = await this.getContentfulEnvironment_() - const productEntry = await environment.getEntry(productId) + const environment = await this.getContentfulEnvironment_() + const productEntry = await environment.getEntry(productId) - const product = await this.productService_.retrieve(productId, { - select: [ - "id", - "handle", - "title", - "subtitle", - "description", - "thumbnail", - ], - }) + const product = await this.productService_.retrieve(productId, { + select: ["id", "handle", "title", "subtitle", "description", "thumbnail"], + }) - const update = {} + const update = {} - const title = - productEntry.fields[this.getCustomField("title", "product")]?.["en-US"] + const title = + productEntry.fields[this.getCustomField("title", "product")]?.["en-US"] - const subtitle = - productEntry.fields[this.getCustomField("subtitle", "product")]?.[ - "en-US" - ] + const subtitle = + productEntry.fields[this.getCustomField("subtitle", "product")]?.["en-US"] - const description = - productEntry.fields[this.getCustomField("description", "product")]?.[ - "en-US" - ] + const description = + productEntry.fields[this.getCustomField("description", "product")]?.[ + "en-US" + ] - const handle = - productEntry.fields[this.getCustomField("handle", "product")]?.["en-US"] + const handle = + productEntry.fields[this.getCustomField("handle", "product")]?.["en-US"] - if (product.title !== title) { - update.title = title - } + if (product.title !== title) { + update.title = title + } - if (product.subtitle !== subtitle) { - update.subtitle = subtitle - } + if (product.subtitle !== subtitle) { + update.subtitle = subtitle + } - if (product.description !== description) { - update.description = description - } + if (product.description !== description) { + update.description = description + } - if (product.handle !== handle) { - update.handle = handle - } + if (product.handle !== handle) { + update.handle = handle + } - // Get the thumbnail, if present - if (productEntry.fields.thumbnail) { - const thumb = await environment.getAsset( - productEntry.fields.thumbnail["en-US"].sys.id - ) + // Get the thumbnail, if present + if (productEntry.fields.thumbnail) { + const thumb = await environment.getAsset( + productEntry.fields.thumbnail["en-US"].sys.id + ) - if (thumb.fields.file["en-US"].url) { - if (!product.thumbnail?.includes(thumb.fields.file["en-US"].url)) { - update.thumbnail = thumb.fields.file["en-US"].url - } + if (thumb.fields.file["en-US"].url) { + if (!product.thumbnail?.includes(thumb.fields.file["en-US"].url)) { + update.thumbnail = thumb.fields.file["en-US"].url } } + } - if (!_.isEmpty(update)) { - await this.productService_.update(productId, update).then(async () => { - return await this.addIgnore_(productId, "contentful") - }) - } - } catch (error) { - throw error + if (!_.isEmpty(update)) { + await this.productService_.update(productId, update).then(async () => { + return await this.addIgnore_(productId, "contentful") + }) } } @@ -933,25 +876,19 @@ class ContentfulService extends BaseService { return } - try { - const environment = await this.getContentfulEnvironment_() - const variantEntry = await environment.getEntry(variantId) + const environment = await this.getContentfulEnvironment_() + const variantEntry = await environment.getEntry(variantId) - const updatedVariant = await this.productVariantService_ - .update(variantId, { - title: - variantEntry.fields[this.getCustomField("title", "variant")][ - "en-US" - ], - }) - .then(async () => { - return await this.addIgnore_(variantId, "contentful") - }) + const updatedVariant = await this.productVariantService_ + .update(variantId, { + title: + variantEntry.fields[this.getCustomField("title", "variant")]["en-US"], + }) + .then(async () => { + return await this.addIgnore_(variantId, "contentful") + }) - return updatedVariant - } catch (error) { - throw error - } + return updatedVariant } transformMedusaIds(objOrArray) {