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