Adds plugin for Contentful synchronization (#75)

Adds plugin for Contentful

Will create product and product variants in Contentful, when created in Medusa

Closes #47
This commit is contained in:
Oliver Windall Juhl
2020-07-02 09:41:57 +02:00
committed by GitHub
parent a7951ca29c
commit d56ccba2b1
20 changed files with 7662 additions and 109 deletions
@@ -13,6 +13,8 @@ class ProductVariantModel extends BaseModel {
static schema = {
title: { type: String, required: true },
prices: { type: [MoneyAmountSchema], default: [], required: true },
sku: { type: String, default: "" },
barcode: { type: String, default: "" },
options: { type: [OptionValueSchema], default: [] },
image: { type: String, default: "" },
published: { type: Boolean, default: false },
@@ -4,6 +4,7 @@ import ProductVariantService from "../product-variant"
import { ProductVariantModelMock } from "../../models/__mocks__/product-variant"
import { ProductServiceMock } from "../__mocks__/product"
import { RegionServiceMock } from "../__mocks__/region"
import { EventBusServiceMock } from "../__mocks__/event-bus"
describe("ProductVariantService", () => {
describe("retrieve", () => {
@@ -12,6 +13,7 @@ describe("ProductVariantService", () => {
beforeAll(async () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
res = await productVariantService.retrieve(IdMap.getId("validId"))
@@ -38,6 +40,7 @@ describe("ProductVariantService", () => {
beforeAll(async () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
await productVariantService
@@ -69,6 +72,7 @@ describe("ProductVariantService", () => {
jest.clearAllMocks()
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
productVariantService.createDraft({
@@ -106,6 +110,7 @@ describe("ProductVariantService", () => {
jest.clearAllMocks()
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
productVariantService.publish(IdMap.getId("variantId"))
@@ -124,6 +129,7 @@ describe("ProductVariantService", () => {
describe("update", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -168,6 +174,7 @@ describe("ProductVariantService", () => {
describe("decorate", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
const fakeVariant = {
@@ -216,6 +223,7 @@ describe("ProductVariantService", () => {
describe("setMetadata", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -264,6 +272,7 @@ describe("ProductVariantService", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
productService: ProductServiceMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -331,6 +340,7 @@ describe("ProductVariantService", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
productService: ProductServiceMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -354,6 +364,7 @@ describe("ProductVariantService", () => {
describe("delete", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -373,6 +384,7 @@ describe("ProductVariantService", () => {
describe("canCoverQuantity", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -419,6 +431,7 @@ describe("ProductVariantService", () => {
describe("setCurrencyPrice", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -519,6 +532,7 @@ describe("ProductVariantService", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
regionService: RegionServiceMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -619,6 +633,7 @@ describe("ProductVariantService", () => {
const productVariantService = new ProductVariantService({
productVariantModel: ProductVariantModelMock,
regionService: RegionServiceMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -6,6 +6,7 @@ import {
ProductVariantServiceMock,
variants,
} from "../__mocks__/product-variant"
import { EventBusServiceMock } from "../__mocks__/event-bus"
describe("ProductService", () => {
describe("retrieve", () => {
@@ -14,6 +15,7 @@ describe("ProductService", () => {
beforeAll(async () => {
const productService = new ProductService({
productModel: ProductModelMock,
eventBusService: EventBusServiceMock,
})
res = await productService.retrieve(IdMap.getId("validId"))
@@ -40,6 +42,7 @@ describe("ProductService", () => {
beforeAll(async () => {
const productService = new ProductService({
productModel: ProductModelMock,
eventBusService: EventBusServiceMock,
})
await productService.retrieve(IdMap.getId("failId")).catch(err => {
@@ -70,6 +73,7 @@ describe("ProductService", () => {
jest.clearAllMocks()
const productService = new ProductService({
productModel: ProductModelMock,
eventBusService: EventBusServiceMock,
})
productService.createDraft({
@@ -108,6 +112,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
productService.publish(IdMap.getId("productId"))
@@ -127,6 +132,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
const fakeProduct = {
@@ -193,6 +199,7 @@ describe("ProductService", () => {
describe("setMetadata", () => {
const productService = new ProductService({
productModel: ProductModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -236,6 +243,7 @@ describe("ProductService", () => {
describe("update", () => {
const productService = new ProductService({
productModel: ProductModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -290,6 +298,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
@@ -314,6 +323,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
@@ -479,6 +489,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
@@ -571,6 +582,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
@@ -632,6 +644,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
@@ -659,6 +672,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
@@ -719,6 +733,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
@@ -788,6 +803,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
@@ -843,6 +859,7 @@ describe("ProductService", () => {
const productService = new ProductService({
productModel: ProductModelMock,
productVariantService: ProductVariantServiceMock,
eventBusService: EventBusServiceMock,
})
afterEach(() => {
+131 -58
View File
@@ -7,6 +7,11 @@ import { Validator, MedusaError } from "medusa-core-utils"
* @implements BaseService
*/
class ProductVariantService extends BaseService {
static Events = {
UPDATED: "product-variant.updated",
CREATED: "product-variant.created",
}
/** @param { productVariantModel: (ProductVariantModel) } */
constructor({ productVariantModel, eventBusService, regionService }) {
super()
@@ -73,6 +78,10 @@ class ProductVariantService extends BaseService {
...productVariant,
published: false,
})
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.CREATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
@@ -83,9 +92,13 @@ class ProductVariantService extends BaseService {
* @param {string} variantId - ID of the variant to publish.
* @return {Promise} resolves to the creation result.
*/
publish(variantId) {
async publish(variantId) {
return this.productVariantModel_
.updateOne({ _id: variantId }, { $set: { published: true } })
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
@@ -123,6 +136,10 @@ class ProductVariantService extends BaseService {
{ $set: update },
{ runValidators: true }
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
@@ -163,33 +180,49 @@ class ProductVariantService extends BaseService {
})
}
return this.productVariantModel_.updateOne(
return this.productVariantModel_
.updateOne(
{
_id: variant._id,
},
{
$set: {
prices: newPrices,
},
}
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
return this.productVariantModel_
.updateOne(
{
_id: variant._id,
},
{
$set: {
prices: newPrices,
prices: [
{
currency_code: currencyCode,
amount,
},
],
},
}
)
}
return this.productVariantModel_.updateOne(
{
_id: variant._id,
},
{
$set: {
prices: [
{
currency_code: currencyCode,
amount,
},
],
},
}
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -262,39 +295,55 @@ class ProductVariantService extends BaseService {
})
}
return this.productVariantModel_.updateOne(
return this.productVariantModel_
.updateOne(
{
_id: variant._id,
},
{
$set: {
prices: newPrices,
},
}
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
// Set the price both for default currency price and for the region
return this.productVariantModel_
.updateOne(
{
_id: variant._id,
},
{
$set: {
prices: newPrices,
prices: [
{
region_id: region._id,
currency_code: region.currency_code,
amount,
},
{
currency_code: region.currency_code,
amount,
},
],
},
}
)
}
// Set the price both for default currency price and for the region
return this.productVariantModel_.updateOne(
{
_id: variant._id,
},
{
$set: {
prices: [
{
region_id: region._id,
currency_code: region.currency_code,
amount,
},
{
currency_code: region.currency_code,
amount,
},
],
},
}
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -313,10 +362,18 @@ class ProductVariantService extends BaseService {
)
}
return this.productVariantModel_.updateOne(
{ _id: variantId, "options.option_id": optionId },
{ $set: { "options.$.value": `${optionValue}` } }
)
return this.productVariantModel_
.updateOne(
{ _id: variantId, "options.option_id": optionId },
{ $set: { "options.$.value": `${optionValue}` } }
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -340,10 +397,18 @@ class ProductVariantService extends BaseService {
)
}
return this.productVariantModel_.updateOne(
{ _id: variant._id },
{ $push: { options: { option_id: optionId, value: `${optionValue}` } } }
)
return this.productVariantModel_
.updateOne(
{ _id: variant._id },
{ $push: { options: { option_id: optionId, value: `${optionValue}` } } }
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -357,10 +422,18 @@ class ProductVariantService extends BaseService {
* @return {Promise} the result of the update operation.
*/
async deleteOptionValue(variantId, optionId) {
return this.productVariantModel_.updateOne(
{ _id: variantId },
{ $pull: { options: { option_id: optionId } } }
)
return this.productVariantModel_
.updateOne(
{ _id: variantId },
{ $pull: { options: { option_id: optionId } } }
)
.then(result => {
this.eventBus_.emit(ProductVariantService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -384,7 +457,7 @@ class ProductVariantService extends BaseService {
* @param {Object} selector - the query object for find
* @return {Promise} the result of the find operation
*/
list(selector) {
async list(selector) {
return this.productVariantModel_.find(selector)
}
+120 -51
View File
@@ -8,6 +8,11 @@ import { BaseService } from "medusa-interfaces"
* @implements BaseService
*/
class ProductService extends BaseService {
static Events = {
UPDATED: "product.updated",
CREATED: "product.created",
}
/** @param { productModel: (ProductModel) } */
constructor({ productModel, eventBusService, productVariantService }) {
super()
@@ -86,12 +91,16 @@ class ProductService extends BaseService {
* @param {object} product - the product to create
* @return {Promise} resolves to the creation result.
*/
createDraft(product) {
async createDraft(product) {
return this.productModel_
.create({
...product,
published: false,
})
.then(result => {
this.eventBus_.emit(ProductService.Events.CREATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
@@ -102,9 +111,13 @@ class ProductService extends BaseService {
* @param {string} productId - ID of the product to publish.
* @return {Promise} resolves to the creation result.
*/
publish(productId) {
async publish(productId) {
return this.productModel_
.updateOne({ _id: productId }, { $set: { published: true } })
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
@@ -119,7 +132,7 @@ class ProductService extends BaseService {
* @param {object} update - an object with the update values.
* @return {Promise} resolves to the update result.
*/
update(productId, update) {
async update(productId, update) {
const validatedId = this.validateId_(productId)
if (update.metadata) {
@@ -142,6 +155,10 @@ class ProductService extends BaseService {
{ $set: update },
{ runValidators: true }
)
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
@@ -225,10 +242,15 @@ class ProductService extends BaseService {
const newVariant = await this.productVariantService_.createDraft(variant)
return this.productModel_.updateOne(
{ _id: product._id },
{ $push: { variants: newVariant._id } }
)
return this.productModel_
.updateOne({ _id: product._id }, { $push: { variants: newVariant._id } })
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -262,7 +284,7 @@ class ProductService extends BaseService {
"Default Value"
)
)
).catch(err => {
).catch(async err => {
// If any of the variants failed to add the new option value we clean up
return Promise.all(
product.variants.map(async variantId =>
@@ -288,7 +310,11 @@ class ProductService extends BaseService {
},
}
)
.catch(err => {
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(async err => {
// If we failed to update the product clean up its variants
return Promise.all(
product.variants.map(async variantId =>
@@ -322,14 +348,22 @@ class ProductService extends BaseService {
return variant
})
return this.productModel_.updateOne(
{
_id: productId,
},
{
$set: { variants: newOrder },
}
)
return this.productModel_
.updateOne(
{
_id: productId,
},
{
$set: { variants: newOrder },
}
)
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -363,14 +397,22 @@ class ProductService extends BaseService {
return option
})
return this.productModel_.updateOne(
{
_id: productId,
},
{
$set: { options: newOrder },
}
)
return this.productModel_
.updateOne(
{
_id: productId,
},
{
$set: { options: newOrder },
}
)
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -407,15 +449,23 @@ class ProductService extends BaseService {
const update = {}
update["options.$.title"] = title
return this.productModel_.updateOne(
{
_id: productId,
"options._id": optionId,
},
{
$set: update,
}
)
return this.productModel_
.updateOne(
{
_id: productId,
"options._id": optionId,
},
{
$set: update,
}
)
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -463,16 +513,23 @@ class ProductService extends BaseService {
}
}
const result = await this.productModel_.updateOne(
{ _id: productId },
{
$pull: {
options: {
_id: optionId,
const result = await this.productModel_
.updateOne(
{ _id: productId },
{
$pull: {
options: {
_id: optionId,
},
},
},
}
)
}
)
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
// If we reached this point, we can delete option value from variants
if (product.variants) {
@@ -497,14 +554,22 @@ class ProductService extends BaseService {
await this.productVariantService_.delete(variantId)
return this.productModel_.updateOne(
{ _id: product._id },
{
$pull: {
variants: variantId,
},
}
)
return this.productModel_
.updateOne(
{ _id: product._id },
{
$pull: {
variants: variantId,
},
}
)
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
async updateOptionValue(productId, variantId, optionId, value) {
@@ -597,6 +662,10 @@ class ProductService extends BaseService {
const keyPath = `metadata.${key}`
return this.productModel_
.updateOne({ _id: validatedId }, { $set: { [keyPath]: value } })
.then(result => {
this.eventBus_.emit(ProductService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})