fix: contentful sync (#206)
This commit is contained in:
1 parent
31d5bb017d
commit
227cdb6222
6 files changed
+133
-84
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "medusa-plugin-contentful",
|
"name": "medusa-plugin-contentful",
|
||||||
"version": "1.1.4",
|
"version": "1.1.4-alpha.7+739b7b3c",
|
||||||
"description": "Contentful plugin for Medusa Commerce",
|
"description": "Contentful plugin for Medusa Commerce",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -43,5 +43,5 @@
|
|||||||
"medusa-test-utils": "^1.1.3",
|
"medusa-test-utils": "^1.1.3",
|
||||||
"redis": "^3.0.2"
|
"redis": "^3.0.2"
|
||||||
},
|
},
|
||||||
"gitHead": "0646bd395a6056657cb0aa93c13699c4a9dbbcdd"
|
"gitHead": "739b7b3cc5ff31aa7a15637db2000e645c7597b1"
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ const checkContentTypes = async (container) => {
|
|||||||
if (!product) {
|
if (!product) {
|
||||||
throw Error("Content type: `product` is missing in Contentful")
|
throw Error("Content type: `product` is missing in Contentful")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!variant) {
|
if (!variant) {
|
||||||
throw Error("Content type: `productVariant` is missing in Contentful")
|
throw Error("Content type: `productVariant` is missing in Contentful")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,13 +96,15 @@ class ContentfulService extends BaseService {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
await asset.processForAllLocales()
|
const finalAsset = await asset
|
||||||
|
.processForAllLocales()
|
||||||
|
.then((ast) => ast.publish())
|
||||||
|
|
||||||
assets.push({
|
assets.push({
|
||||||
sys: {
|
sys: {
|
||||||
type: "Link",
|
type: "Link",
|
||||||
linkType: "Asset",
|
linkType: "Asset",
|
||||||
id: asset.sys.id,
|
id: finalAsset.sys.id,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -231,9 +233,9 @@ class ContentfulService extends BaseService {
|
|||||||
fields,
|
fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
// const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
||||||
ignoreIds.push(product.id)
|
// ignoreIds.push(product.id)
|
||||||
this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
// this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
@@ -271,38 +273,47 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
// const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
||||||
ignoreIds.push(v.id)
|
// ignoreIds.push(v.id)
|
||||||
this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
// this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateProductInContentful(product) {
|
async updateProductInContentful(data) {
|
||||||
|
const updateFields = [
|
||||||
|
"variants",
|
||||||
|
"options",
|
||||||
|
"tags",
|
||||||
|
"title",
|
||||||
|
"tags",
|
||||||
|
"type",
|
||||||
|
"type_id",
|
||||||
|
"collection",
|
||||||
|
"collection_id",
|
||||||
|
"thumbnail",
|
||||||
|
]
|
||||||
|
|
||||||
|
const found = data.fields.find((f) => updateFields.includes(f))
|
||||||
|
if (!found) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
// const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
||||||
|
|
||||||
if (ignoreIds.includes(product.id)) {
|
// if (ignoreIds.includes(product.id)) {
|
||||||
const newIgnoreIds = ignoreIds.filter((id) => id !== product.id)
|
// const newIgnoreIds = ignoreIds.filter((id) => id !== product.id)
|
||||||
this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds))
|
// this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds))
|
||||||
return
|
// return
|
||||||
} else {
|
// } else {
|
||||||
ignoreIds.push(product.id)
|
// ignoreIds.push(product.id)
|
||||||
this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
// this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
||||||
}
|
// }
|
||||||
|
|
||||||
const environment = await this.getContentfulEnvironment_()
|
const p = await this.productService_.retrieve(data.id, {
|
||||||
// check if product exists
|
|
||||||
let productEntry = undefined
|
|
||||||
try {
|
|
||||||
productEntry = await environment.getEntry(product.id)
|
|
||||||
} catch (error) {
|
|
||||||
return this.createProductInContentful(product)
|
|
||||||
}
|
|
||||||
|
|
||||||
const p = await this.productService_.retrieve(product.id, {
|
|
||||||
relations: [
|
relations: [
|
||||||
"options",
|
"options",
|
||||||
"variants",
|
"variants",
|
||||||
@@ -313,6 +324,15 @@ class ContentfulService extends BaseService {
|
|||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const environment = await this.getContentfulEnvironment_()
|
||||||
|
// check if product exists
|
||||||
|
let productEntry = undefined
|
||||||
|
try {
|
||||||
|
productEntry = await environment.getEntry(data.id)
|
||||||
|
} catch (error) {
|
||||||
|
return this.createProductInContentful(p)
|
||||||
|
}
|
||||||
|
|
||||||
const variantEntries = await this.getVariantEntries_(p.variants)
|
const variantEntries = await this.getVariantEntries_(p.variants)
|
||||||
const variantLinks = this.getVariantLinks_(variantEntries)
|
const variantLinks = this.getVariantLinks_(variantEntries)
|
||||||
|
|
||||||
@@ -332,7 +352,12 @@ class ContentfulService extends BaseService {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.thumbnail) {
|
if (data.fields.includes("thumbnail") && p.thumbnail) {
|
||||||
|
let url = p.thumbnail
|
||||||
|
if (p.thumbnail.startsWith("//")) {
|
||||||
|
url = `https:${url}`
|
||||||
|
}
|
||||||
|
|
||||||
const thumbnailAsset = await environment.createAsset({
|
const thumbnailAsset = await environment.createAsset({
|
||||||
fields: {
|
fields: {
|
||||||
title: {
|
title: {
|
||||||
@@ -344,8 +369,8 @@ class ContentfulService extends BaseService {
|
|||||||
file: {
|
file: {
|
||||||
"en-US": {
|
"en-US": {
|
||||||
contentType: "image/xyz",
|
contentType: "image/xyz",
|
||||||
fileName: p.thumbnail,
|
fileName: url,
|
||||||
upload: p.thumbnail,
|
upload: url,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -412,20 +437,37 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateProductVariantInContentful(variant) {
|
async updateProductVariantInContentful(variant) {
|
||||||
try {
|
const updateFields = [
|
||||||
const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
"title",
|
||||||
|
"prices",
|
||||||
|
"sku",
|
||||||
|
"material",
|
||||||
|
"weight",
|
||||||
|
"length",
|
||||||
|
"height",
|
||||||
|
"origin_country",
|
||||||
|
"options",
|
||||||
|
]
|
||||||
|
|
||||||
if (ignoreIds.includes(variant.id)) {
|
const found = variant.fields.find((f) => updateFields.includes(f))
|
||||||
const newIgnoreIds = ignoreIds.filter((id) => id !== variant.id)
|
if (!found) {
|
||||||
this.redis_.set(
|
return
|
||||||
"product_variant_ignore_ids",
|
}
|
||||||
JSON.stringify(newIgnoreIds)
|
|
||||||
)
|
try {
|
||||||
return
|
// const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
||||||
} else {
|
|
||||||
ignoreIds.push(variant.id)
|
//if (ignoreIds.includes(variant.id)) {
|
||||||
this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
// const newIgnoreIds = ignoreIds.filter((id) => id !== variant.id)
|
||||||
}
|
// this.redis_.set(
|
||||||
|
// "product_variant_ignore_ids",
|
||||||
|
// JSON.stringify(newIgnoreIds)
|
||||||
|
// )
|
||||||
|
// return
|
||||||
|
//} else {
|
||||||
|
// ignoreIds.push(variant.id)
|
||||||
|
// this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
||||||
|
//}
|
||||||
|
|
||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
// check if product exists
|
// check if product exists
|
||||||
@@ -476,19 +518,23 @@ class ContentfulService extends BaseService {
|
|||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
const productEntry = await environment.getEntry(productId)
|
const productEntry = await environment.getEntry(productId)
|
||||||
|
|
||||||
const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
const product = await this.productService_.retrieve(productId)
|
||||||
if (ignoreIds.includes(productId)) {
|
|
||||||
const newIgnoreIds = ignoreIds.filter((id) => id !== productId)
|
|
||||||
this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds))
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
ignoreIds.push(productId)
|
|
||||||
this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
|
||||||
}
|
|
||||||
|
|
||||||
let update = {
|
//const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
||||||
title:
|
//if (ignoreIds.includes(productId)) {
|
||||||
productEntry.fields[this.getCustomField("title", "product")]["en-US"],
|
// const newIgnoreIds = ignoreIds.filter((id) => id !== productId)
|
||||||
|
// this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds))
|
||||||
|
// return
|
||||||
|
//} else {
|
||||||
|
// ignoreIds.push(productId)
|
||||||
|
// this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
||||||
|
//}
|
||||||
|
|
||||||
|
let update = {}
|
||||||
|
const title =
|
||||||
|
productEntry.fields[this.getCustomField("title", "product")]["en-US"]
|
||||||
|
if (product.title !== title) {
|
||||||
|
update.title = title
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the thumbnail, if present
|
// Get the thumbnail, if present
|
||||||
@@ -497,15 +543,16 @@ class ContentfulService extends BaseService {
|
|||||||
productEntry.fields.thumbnail["en-US"].sys.id
|
productEntry.fields.thumbnail["en-US"].sys.id
|
||||||
)
|
)
|
||||||
|
|
||||||
update.thumbnail = thumb.fields.file["en-US"].url
|
if (thumb.fields.file["en-US"].url) {
|
||||||
|
if (!product.thumbnail.includes(thumb.fields.file["en-US"].url)) {
|
||||||
|
update.thumbnail = thumb.fields.file["en-US"].url
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedProduct = await this.productService_.update(
|
if (!_.isEmpty(update)) {
|
||||||
productId,
|
await this.productService_.update(productId, update)
|
||||||
update
|
}
|
||||||
)
|
|
||||||
|
|
||||||
return updatedProduct
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
@@ -516,18 +563,18 @@ class ContentfulService extends BaseService {
|
|||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
const variantEntry = await environment.getEntry(variantId)
|
const variantEntry = await environment.getEntry(variantId)
|
||||||
|
|
||||||
const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
// const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
||||||
if (ignoreIds.includes(variantId)) {
|
// if (ignoreIds.includes(variantId)) {
|
||||||
const newIgnoreIds = ignoreIds.filter((id) => id !== variantId)
|
// const newIgnoreIds = ignoreIds.filter((id) => id !== variantId)
|
||||||
this.redis_.set(
|
// this.redis_.set(
|
||||||
"product_variant_ignore_ids",
|
// "product_variant_ignore_ids",
|
||||||
JSON.stringify(newIgnoreIds)
|
// JSON.stringify(newIgnoreIds)
|
||||||
)
|
// )
|
||||||
return
|
// return
|
||||||
} else {
|
// } else {
|
||||||
ignoreIds.push(variantId)
|
// ignoreIds.push(variantId)
|
||||||
this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
// this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
||||||
}
|
// }
|
||||||
|
|
||||||
const updatedVariant = await this.productVariantService_.update(
|
const updatedVariant = await this.productVariantService_.update(
|
||||||
variantId,
|
variantId,
|
||||||
|
|||||||
@@ -309,7 +309,7 @@ describe("ProductVariantService", () => {
|
|||||||
"product-variant.updated",
|
"product-variant.updated",
|
||||||
{
|
{
|
||||||
id: IdMap.getId("ironman"),
|
id: IdMap.getId("ironman"),
|
||||||
title: "new title",
|
fields: ["title"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -333,7 +333,7 @@ describe("ProductVariantService", () => {
|
|||||||
"product-variant.updated",
|
"product-variant.updated",
|
||||||
{
|
{
|
||||||
id: IdMap.getId("ironman"),
|
id: IdMap.getId("ironman"),
|
||||||
title: "new title",
|
fields: ["title"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -370,10 +370,7 @@ describe("ProductVariantService", () => {
|
|||||||
"product-variant.updated",
|
"product-variant.updated",
|
||||||
{
|
{
|
||||||
id: IdMap.getId("ironman"),
|
id: IdMap.getId("ironman"),
|
||||||
title: "new title",
|
fields: ["title", "metadata"],
|
||||||
metadata: {
|
|
||||||
testing: "this",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -298,7 +298,10 @@ class ProductVariantService extends BaseService {
|
|||||||
const result = await variantRepo.save(variant)
|
const result = await variantRepo.save(variant)
|
||||||
await this.eventBus_
|
await this.eventBus_
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.emit(ProductVariantService.Events.UPDATED, result)
|
.emit(ProductVariantService.Events.UPDATED, {
|
||||||
|
id: result.id,
|
||||||
|
fields: Object.keys(update),
|
||||||
|
})
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -444,6 +444,7 @@ class ProductService extends BaseService {
|
|||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.emit(ProductService.Events.UPDATED, {
|
.emit(ProductService.Events.UPDATED, {
|
||||||
id: result.id,
|
id: result.id,
|
||||||
|
fields: Object.keys(update),
|
||||||
})
|
})
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user