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

View File

@@ -1,13 +1,13 @@
let ignore = [`**/dist`]
let ignore = [`**/dist`];
// Jest needs to compile this code, but generally we don't want this copied
// to output folders
if (process.env.NODE_ENV !== `test`) {
ignore.push(`**/__tests__`)
ignore.push(`**/__tests__`);
}
module.exports = {
sourceMaps: true,
presets: ["babel-preset-medusa-package"],
ignore,
}
};

View File

@@ -1,26 +0,0 @@
export const MockEntry = {
update: jest.fn(() => Promise.resolve(MockEntry)),
publish: jest.fn(() => Promise.resolve({ sys: { id: "test" } })),
}
export const MockAsset = {
processForAllLocales: jest.fn(() => Promise.resolve(MockAsset)),
publish: jest.fn(() => Promise.resolve({ sys: { id: "test" } })),
}
export const MockEnvironment = {
createAsset: jest.fn((d) => Promise.resolve(MockAsset)),
createEntryWithId: jest.fn(() => Promise.resolve(MockEntry)),
getEntry: jest.fn(() => Promise.resolve(MockEntry)),
getContentType: jest.fn(() => Promise.resolve({})),
}
export const MockSpace = {
getEnvironment: jest.fn(() => Promise.resolve(MockEnvironment)),
}
export const MockClient = {
getSpace: jest.fn(() => Promise.resolve(MockSpace)),
}
export const createClient = jest.fn(() => MockClient)

View File

@@ -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",
},
},
},
})
})
})
})

View File

@@ -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,
},
}

View File

@@ -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)
})

View File

@@ -1,6 +1,14 @@
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
import RegionService from "../region"
const eventBusService = {
emit: jest.fn(),
withTransaction: function() {
return this
},
}
describe("RegionService", () => {
describe("create", () => {
const regionRepository = MockRepository({})
@@ -58,6 +66,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
fulfillmentProviderRepository: fpRepository,
paymentProviderRepository: ppRepository,
currencyRepository,
@@ -168,6 +177,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
regionRepository,
})
@@ -237,6 +247,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
fulfillmentProviderRepository: fpRepository,
paymentProviderRepository: ppRepository,
regionRepository,
@@ -335,6 +346,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
fulfillmentProviderRepository: fpRepository,
paymentProviderRepository: ppRepository,
regionRepository,
@@ -380,6 +392,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
regionRepository,
})
@@ -429,6 +442,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
regionRepository,
countryRepository,
})
@@ -473,6 +487,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
regionRepository,
})
@@ -522,6 +537,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
fulfillmentProviderRepository: fpRepository,
paymentProviderRepository: ppRepository,
regionRepository,
@@ -582,6 +598,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
fulfillmentProviderRepository: fpRepository,
regionRepository,
})
@@ -631,6 +648,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
regionRepository,
})
@@ -665,6 +683,7 @@ describe("RegionService", () => {
const regionService = new RegionService({
manager: MockManager,
eventBusService,
regionRepository,
})