From eb478966684776bb2aa48e98789519644b05cd33 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Thu, 27 Aug 2020 18:03:46 +0200 Subject: [PATCH] fix(contentful-plugin): Keep existing entry fields --- .../medusa-core-utils/src/compare-objects.js | 17 +++++ packages/medusa-core-utils/src/index.js | 1 + .../src/api/routes/hooks/index.js | 6 +- .../src/services/contentful.js | 15 +++-- packages/medusa/src/services/product.js | 63 ++++++++++++------- 5 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 packages/medusa-core-utils/src/compare-objects.js diff --git a/packages/medusa-core-utils/src/compare-objects.js b/packages/medusa-core-utils/src/compare-objects.js new file mode 100644 index 0000000000..d2d3a95e68 --- /dev/null +++ b/packages/medusa-core-utils/src/compare-objects.js @@ -0,0 +1,17 @@ +import _ from "lodash" + +function compareObjectsByProp(object1, object2, prop) { + if (Array.isArray(object1[prop])) { + object2[prop] = object2[prop].map(({ _id, ...rest }) => rest) + return ( + _.differenceWith(object1[prop], object2[prop], _.isEqual).length === 0 + ) + } else if (typeof object1[prop] === "object") { + delete object2[prop]._id + return _.isEqual(object1[prop], object2[prop]) + } else { + return object1[prop] === object2[prop] + } +} + +export default compareObjectsByProp diff --git a/packages/medusa-core-utils/src/index.js b/packages/medusa-core-utils/src/index.js index 1ba0b8ab13..c8b5f91718 100644 --- a/packages/medusa-core-utils/src/index.js +++ b/packages/medusa-core-utils/src/index.js @@ -2,3 +2,4 @@ export { default as Validator } from "./validator" export { default as MedusaError } from "./errors" export { default as getConfigFile } from "./get-config-file" export { default as createRequireFromPath } from "./create-require-from-path" +export { default as compareObjectsByProp } from "./compare-objects" diff --git a/packages/medusa-plugin-contentful/src/api/routes/hooks/index.js b/packages/medusa-plugin-contentful/src/api/routes/hooks/index.js index 97b2bc4787..ec374b2fbc 100644 --- a/packages/medusa-plugin-contentful/src/api/routes/hooks/index.js +++ b/packages/medusa-plugin-contentful/src/api/routes/hooks/index.js @@ -7,7 +7,11 @@ const route = Router() export default (app) => { app.use("/hooks", route) - route.post("/contentful", middlewares.wrap(require("./contentful").default)) + route.post( + "/contentful", + bodyParser.json(), + middlewares.wrap(require("./contentful").default) + ) return app } diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index a81cfa2cb9..74c416454a 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -168,7 +168,9 @@ class ContentfulService extends BaseService { const variantEntries = await this.getVariantEntries_(product._id) const variantLinks = this.getVariantLinks_(variantEntries) - productEntry.fields = _.assignIn(productEntry.fields, { + + const productEntryFields = { + ...productEntry.fields, title: { "en-US": product.title, }, @@ -181,7 +183,9 @@ class ContentfulService extends BaseService { objectId: { "en-US": product._id, }, - }) + } + + productEntry.fields = productEntryFields const updatedEntry = await productEntry.update() const publishedEntry = await updatedEntry.publish() @@ -218,7 +222,8 @@ class ContentfulService extends BaseService { return this.createProductVariantInContentful(variant) } - variantEntry.fields = _.assignIn(variantEntry.fields, { + const variantEntryFields = { + ...variantEntry.fields, title: { "en-US": variant.title, }, @@ -234,7 +239,9 @@ class ContentfulService extends BaseService { objectId: { "en-US": variant._id, }, - }) + } + + variantEntry.fields = variantEntryFields const updatedEntry = await variantEntry.update() const publishedEntry = await updatedEntry.publish() diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index ed79769021..ca951d129b 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -1,6 +1,6 @@ import mongoose from "mongoose" import _ from "lodash" -import { Validator, MedusaError } from "medusa-core-utils" +import { Validator, MedusaError, compareObjectsByProp } from "medusa-core-utils" import { BaseService } from "medusa-interfaces" /** @@ -161,32 +161,53 @@ class ProductService extends BaseService { await Promise.all( update.variants.map(async variant => { if (variant._id) { + const variantFromDb = existingVariants.find(v => + v._id.equals(variant._id) + ) if (variant.prices && variant.prices.length) { - for (const price of variant.prices) { - if (price.region_id) { - await this.productVariantService_.setRegionPrice( - variant._id, - price.region_id, - price.amount - ) - } else { - await this.productVariantService_.setCurrencyPrice( - variant._id, - price.currency_code, - price.amount - ) + // if equal we dont want to update + const isPricesEqual = compareObjectsByProp( + variant, + variantFromDb, + "prices" + ) + + if (!isPricesEqual) { + for (const price of variant.prices) { + if (price.region_id) { + await this.productVariantService_.setRegionPrice( + variant._id, + price.region_id, + price.amount + ) + } else { + await this.productVariantService_.setCurrencyPrice( + variant._id, + price.currency_code, + price.amount + ) + } } } } if (variant.options && variant.options.length) { - for (const option of variant.options) { - await this.updateOptionValue( - productId, - variant._id, - option.option_id, - option.value - ) + // if equal we dont want to update + const isOptionsEqual = compareObjectsByProp( + variant, + variantFromDb, + "options" + ) + + if (!isOptionsEqual) { + for (const option of variant.options) { + await this.updateOptionValue( + productId, + variant._id, + option.option_id, + option.value + ) + } } }