fix: region sync

This commit is contained in:
Sebastian Rindom
2021-06-15 15:45:59 +02:00
parent 27173d7e6a
commit 8e29e6e63c
6 changed files with 48 additions and 133 deletions
@@ -1,97 +0,0 @@
import ContentfulService from "../contentful"
import { MockEnvironment } from "../../../__mocks__/contentful-management"
jest.mock("contentful-management")
describe("contentfulService", () => {
const redisClient = {
set: jest.fn(),
get: jest.fn(),
}
const ProductVariantService = {
retrieve: jest.fn(() =>
Promise.resolve({
id: "variant_medusa",
title: "testvar",
})
),
}
const ProductService = {
retrieve: jest.fn(() =>
Promise.resolve({
id: "product_medusa",
title: "test",
subtitle: "subtest",
description: "something long",
variants: [
{
id: "variant_medusa",
title: "testvar",
},
],
})
),
}
describe("createProductInContentful", () => {
beforeEach(() => {
jest.clearAllMocks()
})
it("resolves", async () => {
const contentfulService = new ContentfulService(
{
redisClient,
productVariantService: ProductVariantService,
productService: ProductService,
},
{}
)
await contentfulService.updateProductInContentful({
id: "testId",
fields: ["title"],
})
})
})
describe("createImageAssets", () => {
beforeEach(() => {
jest.clearAllMocks()
})
it("successfully creates assets", async () => {
const contentfulService = new ContentfulService(
{
redisClient,
},
{}
)
await contentfulService.createImageAssets({
title: "testprod",
images: [{ url: "test123" }],
})
expect(MockEnvironment.createAsset).toHaveBeenCalledTimes(1)
expect(MockEnvironment.createAsset).toHaveBeenCalledWith({
fields: {
title: {
"en-US": `testprod - 0`,
},
description: {
"en-US": "",
},
file: {
"en-US": {
contentType: "image/xyz",
fileName: "test123",
upload: "test123",
},
},
},
})
})
})
})
@@ -6,7 +6,13 @@ const IGNORE_THRESHOLD = 2 // seconds
class ContentfulService extends BaseService {
constructor(
{ productService, redisClient, productVariantService, eventBusService },
{
regionService,
productService,
redisClient,
productVariantService,
eventBusService,
},
options
) {
super()
@@ -15,6 +21,8 @@ class ContentfulService extends BaseService {
this.productVariantService_ = productVariantService
this.regionService_ = regionService
this.eventBus_ = eventBusService
this.options_ = options
@@ -306,28 +314,31 @@ class ContentfulService extends BaseService {
return
}
try {
const r = await this.regionService_.retrieve(data.id, {
const r = await this.regionService_.retrieve(region.id, {
relations: ["countries", "payment_providers", "fulfillment_providers"],
})
const environment = await this.getContentfulEnvironment_()
const fields = {
[this.getCustomField("medusaId", "product")]: {
"en-US": r.id,
},
[this.getCustomField("name", "region")]: {
"en-US": r.name,
},
[this.getCustomField("countries", "region")]: {
"en-US": r.countries,
},
[this.getCustomField("payment_providers", "region")]: {
[this.getCustomField("paymentProviders", "region")]: {
"en-US": r.payment_providers,
},
[this.getCustomField("fulfillment_providers", "region")]: {
[this.getCustomField("fulfillmentProviders", "region")]: {
"en-US": r.fulfillment_providers,
},
}
const result = await environment.createEntryWithId("region", p.id, {
const result = await environment.createEntryWithId("region", r.id, {
fields,
})
@@ -385,10 +396,10 @@ class ContentfulService extends BaseService {
[this.getCustomField("countries", "region")]: {
"en-US": r.countries,
},
[this.getCustomField("payment_providers", "region")]: {
[this.getCustomField("paymentProviders", "region")]: {
"en-US": r.payment_providers,
},
[this.getCustomField("fulfillment_providers", "region")]: {
[this.getCustomField("fulfillmentProviders", "region")]: {
"en-US": r.fulfillment_providers,
},
}
@@ -10,6 +10,14 @@ class ContentfulSubscriber {
this.contentfulService_ = contentfulService
this.eventBus_ = eventBusService
this.eventBus_.subscribe("region.created", async (data) => {
await this.contentfulService_.createRegionInContentful(data)
})
this.eventBus_.subscribe("region.updated", async (data) => {
await this.contentfulService_.updateRegionInContentful(data)
})
this.eventBus_.subscribe("product-variant.updated", async (data) => {
await this.contentfulService_.updateProductVariantInContentful(data)
})