fix(medusa-plugin-contentful): Allow custom fields in plugin options (#180)
This commit is contained in:
1 parent
e27cf72a8c
commit
587a464e83
2 files changed
+164
-34
No files matched your search
@@ -19,8 +19,16 @@ const checkContentTypes = async (container) => {
|
|||||||
if (product && product.fields) {
|
if (product && product.fields) {
|
||||||
const productFields = product.fields
|
const productFields = product.fields
|
||||||
|
|
||||||
|
const customProductFields = Object.keys(
|
||||||
|
contentfulService.options_.custom_product_fields || {}
|
||||||
|
)
|
||||||
const keys = Object.values(productFields).map((f) => f.id)
|
const keys = Object.values(productFields).map((f) => f.id)
|
||||||
if (!requiredProductFields.every((f) => keys.includes(f))) {
|
|
||||||
|
const missingKeys = requiredProductFields.filter(
|
||||||
|
(rpf) => !keys.includes(rpf) && !customProductFields.includes(rpf)
|
||||||
|
)
|
||||||
|
|
||||||
|
if (missingKeys.length) {
|
||||||
throw Error(
|
throw Error(
|
||||||
`Contentful: Content type ${`product`} is missing some required key(s). Required: ${requiredProductFields.join(
|
`Contentful: Content type ${`product`} is missing some required key(s). Required: ${requiredProductFields.join(
|
||||||
", "
|
", "
|
||||||
@@ -32,8 +40,16 @@ const checkContentTypes = async (container) => {
|
|||||||
if (variant && variant.fields) {
|
if (variant && variant.fields) {
|
||||||
const variantFields = variant.fields
|
const variantFields = variant.fields
|
||||||
|
|
||||||
|
const customVariantFields = Object.keys(
|
||||||
|
contentfulService.options_.custom_variant_fields || {}
|
||||||
|
)
|
||||||
const keys = Object.values(variantFields).map((f) => f.id)
|
const keys = Object.values(variantFields).map((f) => f.id)
|
||||||
if (!requiredVariantFields.every((f) => keys.includes(f))) {
|
|
||||||
|
const missingKeys = requiredVariantFields.filter(
|
||||||
|
(rpf) => !keys.includes(rpf) && !customVariantFields.includes(rpf)
|
||||||
|
)
|
||||||
|
|
||||||
|
if (missingKeys.length) {
|
||||||
throw Error(
|
throw Error(
|
||||||
`Contentful: Content type ${`productVariant`} is missing some required key(s). Required: ${requiredVariantFields.join(
|
`Contentful: Content type ${`productVariant`} is missing some required key(s). Required: ${requiredVariantFields.join(
|
||||||
", "
|
", "
|
||||||
@@ -47,13 +63,13 @@ const requiredProductFields = [
|
|||||||
"title",
|
"title",
|
||||||
"variants",
|
"variants",
|
||||||
"options",
|
"options",
|
||||||
"objectId",
|
"medusaId",
|
||||||
"type",
|
"type",
|
||||||
"collection",
|
"collection",
|
||||||
"tags",
|
"tags",
|
||||||
"handle",
|
"handle",
|
||||||
]
|
]
|
||||||
|
|
||||||
const requiredVariantFields = ["title", "sku", "prices", "options", "objectId"]
|
const requiredVariantFields = ["title", "sku", "prices", "options", "medusaId"]
|
||||||
|
|
||||||
export default checkContentTypes
|
export default checkContentTypes
|
||||||
@@ -111,10 +111,27 @@ class ContentfulService extends BaseService {
|
|||||||
return assets
|
return assets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getCustomField(field, type) {
|
||||||
|
const customOptions = this.options_[`custom_${type}_fields`]
|
||||||
|
|
||||||
|
if (customOptions) {
|
||||||
|
return customOptions[field] || field
|
||||||
|
} else {
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async createProductInContentful(product) {
|
async createProductInContentful(product) {
|
||||||
try {
|
try {
|
||||||
const p = await this.productService_.retrieve(product.id, {
|
const p = await this.productService_.retrieve(product.id, {
|
||||||
relations: ["variants", "options", "tags", "type", "collection"],
|
relations: [
|
||||||
|
"variants",
|
||||||
|
"options",
|
||||||
|
"tags",
|
||||||
|
"type",
|
||||||
|
"collection",
|
||||||
|
"images",
|
||||||
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
@@ -122,46 +139,92 @@ class ContentfulService extends BaseService {
|
|||||||
const variantLinks = this.getVariantLinks_(variantEntries)
|
const variantLinks = this.getVariantLinks_(variantEntries)
|
||||||
|
|
||||||
const fields = {
|
const fields = {
|
||||||
title: {
|
[this.getCustomField("title", "product")]: {
|
||||||
"en-US": p.title,
|
"en-US": p.title,
|
||||||
},
|
},
|
||||||
variants: {
|
[this.getCustomField("variants", "product")]: {
|
||||||
"en-US": variantLinks,
|
"en-US": variantLinks,
|
||||||
},
|
},
|
||||||
options: {
|
[this.getCustomField("options", "product")]: {
|
||||||
"en-US": p.options,
|
"en-US": p.options,
|
||||||
},
|
},
|
||||||
objectId: {
|
[this.getCustomField("medusaId", "product")]: {
|
||||||
"en-US": p.id,
|
"en-US": p.id,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (p.images.length > 0) {
|
||||||
|
const imageLinks = await this.createImageAssets(product)
|
||||||
|
|
||||||
|
const thumbnailAsset = await environment.createAsset({
|
||||||
|
fields: {
|
||||||
|
title: {
|
||||||
|
"en-US": `${p.title}`,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
"en-US": "",
|
||||||
|
},
|
||||||
|
file: {
|
||||||
|
"en-US": {
|
||||||
|
contentType: "image/xyz",
|
||||||
|
fileName: p.thumbnail,
|
||||||
|
upload: p.thumbnail,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await thumbnailAsset.processForAllLocales()
|
||||||
|
|
||||||
|
const thumbnailLink = {
|
||||||
|
sys: {
|
||||||
|
type: "Link",
|
||||||
|
linkType: "Asset",
|
||||||
|
id: thumbnailAsset.sys.id,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fields.thumbnail = {
|
||||||
|
"en-US": thumbnailLink,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (imageLinks) {
|
||||||
|
fields.images = {
|
||||||
|
"en-US": imageLinks,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (p.type) {
|
if (p.type) {
|
||||||
const type = {
|
const type = {
|
||||||
"en-US": p.type.value,
|
"en-US": p.type.value,
|
||||||
}
|
}
|
||||||
fields.type = type
|
|
||||||
|
fields[this.getCustomField("type", "product")] = type
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.collection) {
|
if (p.collection) {
|
||||||
const collection = {
|
const collection = {
|
||||||
"en-US": p.collection.title,
|
"en-US": p.collection.title,
|
||||||
}
|
}
|
||||||
fields.collection = collection
|
|
||||||
|
fields[this.getCustomField("collection", "product")] = collection
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.tags) {
|
if (p.tags) {
|
||||||
const tags = {
|
const tags = {
|
||||||
"en-US": p.tags,
|
"en-US": p.tags,
|
||||||
}
|
}
|
||||||
fields.tags = tags
|
|
||||||
|
fields[this.getCustomField("tags", "product")] = tags
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.handle) {
|
if (p.handle) {
|
||||||
const handle = {
|
const handle = {
|
||||||
"en-US": p.handle,
|
"en-US": p.handle,
|
||||||
}
|
}
|
||||||
fields.handle = handle
|
|
||||||
|
fields[this.getCustomField("handle", "product")] = handle
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await environment.createEntryWithId("product", p.id, {
|
const result = await environment.createEntryWithId("product", p.id, {
|
||||||
@@ -189,19 +252,19 @@ class ContentfulService extends BaseService {
|
|||||||
v.id,
|
v.id,
|
||||||
{
|
{
|
||||||
fields: {
|
fields: {
|
||||||
title: {
|
[this.getCustomField("title", "variant")]: {
|
||||||
"en-US": v.title,
|
"en-US": v.title,
|
||||||
},
|
},
|
||||||
sku: {
|
[this.getCustomField("sku", "variant")]: {
|
||||||
"en-US": v.sku,
|
"en-US": v.sku,
|
||||||
},
|
},
|
||||||
prices: {
|
[this.getCustomField("prices", "variant")]: {
|
||||||
"en-US": v.prices,
|
"en-US": v.prices,
|
||||||
},
|
},
|
||||||
options: {
|
[this.getCustomField("options", "variant")]: {
|
||||||
"en-US": v.options,
|
"en-US": v.options,
|
||||||
},
|
},
|
||||||
objectId: {
|
[this.getCustomField("medusaId", "variant")]: {
|
||||||
"en-US": v.id,
|
"en-US": v.id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -240,7 +303,14 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const p = await this.productService_.retrieve(product.id, {
|
const p = await this.productService_.retrieve(product.id, {
|
||||||
relations: ["options", "variants", "type", "collection", "tags"],
|
relations: [
|
||||||
|
"options",
|
||||||
|
"variants",
|
||||||
|
"type",
|
||||||
|
"collection",
|
||||||
|
"tags",
|
||||||
|
"images",
|
||||||
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
const variantEntries = await this.getVariantEntries_(p.variants)
|
const variantEntries = await this.getVariantEntries_(p.variants)
|
||||||
@@ -248,46 +318,86 @@ class ContentfulService extends BaseService {
|
|||||||
|
|
||||||
const productEntryFields = {
|
const productEntryFields = {
|
||||||
...productEntry.fields,
|
...productEntry.fields,
|
||||||
title: {
|
[this.getCustomField("title", "product")]: {
|
||||||
"en-US": p.title,
|
"en-US": p.title,
|
||||||
},
|
},
|
||||||
options: {
|
[this.getCustomField("options", "product")]: {
|
||||||
"en-US": p.options,
|
"en-US": p.options,
|
||||||
},
|
},
|
||||||
variants: {
|
[this.getCustomField("variants", "product")]: {
|
||||||
"en-US": variantLinks,
|
"en-US": variantLinks,
|
||||||
},
|
},
|
||||||
objectId: {
|
[this.getCustomField("medusaId", "product")]: {
|
||||||
"en-US": p.id,
|
"en-US": p.id,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (p.thumbnail) {
|
||||||
|
const thumbnailAsset = await environment.createAsset({
|
||||||
|
fields: {
|
||||||
|
title: {
|
||||||
|
"en-US": `${p.title}`,
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
"en-US": "",
|
||||||
|
},
|
||||||
|
file: {
|
||||||
|
"en-US": {
|
||||||
|
contentType: "image/xyz",
|
||||||
|
fileName: p.thumbnail,
|
||||||
|
upload: p.thumbnail,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await thumbnailAsset.processForAllLocales()
|
||||||
|
|
||||||
|
const thumbnailLink = {
|
||||||
|
sys: {
|
||||||
|
type: "Link",
|
||||||
|
linkType: "Asset",
|
||||||
|
id: thumbnailAsset.sys.id,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
productEntryFields.thumbnail = {
|
||||||
|
"en-US": thumbnailLink,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (p.type) {
|
if (p.type) {
|
||||||
const type = {
|
const type = {
|
||||||
"en-US": p.type.value,
|
"en-US": p.type.value,
|
||||||
}
|
}
|
||||||
productEntryFields.type = type
|
|
||||||
|
productEntryFields[this.getCustomField("type", "product")] = type
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.collection) {
|
if (p.collection) {
|
||||||
const collection = {
|
const collection = {
|
||||||
"en-US": p.collection.title,
|
"en-US": p.collection.title,
|
||||||
}
|
}
|
||||||
productEntryFields.collection = collection
|
|
||||||
|
productEntryFields[
|
||||||
|
this.getCustomField("collection", "product")
|
||||||
|
] = collection
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.tags) {
|
if (p.tags) {
|
||||||
const tags = {
|
const tags = {
|
||||||
"en-US": p.tags,
|
"en-US": p.tags,
|
||||||
}
|
}
|
||||||
productEntryFields.tags = tags
|
|
||||||
|
productEntryFields[this.getCustomField("tags", "product")] = tags
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.handle) {
|
if (p.handle) {
|
||||||
const handle = {
|
const handle = {
|
||||||
"en-US": p.handle,
|
"en-US": p.handle,
|
||||||
}
|
}
|
||||||
productEntryFields.handle = handle
|
|
||||||
|
productEntryFields[this.getCustomField("handle", "product")] = handle
|
||||||
}
|
}
|
||||||
|
|
||||||
productEntry.fields = productEntryFields
|
productEntry.fields = productEntryFields
|
||||||
@@ -333,19 +443,19 @@ class ContentfulService extends BaseService {
|
|||||||
|
|
||||||
const variantEntryFields = {
|
const variantEntryFields = {
|
||||||
...variantEntry.fields,
|
...variantEntry.fields,
|
||||||
title: {
|
[this.getCustomField("title", "variant")]: {
|
||||||
"en-US": v.title,
|
"en-US": v.title,
|
||||||
},
|
},
|
||||||
sku: {
|
[this.getCustomField("sku", "variant")]: {
|
||||||
"en-US": v.sku,
|
"en-US": v.sku,
|
||||||
},
|
},
|
||||||
options: {
|
[this.getCustomField("options", "variant")]: {
|
||||||
"en-US": v.options,
|
"en-US": v.options,
|
||||||
},
|
},
|
||||||
prices: {
|
[this.getCustomField("prices", "variant")]: {
|
||||||
"en-US": v.prices,
|
"en-US": v.prices,
|
||||||
},
|
},
|
||||||
objectId: {
|
[this.getCustomField("medusaId", "variant")]: {
|
||||||
"en-US": v.id,
|
"en-US": v.id,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -377,7 +487,8 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let update = {
|
let update = {
|
||||||
title: productEntry.fields.title["en-US"],
|
title:
|
||||||
|
productEntry.fields[this.getCustomField("title", "product")]["en-US"],
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the thumbnail, if present
|
// Get the thumbnail, if present
|
||||||
@@ -421,7 +532,10 @@ class ContentfulService extends BaseService {
|
|||||||
const updatedVariant = await this.productVariantService_.update(
|
const updatedVariant = await this.productVariantService_.update(
|
||||||
variantId,
|
variantId,
|
||||||
{
|
{
|
||||||
title: variantEntry.fields.title["en-US"],
|
title:
|
||||||
|
variantEntry.fields[this.getCustomField("title", "variant")][
|
||||||
|
"en-US"
|
||||||
|
],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user