chore: testing skeleton
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
module.exports = {
|
||||||
|
mockClient: {
|
||||||
|
|
||||||
|
},
|
||||||
|
createClient: () => {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
describe("contentfulService", () => {
|
||||||
|
const redisClient = {
|
||||||
|
set: jest.fn(),
|
||||||
|
get: jest.fn()
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("", () => {
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -2,6 +2,8 @@ import _ from "lodash"
|
|||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import { createClient } from "contentful-management"
|
import { createClient } from "contentful-management"
|
||||||
|
|
||||||
|
const IGNORE_THRESHOLD = 1 // seconds
|
||||||
|
|
||||||
class ContentfulService extends BaseService {
|
class ContentfulService extends BaseService {
|
||||||
constructor(
|
constructor(
|
||||||
{ productService, redisClient, productVariantService, eventBusService },
|
{ productService, redisClient, productVariantService, eventBusService },
|
||||||
@@ -24,16 +26,14 @@ class ContentfulService extends BaseService {
|
|||||||
this.redis_ = redisClient
|
this.redis_ = redisClient
|
||||||
}
|
}
|
||||||
|
|
||||||
async getIgnoreIds_(type) {
|
async addIgnore_(id, side) {
|
||||||
return new Promise((resolve, reject) => {
|
const key = `${id}_ignore_${side}`
|
||||||
this.redis_.get(`${type}_ignore_ids`, (err, reply) => {
|
return await this.redis_.set(key, 1, "EX", IGNORE_THRESHOLD)
|
||||||
if (err) {
|
|
||||||
return reject(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(JSON.parse(reply))
|
async shouldIgnore_(id, side) {
|
||||||
})
|
const key = `${id}_ignore_${side}`
|
||||||
})
|
return await this.redis_.get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
async getContentfulEnvironment_() {
|
async getContentfulEnvironment_() {
|
||||||
@@ -46,12 +46,17 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getVariantEntries_(variants) {
|
async getVariantEntries_(variants, config = { publish: false }) {
|
||||||
try {
|
try {
|
||||||
const contentfulVariants = await Promise.all(
|
const contentfulVariants = await Promise.all(
|
||||||
variants.map((variant) =>
|
variants.map(async (variant) => {
|
||||||
this.updateProductVariantInContentful(variant)
|
let updated = await this.updateProductVariantInContentful(variant)
|
||||||
)
|
if (config.publish) {
|
||||||
|
updated = updated.publish()
|
||||||
|
}
|
||||||
|
|
||||||
|
return updated
|
||||||
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
return contentfulVariants
|
return contentfulVariants
|
||||||
@@ -137,13 +142,21 @@ class ContentfulService extends BaseService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
const variantEntries = await this.getVariantEntries_(p.variants)
|
const variantEntries = await this.getVariantEntries_(p.variants, {
|
||||||
|
publish: true,
|
||||||
|
})
|
||||||
const variantLinks = this.getVariantLinks_(variantEntries)
|
const variantLinks = this.getVariantLinks_(variantEntries)
|
||||||
|
|
||||||
const fields = {
|
const fields = {
|
||||||
[this.getCustomField("title", "product")]: {
|
[this.getCustomField("title", "product")]: {
|
||||||
"en-US": p.title,
|
"en-US": p.title,
|
||||||
},
|
},
|
||||||
|
[this.getCustomField("subtitle", "product")]: {
|
||||||
|
"en-US": p.subtitle,
|
||||||
|
},
|
||||||
|
[this.getCustomField("description", "product")]: {
|
||||||
|
"en-US": p.description,
|
||||||
|
},
|
||||||
[this.getCustomField("variants", "product")]: {
|
[this.getCustomField("variants", "product")]: {
|
||||||
"en-US": variantLinks,
|
"en-US": variantLinks,
|
||||||
},
|
},
|
||||||
@@ -157,7 +170,14 @@ class ContentfulService extends BaseService {
|
|||||||
|
|
||||||
if (p.images.length > 0) {
|
if (p.images.length > 0) {
|
||||||
const imageLinks = await this.createImageAssets(product)
|
const imageLinks = await this.createImageAssets(product)
|
||||||
|
if (imageLinks) {
|
||||||
|
fields.images = {
|
||||||
|
"en-US": imageLinks,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.thumbnail) {
|
||||||
const thumbnailAsset = await environment.createAsset({
|
const thumbnailAsset = await environment.createAsset({
|
||||||
fields: {
|
fields: {
|
||||||
title: {
|
title: {
|
||||||
@@ -176,7 +196,7 @@ class ContentfulService extends BaseService {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
await thumbnailAsset.processForAllLocales()
|
await thumbnailAsset.processForAllLocales().then((a) => a.publish())
|
||||||
|
|
||||||
const thumbnailLink = {
|
const thumbnailLink = {
|
||||||
sys: {
|
sys: {
|
||||||
@@ -189,12 +209,6 @@ class ContentfulService extends BaseService {
|
|||||||
fields.thumbnail = {
|
fields.thumbnail = {
|
||||||
"en-US": thumbnailLink,
|
"en-US": thumbnailLink,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imageLinks) {
|
|
||||||
fields.images = {
|
|
||||||
"en-US": imageLinks,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.type) {
|
if (p.type) {
|
||||||
@@ -233,9 +247,6 @@ class ContentfulService extends BaseService {
|
|||||||
fields,
|
fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
// const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
|
||||||
// ignoreIds.push(product.id)
|
|
||||||
// this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
@@ -288,6 +299,7 @@ class ContentfulService extends BaseService {
|
|||||||
"options",
|
"options",
|
||||||
"tags",
|
"tags",
|
||||||
"title",
|
"title",
|
||||||
|
"subtitle",
|
||||||
"tags",
|
"tags",
|
||||||
"type",
|
"type",
|
||||||
"type_id",
|
"type_id",
|
||||||
@@ -302,16 +314,10 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
const ignore = await this.shouldIgnore_(data.id, "contentful")
|
||||||
|
if (ignore) {
|
||||||
// if (ignoreIds.includes(product.id)) {
|
return
|
||||||
// const newIgnoreIds = ignoreIds.filter((id) => id !== product.id)
|
}
|
||||||
// this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds))
|
|
||||||
// return
|
|
||||||
// } else {
|
|
||||||
// ignoreIds.push(product.id)
|
|
||||||
// this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
|
||||||
// }
|
|
||||||
|
|
||||||
const p = await this.productService_.retrieve(data.id, {
|
const p = await this.productService_.retrieve(data.id, {
|
||||||
relations: [
|
relations: [
|
||||||
@@ -341,6 +347,12 @@ class ContentfulService extends BaseService {
|
|||||||
[this.getCustomField("title", "product")]: {
|
[this.getCustomField("title", "product")]: {
|
||||||
"en-US": p.title,
|
"en-US": p.title,
|
||||||
},
|
},
|
||||||
|
[this.getCustomField("subtitle", "product")]: {
|
||||||
|
"en-US": p.subtitle,
|
||||||
|
},
|
||||||
|
[this.getCustomField("description", "product")]: {
|
||||||
|
"en-US": p.description,
|
||||||
|
},
|
||||||
[this.getCustomField("options", "product")]: {
|
[this.getCustomField("options", "product")]: {
|
||||||
"en-US": p.options,
|
"en-US": p.options,
|
||||||
},
|
},
|
||||||
@@ -376,7 +388,7 @@ class ContentfulService extends BaseService {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
await thumbnailAsset.processForAllLocales()
|
await thumbnailAsset.processForAllLocales().then((a) => a.publish())
|
||||||
|
|
||||||
const thumbnailLink = {
|
const thumbnailLink = {
|
||||||
sys: {
|
sys: {
|
||||||
@@ -430,6 +442,8 @@ class ContentfulService extends BaseService {
|
|||||||
const updatedEntry = await productEntry.update()
|
const updatedEntry = await productEntry.update()
|
||||||
const publishedEntry = await updatedEntry.publish()
|
const publishedEntry = await updatedEntry.publish()
|
||||||
|
|
||||||
|
await this.addIgnore_(data.id, "medusa")
|
||||||
|
|
||||||
return publishedEntry
|
return publishedEntry
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
@@ -460,19 +474,10 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
const ignore = await this.shouldIgnore_(variant.id, "contentful")
|
||||||
|
if (ignore) {
|
||||||
//if (ignoreIds.includes(variant.id)) {
|
return
|
||||||
// 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
|
||||||
@@ -512,6 +517,8 @@ class ContentfulService extends BaseService {
|
|||||||
const updatedEntry = await variantEntry.update()
|
const updatedEntry = await variantEntry.update()
|
||||||
const publishedEntry = await updatedEntry.publish()
|
const publishedEntry = await updatedEntry.publish()
|
||||||
|
|
||||||
|
await this.addIgnore_(variant.id, "medusa")
|
||||||
|
|
||||||
return publishedEntry
|
return publishedEntry
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
@@ -519,29 +526,42 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sendContentfulProductToAdmin(productId) {
|
async sendContentfulProductToAdmin(productId) {
|
||||||
|
const ignore = await this.shouldIgnore_(productId, "medusa")
|
||||||
|
if (ignore) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
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)
|
||||||
|
|
||||||
//const ignoreIds = (await this.getIgnoreIds_("product")) || []
|
|
||||||
//if (ignoreIds.includes(productId)) {
|
|
||||||
// const newIgnoreIds = ignoreIds.filter((id) => id !== productId)
|
|
||||||
// this.redis_.set("product_ignore_ids", JSON.stringify(newIgnoreIds))
|
|
||||||
// return
|
|
||||||
//} else {
|
|
||||||
// ignoreIds.push(productId)
|
|
||||||
// this.redis_.set("product_ignore_ids", JSON.stringify(ignoreIds))
|
|
||||||
//}
|
|
||||||
|
|
||||||
let update = {}
|
let update = {}
|
||||||
|
|
||||||
const title =
|
const title =
|
||||||
productEntry.fields[this.getCustomField("title", "product")]["en-US"]
|
productEntry.fields[this.getCustomField("title", "product")]["en-US"]
|
||||||
|
|
||||||
|
const subtitle =
|
||||||
|
productEntry.fields[this.getCustomField("subtitle", "product")]["en-US"]
|
||||||
|
|
||||||
|
const description =
|
||||||
|
productEntry.fields[this.getCustomField("description", "product")][
|
||||||
|
"en-US"
|
||||||
|
]
|
||||||
|
|
||||||
if (product.title !== title) {
|
if (product.title !== title) {
|
||||||
update.title = title
|
update.title = title
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (product.subtitle !== subtitle) {
|
||||||
|
update.subtitle = subtitle
|
||||||
|
}
|
||||||
|
|
||||||
|
if (product.description !== description) {
|
||||||
|
update.description = description
|
||||||
|
}
|
||||||
|
|
||||||
// Get the thumbnail, if present
|
// Get the thumbnail, if present
|
||||||
if (productEntry.fields.thumbnail) {
|
if (productEntry.fields.thumbnail) {
|
||||||
const thumb = await environment.getAsset(
|
const thumb = await environment.getAsset(
|
||||||
@@ -556,7 +576,9 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!_.isEmpty(update)) {
|
if (!_.isEmpty(update)) {
|
||||||
await this.productService_.update(productId, update)
|
await this.productService_.update(productId, update).then(async () => {
|
||||||
|
return await this.addIgnore_(productId, "contentful")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
@@ -564,32 +586,25 @@ class ContentfulService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sendContentfulProductVariantToAdmin(variantId) {
|
async sendContentfulProductVariantToAdmin(variantId) {
|
||||||
|
const ignore = this.shouldIgnore_(variantId, "medusa")
|
||||||
|
if (ignore) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const environment = await this.getContentfulEnvironment_()
|
const environment = await this.getContentfulEnvironment_()
|
||||||
const variantEntry = await environment.getEntry(variantId)
|
const variantEntry = await environment.getEntry(variantId)
|
||||||
|
|
||||||
// const ignoreIds = (await this.getIgnoreIds_("product_variant")) || []
|
const updatedVariant = await this.productVariantService_
|
||||||
// if (ignoreIds.includes(variantId)) {
|
.update(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))
|
|
||||||
// }
|
|
||||||
|
|
||||||
const updatedVariant = await this.productVariantService_.update(
|
|
||||||
variantId,
|
|
||||||
{
|
|
||||||
title:
|
title:
|
||||||
variantEntry.fields[this.getCustomField("title", "variant")][
|
variantEntry.fields[this.getCustomField("title", "variant")][
|
||||||
"en-US"
|
"en-US"
|
||||||
],
|
],
|
||||||
}
|
})
|
||||||
)
|
.then(async () => {
|
||||||
|
return await this.addIgnore_(variantId, "contentful")
|
||||||
|
})
|
||||||
|
|
||||||
return updatedVariant
|
return updatedVariant
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user