Finalized Contentful product sync
This commit is contained in:
+4
-3
@@ -2,16 +2,17 @@ export default async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const contentfulService = req.scope.resolve("contentfulService")
|
const contentfulService = req.scope.resolve("contentfulService")
|
||||||
|
|
||||||
const contentfulType = req.body.contentType.sys.id
|
const contentfulType = req.body.sys.contentType.sys.id
|
||||||
|
const entryId = req.body.sys.id
|
||||||
|
|
||||||
let updated = {}
|
let updated = {}
|
||||||
switch (contentfulType) {
|
switch (contentfulType) {
|
||||||
case "product":
|
case "product":
|
||||||
updated = await contentfulService.sendContentfulProductToAdmin(req.body)
|
updated = await contentfulService.sendContentfulProductToAdmin(entryId)
|
||||||
break
|
break
|
||||||
case "productVariant":
|
case "productVariant":
|
||||||
updated = await contentfulService.sendContentfulProductVariantToAdmin(
|
updated = await contentfulService.sendContentfulProductVariantToAdmin(
|
||||||
req.body
|
entryId
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
@@ -32,10 +32,6 @@ class ContentfulService extends BaseService {
|
|||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reply) {
|
|
||||||
return reject("Missing key")
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(JSON.parse(reply))
|
return resolve(JSON.parse(reply))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -88,7 +84,7 @@ class ContentfulService extends BaseService {
|
|||||||
"en-US": product.title,
|
"en-US": product.title,
|
||||||
},
|
},
|
||||||
variants: {
|
variants: {
|
||||||
"en-US": this.getVariantLinks_(variantEntries),
|
"en-US": [],
|
||||||
},
|
},
|
||||||
objectId: {
|
objectId: {
|
||||||
"en-US": product._id,
|
"en-US": product._id,
|
||||||
@@ -126,6 +122,17 @@ class ContentfulService extends BaseService {
|
|||||||
|
|
||||||
async updateProductInContentful(product) {
|
async updateProductInContentful(product) {
|
||||||
try {
|
try {
|
||||||
|
const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
||||||
|
|
||||||
|
if (ignoreIds.includes(product._id)) {
|
||||||
|
const newIgnoreIds = ignoreIds.filter((id) => id !== product._id)
|
||||||
|
this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds))
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
ignoreIds.push(product._id)
|
||||||
|
this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
||||||
|
}
|
||||||
|
|
||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
// check if product exists
|
// check if product exists
|
||||||
let productEntry = undefined
|
let productEntry = undefined
|
||||||
@@ -135,25 +142,18 @@ class ContentfulService extends BaseService {
|
|||||||
return this.createProductInContentful(product)
|
return this.createProductInContentful(product)
|
||||||
}
|
}
|
||||||
|
|
||||||
const variantEntries = await this.getVariantEntries_(product.variants)
|
// const variantEntries = await this.getVariantEntries_(product.variants)
|
||||||
productEntry.fields = _.assignIn(productEntry.fields, {
|
productEntry.fields = _.assignIn(productEntry.fields, {
|
||||||
title: {
|
title: {
|
||||||
"en-US": product.title,
|
"en-US": product.title,
|
||||||
},
|
},
|
||||||
variants: {
|
variants: {
|
||||||
"en-US": this.getVariantLinks_(variantEntries),
|
"en-US": [],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
await productEntry.update()
|
const updatedEntry = await productEntry.update()
|
||||||
const publishedEntry = await productEntry.publish()
|
const publishedEntry = await updatedEntry.publish()
|
||||||
|
|
||||||
const ignoreIds = await this.getIgnoreIds_("product")
|
|
||||||
if (ignoreIds.includes(publishedEntry.sys.id)) {
|
|
||||||
ignoreIds.filter((id) => id !== publishedEntry.sys.id)
|
|
||||||
} else {
|
|
||||||
this.eventBus_.emit("product.updated", publishedEntry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return publishedEntry
|
return publishedEntry
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -191,6 +191,7 @@ class ContentfulService extends BaseService {
|
|||||||
const publishedEntry = await variantEntry.publish()
|
const publishedEntry = await variantEntry.publish()
|
||||||
|
|
||||||
const ignoreIds = await this.getIgnoreIds_("product_variant")
|
const ignoreIds = await this.getIgnoreIds_("product_variant")
|
||||||
|
|
||||||
if (ignoreIds.includes(publishedEntry.sys.id)) {
|
if (ignoreIds.includes(publishedEntry.sys.id)) {
|
||||||
ignoreIds.filter((id) => id !== publishedEntry.sys.id)
|
ignoreIds.filter((id) => id !== publishedEntry.sys.id)
|
||||||
} else {
|
} else {
|
||||||
@@ -203,25 +204,29 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendContentfulProductToAdmin(product) {
|
async sendContentfulProductToAdmin(productId) {
|
||||||
try {
|
try {
|
||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
const productEntry = await environment.getEntry(product.sys.id)
|
const productEntry = await environment.getEntry(productId)
|
||||||
|
|
||||||
const ignoreIds = await this.getIgnoreIds_("product")
|
const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
||||||
ignoreIds.push(product.sys.id)
|
if (ignoreIds.includes(productId)) {
|
||||||
this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
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([productId]))
|
||||||
|
}
|
||||||
|
|
||||||
const updatedProduct = await this.productService_.update(
|
const updatedProduct = await this.productService_.update(productId, {
|
||||||
productEntry.objectId,
|
title: productEntry.fields.title["en-US"],
|
||||||
{
|
// variants: productEntry.fields.variants["en-US"],
|
||||||
title: productEntry.fields.title["en-US"],
|
})
|
||||||
variants: productEntry.fields.variants["en-US"],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return updatedProduct
|
return updatedProduct
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class ContentfulSubscriber {
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.eventBus_.subscribe("product.created", async (data) => {
|
this.eventBus_.subscribe("product.created", async (data) => {
|
||||||
await this.contentfulService_.createProductVariantInContentful(data)
|
await this.contentfulService_.createProductInContentful(data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user