fix(contentful-plugin): Keep existing entry fields

This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2020-08-27 18:03:46 +02:00
1 parent 9dc6999a9a
commit eb47896668
5 files changed
+76 -26

No files matched your search

+42 -21
View File
@@ -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
)
}
}
}