Finalized contentful plugin
This commit is contained in:
@@ -21,6 +21,7 @@ export default async (req, res) => {
|
||||
|
||||
res.status(200).send(updated)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
res.status(400).send(`Webhook error: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,24 +47,26 @@ class ContentfulService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
async getVariantEntries_(variantEntryIds) {
|
||||
if (!variantEntryIds) {
|
||||
return []
|
||||
}
|
||||
|
||||
async getVariantEntries_(productId) {
|
||||
try {
|
||||
const environment = await this.getContentfulEnvironment_()
|
||||
return Promise.all(variantEntryIds.map((v) => environment.getEntry(v)))
|
||||
const productVariants = await this.productService_.retrieveVariants(
|
||||
productId
|
||||
)
|
||||
|
||||
const contentfulVariants = await Promise.all(
|
||||
productVariants.map((variant) =>
|
||||
this.updateProductVariantInContentful(variant)
|
||||
)
|
||||
)
|
||||
|
||||
return contentfulVariants
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async getVariantLinks_(variantEntries) {
|
||||
if (!variantEntries) {
|
||||
return []
|
||||
}
|
||||
|
||||
getVariantLinks_(variantEntries) {
|
||||
return variantEntries.map((v) => ({
|
||||
sys: {
|
||||
type: "Link",
|
||||
@@ -77,20 +79,30 @@ class ContentfulService extends BaseService {
|
||||
async createProductInContentful(product) {
|
||||
try {
|
||||
const environment = await this.getContentfulEnvironment_()
|
||||
const variantEntries = await this.getVariantEntries_(product.variants)
|
||||
return environment.createEntryWithId("product", product._id, {
|
||||
fields: {
|
||||
title: {
|
||||
"en-US": product.title,
|
||||
const variantEntries = await this.getVariantEntries_(product._id)
|
||||
const variantLinks = this.getVariantLinks_(variantEntries)
|
||||
const result = await environment.createEntryWithId(
|
||||
"product",
|
||||
product._id,
|
||||
{
|
||||
fields: {
|
||||
title: {
|
||||
"en-US": product.title,
|
||||
},
|
||||
variants: {
|
||||
"en-US": variantLinks,
|
||||
},
|
||||
objectId: {
|
||||
"en-US": product._id,
|
||||
},
|
||||
},
|
||||
variants: {
|
||||
"en-US": [],
|
||||
},
|
||||
objectId: {
|
||||
"en-US": product._id,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
||||
ignoreIds.push(product._id)
|
||||
this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
||||
return result
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
@@ -99,22 +111,31 @@ class ContentfulService extends BaseService {
|
||||
async createProductVariantInContentful(variant) {
|
||||
try {
|
||||
const environment = await this.getContentfulEnvironment_()
|
||||
return environment.createEntryWithId("productVariant", variant._id, {
|
||||
fields: {
|
||||
title: {
|
||||
"en-US": variant.title,
|
||||
const result = await environment.createEntryWithId(
|
||||
"productVariant",
|
||||
variant._id,
|
||||
{
|
||||
fields: {
|
||||
title: {
|
||||
"en-US": variant.title,
|
||||
},
|
||||
sku: {
|
||||
"en-US": variant.sku,
|
||||
},
|
||||
prices: {
|
||||
"en-US": variant.prices,
|
||||
},
|
||||
objectId: {
|
||||
"en-US": variant._id,
|
||||
},
|
||||
},
|
||||
sku: {
|
||||
"en-US": variant.sku,
|
||||
},
|
||||
prices: {
|
||||
"en-US": variant.prices,
|
||||
},
|
||||
objectId: {
|
||||
"en-US": variant._id,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
||||
ignoreIds.push(variant._id)
|
||||
this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
||||
return result
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
@@ -142,13 +163,17 @@ class ContentfulService extends BaseService {
|
||||
return this.createProductInContentful(product)
|
||||
}
|
||||
|
||||
// const variantEntries = await this.getVariantEntries_(product.variants)
|
||||
const variantEntries = await this.getVariantEntries_(product.variants)
|
||||
const variantLinks = this.getVariantLinks_(variantEntries)
|
||||
productEntry.fields = _.assignIn(productEntry.fields, {
|
||||
title: {
|
||||
"en-US": product.title,
|
||||
},
|
||||
variants: {
|
||||
"en-US": [],
|
||||
"en-US": variantLinks,
|
||||
},
|
||||
objectId: {
|
||||
"en-US": product._id,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -163,12 +188,27 @@ class ContentfulService extends BaseService {
|
||||
|
||||
async updateProductVariantInContentful(variant) {
|
||||
try {
|
||||
const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
||||
|
||||
if (ignoreIds.includes(variant._id)) {
|
||||
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_()
|
||||
// check if product exists
|
||||
let variantEntry = undefined
|
||||
variantEntry = await environment.getEntry(variant._id)
|
||||
// if not, we create a new one
|
||||
if (!variantEntry) {
|
||||
try {
|
||||
variantEntry = await environment.getEntry(variant._id)
|
||||
} catch (error) {
|
||||
return this.createProductVariantInContentful(variant)
|
||||
}
|
||||
|
||||
@@ -187,16 +227,8 @@ class ContentfulService extends BaseService {
|
||||
},
|
||||
})
|
||||
|
||||
await variantEntry.update()
|
||||
const publishedEntry = await variantEntry.publish()
|
||||
|
||||
const ignoreIds = await this.getIgnoreIds_("product_variant")
|
||||
|
||||
if (ignoreIds.includes(publishedEntry.sys.id)) {
|
||||
ignoreIds.filter((id) => id !== publishedEntry.sys.id)
|
||||
} else {
|
||||
this.eventBus_.emit("product-variant.updated", publishedEntry)
|
||||
}
|
||||
const updatedEntry = await variantEntry.update()
|
||||
const publishedEntry = await updatedEntry.publish()
|
||||
|
||||
return publishedEntry
|
||||
} catch (error) {
|
||||
@@ -216,36 +248,41 @@ class ContentfulService extends BaseService {
|
||||
return
|
||||
} else {
|
||||
ignoreIds.push(productId)
|
||||
this.redis_.set("product_ignore_ids", JSON.stringify([productId]))
|
||||
this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
||||
}
|
||||
|
||||
const updatedProduct = await this.productService_.update(productId, {
|
||||
title: productEntry.fields.title["en-US"],
|
||||
// variants: productEntry.fields.variants["en-US"],
|
||||
})
|
||||
|
||||
return updatedProduct
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async sendContentfulProductVariantToAdmin(variant) {
|
||||
async sendContentfulProductVariantToAdmin(variantId) {
|
||||
try {
|
||||
const environment = await this.getContentfulEnvironment_()
|
||||
const variantEntry = await environment.getEntry(variant.sys.id)
|
||||
const variantEntry = await environment.getEntry(variantId)
|
||||
|
||||
const ignoreIds = await this.getIgnoreIds_("product_variant")
|
||||
ignoreIds.push(variant.sys.id)
|
||||
this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
||||
const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
||||
if (ignoreIds.includes(variantId)) {
|
||||
const newIgnoreIds = ignoreIds.filter((id) => id !== variantId)
|
||||
this.redis_.set(
|
||||
"product_variant_ignore_ids",
|
||||
JSON.stringify(newIgnoreIds)
|
||||
)
|
||||
return
|
||||
} else {
|
||||
ignoreIds.push(variantId)
|
||||
this.redis_.set("product_variant_ignore_ids", JSON.stringify(ignoreIds))
|
||||
}
|
||||
|
||||
const updatedVariant = await this.variantService_.update(
|
||||
variantEntry.objectId,
|
||||
const updatedVariant = await this.productVariantService_.update(
|
||||
variantId,
|
||||
{
|
||||
title: variantEntry.fields.title["en-US"],
|
||||
sku: variantEntry.fields.sku["en-US"],
|
||||
prices: variantEntry.fields.prices["en-US"],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ class ProductService extends BaseService {
|
||||
* @param {string} productId - the id of the product to get variants from.
|
||||
* @return {Promise} an array of variants
|
||||
*/
|
||||
async retrieveVariants(productId) {
|
||||
async retrieveVariants(productId) {)
|
||||
const product = await this.retrieve(productId)
|
||||
return this.productVariantService_.list({ _id: { $in: product.variants } })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user