fix(contentful-plugin): Keep existing entry fields
This commit is contained in:
committed by
GitHub
parent
9dc6999a9a
commit
eb47896668
17
packages/medusa-core-utils/src/compare-objects.js
Normal file
17
packages/medusa-core-utils/src/compare-objects.js
Normal file
@@ -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
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user