fix(contentful-plugin): Keep existing entry fields
This commit is contained in:
1 parent
9dc6999a9a
commit
eb47896668
5 files changed
+76
-26
No files matched your search
@@ -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 MedusaError } from "./errors"
|
||||||
export { default as getConfigFile } from "./get-config-file"
|
export { default as getConfigFile } from "./get-config-file"
|
||||||
export { default as createRequireFromPath } from "./create-require-from-path"
|
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) => {
|
export default (app) => {
|
||||||
app.use("/hooks", route)
|
app.use("/hooks", route)
|
||||||
|
|
||||||
route.post("/contentful", middlewares.wrap(require("./contentful").default))
|
route.post(
|
||||||
|
"/contentful",
|
||||||
|
bodyParser.json(),
|
||||||
|
middlewares.wrap(require("./contentful").default)
|
||||||
|
)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
@@ -168,7 +168,9 @@ class ContentfulService extends BaseService {
|
|||||||
|
|
||||||
const variantEntries = await this.getVariantEntries_(product._id)
|
const variantEntries = await this.getVariantEntries_(product._id)
|
||||||
const variantLinks = this.getVariantLinks_(variantEntries)
|
const variantLinks = this.getVariantLinks_(variantEntries)
|
||||||
productEntry.fields = _.assignIn(productEntry.fields, {
|
|
||||||
|
const productEntryFields = {
|
||||||
|
...productEntry.fields,
|
||||||
title: {
|
title: {
|
||||||
"en-US": product.title,
|
"en-US": product.title,
|
||||||
},
|
},
|
||||||
@@ -181,7 +183,9 @@ class ContentfulService extends BaseService {
|
|||||||
objectId: {
|
objectId: {
|
||||||
"en-US": product._id,
|
"en-US": product._id,
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
|
||||||
|
productEntry.fields = productEntryFields
|
||||||
|
|
||||||
const updatedEntry = await productEntry.update()
|
const updatedEntry = await productEntry.update()
|
||||||
const publishedEntry = await updatedEntry.publish()
|
const publishedEntry = await updatedEntry.publish()
|
||||||
@@ -218,7 +222,8 @@ class ContentfulService extends BaseService {
|
|||||||
return this.createProductVariantInContentful(variant)
|
return this.createProductVariantInContentful(variant)
|
||||||
}
|
}
|
||||||
|
|
||||||
variantEntry.fields = _.assignIn(variantEntry.fields, {
|
const variantEntryFields = {
|
||||||
|
...variantEntry.fields,
|
||||||
title: {
|
title: {
|
||||||
"en-US": variant.title,
|
"en-US": variant.title,
|
||||||
},
|
},
|
||||||
@@ -234,7 +239,9 @@ class ContentfulService extends BaseService {
|
|||||||
objectId: {
|
objectId: {
|
||||||
"en-US": variant._id,
|
"en-US": variant._id,
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
|
||||||
|
variantEntry.fields = variantEntryFields
|
||||||
|
|
||||||
const updatedEntry = await variantEntry.update()
|
const updatedEntry = await variantEntry.update()
|
||||||
const publishedEntry = await updatedEntry.publish()
|
const publishedEntry = await updatedEntry.publish()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import mongoose from "mongoose"
|
import mongoose from "mongoose"
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError, compareObjectsByProp } from "medusa-core-utils"
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -161,32 +161,53 @@ class ProductService extends BaseService {
|
|||||||
await Promise.all(
|
await Promise.all(
|
||||||
update.variants.map(async variant => {
|
update.variants.map(async variant => {
|
||||||
if (variant._id) {
|
if (variant._id) {
|
||||||
|
const variantFromDb = existingVariants.find(v =>
|
||||||
|
v._id.equals(variant._id)
|
||||||
|
)
|
||||||
if (variant.prices && variant.prices.length) {
|
if (variant.prices && variant.prices.length) {
|
||||||
for (const price of variant.prices) {
|
// if equal we dont want to update
|
||||||
if (price.region_id) {
|
const isPricesEqual = compareObjectsByProp(
|
||||||
await this.productVariantService_.setRegionPrice(
|
variant,
|
||||||
variant._id,
|
variantFromDb,
|
||||||
price.region_id,
|
"prices"
|
||||||
price.amount
|
)
|
||||||
)
|
|
||||||
} else {
|
if (!isPricesEqual) {
|
||||||
await this.productVariantService_.setCurrencyPrice(
|
for (const price of variant.prices) {
|
||||||
variant._id,
|
if (price.region_id) {
|
||||||
price.currency_code,
|
await this.productVariantService_.setRegionPrice(
|
||||||
price.amount
|
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) {
|
if (variant.options && variant.options.length) {
|
||||||
for (const option of variant.options) {
|
// if equal we dont want to update
|
||||||
await this.updateOptionValue(
|
const isOptionsEqual = compareObjectsByProp(
|
||||||
productId,
|
variant,
|
||||||
variant._id,
|
variantFromDb,
|
||||||
option.option_id,
|
"options"
|
||||||
option.value
|
)
|
||||||
)
|
|
||||||
|
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