chore(medusa-plugin-contentful): Cleanup plugin (#3990)

This commit is contained in:
Philip Korsholm authored and GitHub committed 2023-05-07 13:01:12 +02:00
1 parent 3d6bcaaf65
commit eff9f4c6f9
1 file changed
+12 -75
@@ -1,5 +1,5 @@
import _ from "lodash"
import { BaseService } from "medusa-interfaces"
import _ from "lodash"
import { createClient } from "contentful-management"
const IGNORE_THRESHOLD = 2 // seconds
@@ -51,17 +51,12 @@ class ContentfulService extends BaseService {
}
async getContentfulEnvironment_() {
try {
const space = await this.contentful_.getSpace(this.options_.space_id)
const environment = await space.getEnvironment(this.options_.environment)
return environment
} catch (error) {
throw error
}
}
async getVariantEntries_(variants, config = { publish: false }) {
try {
const contentfulVariants = await Promise.all(
variants.map(async (variant) => {
let updated = await this.updateProductVariantInContentful(variant)
@@ -74,9 +69,6 @@ class ContentfulService extends BaseService {
)
return contentfulVariants
} catch (error) {
throw error
}
}
getLink_(fromEntry) {
@@ -147,7 +139,6 @@ class ContentfulService extends BaseService {
}
async createProductInContentful(product) {
try {
const p = await this.productService_.retrieve(product.id, {
relations: [
"variants",
@@ -236,22 +227,15 @@ class ContentfulService extends BaseService {
})
return result
} catch (error) {
throw error
}
}
async createProductVariantInContentful(variant) {
try {
const v = await this.productVariantService_.retrieve(variant.id, {
relations: ["prices", "options"],
})
const environment = await this.getContentfulEnvironment_()
const result = await environment.createEntryWithId(
"productVariant",
v.id,
{
const result = await environment.createEntryWithId("productVariant", v.id, {
fields: {
[this.getCustomField("title", "variant")]: {
"en-US": v.title,
@@ -269,13 +253,9 @@ class ContentfulService extends BaseService {
"en-US": v.id,
},
},
}
)
})
return result
} catch (error) {
throw error
}
}
async createRegionInContentful(region) {
@@ -285,7 +265,6 @@ class ContentfulService extends BaseService {
if (!hasType) {
return Promise.resolve()
}
try {
const r = await this.regionService_.retrieve(region.id, {
relations: ["countries", "payment_providers", "fulfillment_providers"],
})
@@ -315,9 +294,6 @@ class ContentfulService extends BaseService {
})
return result
} catch (error) {
throw error
}
}
async updateRegionInContentful(data) {
@@ -341,7 +317,6 @@ class ContentfulService extends BaseService {
return
}
try {
const ignore = await this.shouldIgnore_(data.id, "contentful")
if (ignore) {
return
@@ -385,9 +360,6 @@ class ContentfulService extends BaseService {
await this.addIgnore_(data.id, "medusa")
return publishedEntry
} catch (error) {
throw error
}
}
async createCollectionInContentful(collection) {
@@ -529,7 +501,6 @@ class ContentfulService extends BaseService {
return Promise.resolve()
}
try {
const ignore = await this.shouldIgnore_(data.id, "contentful")
if (ignore) {
return Promise.resolve()
@@ -630,9 +601,6 @@ class ContentfulService extends BaseService {
await this.addIgnore_(data.id, "medusa")
return publishedEntry
} catch (error) {
throw error
}
}
async createSpecialProductFields(fields, p) {
@@ -711,7 +679,6 @@ class ContentfulService extends BaseService {
}
}
try {
const ignore = await this.shouldIgnore_(variant.id, "contentful")
if (ignore) {
return Promise.resolve()
@@ -758,14 +725,10 @@ class ContentfulService extends BaseService {
await this.addIgnore_(variant.id, "medusa")
return publishedEntry
} catch (error) {
throw error
}
}
async archiveProductVariantInContentful(variant) {
let variantEntity
try {
const ignore = await this.shouldIgnore_(variant.id, "contentful")
if (ignore) {
return Promise.resolve()
@@ -782,14 +745,10 @@ class ContentfulService extends BaseService {
}
return await this.archiveEntryWidthId(variant.id)
} catch (error) {
throw error
}
}
async archiveProductInContentful(product) {
let productEntity
try {
const ignore = await this.shouldIgnore_(product.id, "contentful")
if (ignore) {
return Promise.resolve()
@@ -797,21 +756,19 @@ class ContentfulService extends BaseService {
try {
productEntity = await this.productService_.retrieve(product.id)
} catch (err) {}
} catch (err) {
// noop
}
if (productEntity) {
return Promise.resolve()
}
return await this.archiveEntryWidthId(product.id)
} catch (error) {
throw error
}
}
async archiveRegionInContentful(region) {
let regionEntity
try {
const ignore = await this.shouldIgnore_(region.id, "contentful")
if (ignore) {
return Promise.resolve()
@@ -819,16 +776,15 @@ class ContentfulService extends BaseService {
try {
regionEntity = await this.regionService_.retrieve(region.id)
} catch (err) {}
} catch (err) {
// noop
}
if (regionEntity) {
return Promise.resolve()
}
return await this.archiveEntryWidthId(region.id)
} catch (error) {
throw error
}
}
async archiveEntryWidthId(id) {
@@ -855,19 +811,11 @@ class ContentfulService extends BaseService {
return
}
try {
const environment = await this.getContentfulEnvironment_()
const productEntry = await environment.getEntry(productId)
const product = await this.productService_.retrieve(productId, {
select: [
"id",
"handle",
"title",
"subtitle",
"description",
"thumbnail",
],
select: ["id", "handle", "title", "subtitle", "description", "thumbnail"],
})
const update = {}
@@ -876,9 +824,7 @@ class ContentfulService extends BaseService {
productEntry.fields[this.getCustomField("title", "product")]?.["en-US"]
const subtitle =
productEntry.fields[this.getCustomField("subtitle", "product")]?.[
"en-US"
]
productEntry.fields[this.getCustomField("subtitle", "product")]?.["en-US"]
const description =
productEntry.fields[this.getCustomField("description", "product")]?.[
@@ -922,9 +868,6 @@ class ContentfulService extends BaseService {
return await this.addIgnore_(productId, "contentful")
})
}
} catch (error) {
throw error
}
}
async sendContentfulProductVariantToAdmin(variantId) {
@@ -933,25 +876,19 @@ class ContentfulService extends BaseService {
return
}
try {
const environment = await this.getContentfulEnvironment_()
const variantEntry = await environment.getEntry(variantId)
const updatedVariant = await this.productVariantService_
.update(variantId, {
title:
variantEntry.fields[this.getCustomField("title", "variant")][
"en-US"
],
variantEntry.fields[this.getCustomField("title", "variant")]["en-US"],
})
.then(async () => {
return await this.addIgnore_(variantId, "contentful")
})
return updatedVariant
} catch (error) {
throw error
}
}
transformMedusaIds(objOrArray) {