feat(medusa): Product category, type and tags

This commit is contained in:
Oliver Windall Juhl
2021-02-12 08:42:19 +01:00
committed by GitHub
parent 2a8b556256
commit c4d1203155
45 changed files with 1704 additions and 45 deletions
@@ -0,0 +1,59 @@
const checkContentTypes = async (container) => {
const contentfulService = container.resolve("contentfulService")
let product
let variant
try {
product = await contentfulService.getType("product")
variant = await contentfulService.getType("productVariant")
} catch (error) {
if (!product) {
throw Error("Content type: `product` is missing in Contentful")
}
if (!variant) {
throw Error("Content type: `productVariant` is missing in Contentful")
}
}
if (product && product.fields) {
const productFields = product.fields
const keys = Object.values(productFields).map((f) => f.id)
if (!requiredProductFields.every((f) => keys.includes(f))) {
throw Error(
`Contentful: Content type ${`product`} is missing some required key(s). Required: ${requiredProductFields.join(
", "
)}`
)
}
}
if (variant && variant.fields) {
const variantFields = variant.fields
const keys = Object.values(variantFields).map((f) => f.id)
if (!requiredVariantFields.every((f) => keys.includes(f))) {
throw Error(
`Contentful: Content type ${`productVariant`} is missing some required key(s). Required: ${requiredVariantFields.join(
", "
)}`
)
}
}
}
const requiredProductFields = [
"title",
"variants",
"options",
"objectId",
"type",
"collection",
"tags",
"handle",
]
const requiredVariantFields = ["title", "sku", "prices", "options", "objectId"]
export default checkContentTypes
@@ -114,28 +114,58 @@ class ContentfulService extends BaseService {
async createProductInContentful(product) {
try {
const p = await this.productService_.retrieve(product.id, {
relations: ["variants", "options"],
relations: ["variants", "options", "tags", "type", "collection"],
})
const environment = await this.getContentfulEnvironment_()
const variantEntries = await this.getVariantEntries_(p.variants)
const variantLinks = this.getVariantLinks_(variantEntries)
const result = await environment.createEntryWithId("product", p.id, {
fields: {
title: {
"en-US": p.title,
},
variants: {
"en-US": variantLinks,
},
options: {
"en-US": p.options,
},
objectId: {
"en-US": p.id,
},
const fields = {
title: {
"en-US": p.title,
},
variants: {
"en-US": variantLinks,
},
options: {
"en-US": p.options,
},
objectId: {
"en-US": p.id,
},
}
if (p.type) {
const type = {
"en-US": p.type.value,
}
fields.type = type
}
if (p.collection) {
const collection = {
"en-US": p.collection.title,
}
fields.collection = collection
}
if (p.tags) {
const tags = {
"en-US": p.tags,
}
fields.tags = tags
}
if (p.handle) {
const handle = {
"en-US": p.handle,
}
fields.handle = handle
}
const result = await environment.createEntryWithId("product", p.id, {
fields,
})
const ignoreIds = (await this.getIgnoreIds_("product")) || []
@@ -210,7 +240,7 @@ class ContentfulService extends BaseService {
}
const p = await this.productService_.retrieve(product.id, {
relations: ["options", "variants"],
relations: ["options", "variants", "type", "collection", "tags"],
})
const variantEntries = await this.getVariantEntries_(p.variants)
@@ -232,6 +262,34 @@ class ContentfulService extends BaseService {
},
}
if (p.type) {
const type = {
"en-US": p.type.value,
}
productEntryFields.type = type
}
if (p.collection) {
const collection = {
"en-US": p.collection.title,
}
productEntryFields.collection = collection
}
if (p.tags) {
const tags = {
"en-US": p.tags,
}
productEntryFields.tags = tags
}
if (p.handle) {
const handle = {
"en-US": p.handle,
}
productEntryFields.handle = handle
}
productEntry.fields = productEntryFields
const updatedEntry = await productEntry.update()
@@ -372,6 +430,11 @@ class ContentfulService extends BaseService {
throw error
}
}
async getType(type) {
const environment = await this.getContentfulEnvironment_()
return environment.getContentType(type)
}
}
export default ContentfulService