Replaces MongoDB support with PostgreSQL (#151)
- All schemas have been rewritten to a relational model - All services have been rewritten to accommodate the new data model - Adds idempotency keys to core endpoints allowing you to retry requests with no additional side effects - Adds staged jobs to avoid putting jobs in the queue when transactions abort - Adds atomic transactions to all methods with access to the data layer Co-authored-by: Oliver Windall Juhl <oliver@mrbltech.com>
This commit is contained in:
1 parent
5f819486fc
commit
f1baca3cbd
499 files changed
+25909
-16128
No files matched your search
@@ -1,96 +1,254 @@
|
||||
import mongoose from "mongoose"
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
||||
import LineItemService from "../line-item"
|
||||
import { ProductVariantServiceMock } from "../__mocks__/product-variant"
|
||||
import { ProductServiceMock } from "../__mocks__/product"
|
||||
import { RegionServiceMock } from "../__mocks__/region"
|
||||
|
||||
describe("LineItemService", () => {
|
||||
describe("generate", () => {
|
||||
let result
|
||||
beforeAll(async () => {
|
||||
jest.clearAllMocks()
|
||||
const lineItemService = new LineItemService({
|
||||
productVariantService: ProductVariantServiceMock,
|
||||
productService: ProductServiceMock,
|
||||
regionService: RegionServiceMock,
|
||||
})
|
||||
result = await lineItemService.generate(
|
||||
IdMap.getId("eur-10-us-12"),
|
||||
IdMap.getId("region-france"),
|
||||
2
|
||||
)
|
||||
describe("create", () => {
|
||||
const lineItemRepository = MockRepository({})
|
||||
|
||||
const cartRepository = MockRepository({
|
||||
findOne: () =>
|
||||
Promise.resolve({
|
||||
region_id: IdMap.getId("test-region"),
|
||||
}),
|
||||
})
|
||||
|
||||
it("generates line item and successfully defaults quantity of content to 1", () => {
|
||||
expect(result).toEqual({
|
||||
title: "test",
|
||||
description: "EUR10US-12",
|
||||
thumbnail: "test.1234",
|
||||
should_merge: true,
|
||||
content: {
|
||||
unit_price: 10,
|
||||
variant: {
|
||||
_id: IdMap.getId("eur-10-us-12"),
|
||||
title: "EUR10US-12",
|
||||
},
|
||||
const regionService = {
|
||||
retrieve: () => {
|
||||
return {
|
||||
id: IdMap.getId("test-region"),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const productVariantService = {
|
||||
retrieve: query => {
|
||||
if (query === IdMap.getId("test-giftcard")) {
|
||||
return {
|
||||
id: IdMap.getId("test-giftcard"),
|
||||
title: "Test variant",
|
||||
product: {
|
||||
title: "Test product",
|
||||
thumbnail: "",
|
||||
is_giftcard: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
return {
|
||||
id: IdMap.getId("test-variant"),
|
||||
title: "Test variant",
|
||||
product: {
|
||||
_id: "1234",
|
||||
title: "test",
|
||||
thumbnail: "test.1234",
|
||||
title: "Test product",
|
||||
thumbnail: "",
|
||||
},
|
||||
quantity: 1,
|
||||
},
|
||||
}
|
||||
},
|
||||
getRegionPrice: () => 100,
|
||||
}
|
||||
|
||||
const lineItemService = new LineItemService({
|
||||
manager: MockManager,
|
||||
lineItemRepository,
|
||||
productVariantService,
|
||||
regionService,
|
||||
cartRepository,
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("successfully create a line item", async () => {
|
||||
await lineItemService.create({
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 100,
|
||||
quantity: 1,
|
||||
})
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith({
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 100,
|
||||
quantity: 1,
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully create a line item with price and quantity", async () => {
|
||||
await lineItemService.create({
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
unit_price: 50,
|
||||
quantity: 2,
|
||||
})
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith({
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
unit_price: 50,
|
||||
quantity: 2,
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully create a line item giftcard", async () => {
|
||||
const line = await await lineItemService.generate(
|
||||
IdMap.getId("test-giftcard"),
|
||||
IdMap.getId("test-region")
|
||||
)
|
||||
|
||||
await lineItemService.create({
|
||||
...line,
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
})
|
||||
|
||||
expect(lineItemRepository.create).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.create).toHaveBeenCalledWith({
|
||||
allow_discounts: false,
|
||||
variant_id: IdMap.getId("test-giftcard"),
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 100,
|
||||
quantity: 1,
|
||||
is_giftcard: true,
|
||||
should_merge: true,
|
||||
metadata: {},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("generate with giftcard", () => {
|
||||
let result
|
||||
beforeAll(async () => {
|
||||
jest.clearAllMocks()
|
||||
const lineItemService = new LineItemService({
|
||||
productVariantService: ProductVariantServiceMock,
|
||||
productService: ProductServiceMock,
|
||||
regionService: RegionServiceMock,
|
||||
})
|
||||
result = await lineItemService.generate(
|
||||
IdMap.getId("giftCardVar"),
|
||||
IdMap.getId("region-france"),
|
||||
1,
|
||||
{
|
||||
name: "Test Name",
|
||||
}
|
||||
)
|
||||
describe("update", () => {
|
||||
const lineItemRepository = MockRepository({
|
||||
findOne: () =>
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("test-line-item"),
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
variant: {
|
||||
id: IdMap.getId("test-variant"),
|
||||
title: "Test variant",
|
||||
},
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
}),
|
||||
})
|
||||
|
||||
it("results correctly", () => {
|
||||
expect(result).toEqual({
|
||||
title: "Gift Card",
|
||||
description: "100 USD",
|
||||
thumbnail: "1234",
|
||||
is_giftcard: true,
|
||||
content: {
|
||||
unit_price: 100,
|
||||
variant: {
|
||||
_id: IdMap.getId("giftCardVar"),
|
||||
title: "100 USD",
|
||||
},
|
||||
product: {
|
||||
_id: IdMap.getId("giftCardProd"),
|
||||
title: "Gift Card",
|
||||
thumbnail: "1234",
|
||||
is_giftcard: true,
|
||||
},
|
||||
quantity: 1,
|
||||
const lineItemService = new LineItemService({
|
||||
manager: MockManager,
|
||||
lineItemRepository,
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("successfully updates a line item with quantity", async () => {
|
||||
await lineItemService.update(IdMap.getId("test-line-item"), {
|
||||
quantity: 2,
|
||||
has_shipping: true,
|
||||
})
|
||||
|
||||
expect(lineItemRepository.save).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.save).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("test-line-item"),
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
variant: {
|
||||
id: IdMap.getId("test-variant"),
|
||||
title: "Test variant",
|
||||
},
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 50,
|
||||
quantity: 2,
|
||||
has_shipping: true,
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully updates a line item with metadata", async () => {
|
||||
await lineItemService.update(IdMap.getId("test-line-item"), {
|
||||
metadata: {
|
||||
name: "Test Name",
|
||||
testKey: "testValue",
|
||||
},
|
||||
})
|
||||
|
||||
expect(lineItemRepository.save).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.save).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("test-line-item"),
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
variant: {
|
||||
id: IdMap.getId("test-variant"),
|
||||
title: "Test variant",
|
||||
},
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
metadata: {
|
||||
testKey: "testValue",
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
describe("delete", () => {
|
||||
const lineItemRepository = MockRepository({
|
||||
findOne: () =>
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("test-line-item"),
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
variant: {
|
||||
id: IdMap.getId("test-variant"),
|
||||
title: "Test variant",
|
||||
},
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
}),
|
||||
})
|
||||
|
||||
const lineItemService = new LineItemService({
|
||||
manager: MockManager,
|
||||
lineItemRepository,
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("successfully deletes", async () => {
|
||||
await lineItemService.delete(IdMap.getId("test-line-item"))
|
||||
|
||||
expect(lineItemRepository.remove).toHaveBeenCalledTimes(1)
|
||||
expect(lineItemRepository.remove).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("test-line-item"),
|
||||
variant_id: IdMap.getId("test-variant"),
|
||||
variant: {
|
||||
id: IdMap.getId("test-variant"),
|
||||
title: "Test variant",
|
||||
},
|
||||
cart_id: IdMap.getId("test-cart"),
|
||||
title: "Test product",
|
||||
description: "Test variant",
|
||||
thumbnail: "",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
should_merge: true,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user