From eba21d9c5f5fac292013cf1c39ead1d5523414d6 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Thu, 18 May 2023 10:12:34 +0200 Subject: [PATCH] feat(medusa-plugin-contentful): Initial implementation of inventory module (#3991) * cleanup contentful plugin * initial implementation of inventory module in contentful --------- Co-authored-by: olivermrbl --- .changeset/soft-eyes-vanish.md | 5 +++ .../src/services/contentful.js | 32 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .changeset/soft-eyes-vanish.md diff --git a/.changeset/soft-eyes-vanish.md b/.changeset/soft-eyes-vanish.md new file mode 100644 index 0000000000..829c0dced8 --- /dev/null +++ b/.changeset/soft-eyes-vanish.md @@ -0,0 +1,5 @@ +--- +"medusa-plugin-contentful": patch +--- + +feat(medusa-plugin-contentful): include sku from inventory module when syncing variants and products diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index a67ce55251..40fd9c84d0 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -12,6 +12,8 @@ class ContentfulService extends BaseService { cacheService, productVariantService, eventBusService, + productVariantInventoryService, + featureFlagRouter, }, options ) { @@ -33,6 +35,10 @@ class ContentfulService extends BaseService { this.cacheService_ = cacheService + this.productVariantInventoryService_ = productVariantInventoryService + + this.featureFlagRouter_ = featureFlagRouter + this.capab_ = {} } @@ -234,6 +240,17 @@ class ContentfulService extends BaseService { relations: ["prices", "options"], }) + let sku = v.sku + if (this.featureFlagRouter_.isFeatureEnabled("inventoryService")) { + const [inventoryItem] = + await this.productVariantInventoryService_.listInventoryItemsByVariant( + v.id + ) + if (inventoryItem) { + sku = inventoryItem.sku + } + } + const environment = await this.getContentfulEnvironment_() const result = await environment.createEntryWithId("productVariant", v.id, { fields: { @@ -241,7 +258,7 @@ class ContentfulService extends BaseService { "en-US": v.title, }, [this.getCustomField("sku", "variant")]: { - "en-US": v.sku, + "en-US": sku, }, [this.getCustomField("prices", "variant")]: { "en-US": this.transformMedusaIds(v.prices), @@ -698,13 +715,24 @@ class ContentfulService extends BaseService { relations: ["prices", "options"], }) + let sku = v.sku + if (this.featureFlagRouter_.isFeatureEnabled("inventoryService")) { + const [inventoryItem] = + await this.productVariantInventoryService_.listInventoryItemsByVariant( + v.id + ) + if (inventoryItem) { + sku = inventoryItem.sku + } + } + const variantEntryFields = { ...variantEntry.fields, [this.getCustomField("title", "variant")]: { "en-US": v.title, }, [this.getCustomField("sku", "variant")]: { - "en-US": v.sku, + "en-US": sku, }, [this.getCustomField("options", "variant")]: { "en-US": this.transformMedusaIds(v.options),