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