feat(medusa): Performance improvements of Carts domain (#2648)

**What**

I have created a new method on the cart service which is `addLineItems`, allowing a user to add one or multiple items in an optimized way. Also updated the `generate` method from the line item service which now also accept a object data or a collection of data which. Various places have been optimized and cache support has been added to the price selection strategy.

The overall optimization allows to reach another 9000% improvement in the response time as a median (Creating a cart with 6 items):

|   | Min (ms)  | Median (ms)  | Max (ms)  | Median Improvement (%)
|---|:-:|---|---|---|
| Before optimisation  | 1200  | 9999 | 12698  |  N/A
| After optimisation | 63  | 252  | 500  | 39x
| After re optimisation | 56 | 82  | 399  | 121x
| After including addressed feedback | 65 | 202  | 495  | 49x

FIXES CORE-722
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2022-12-07 14:39:12 +00:00
1 parent 3b2c929408
commit 42d9c7222b
71 files changed
+1204 -439

No files matched your search

@@ -1,6 +1,8 @@
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
import { FlagRouter } from "../../utils/flag-router"
import DiscountService from "../discount"
import { TotalsServiceMock } from "../__mocks__/totals"
import { newTotalsServiceMock } from "../__mocks__/new-totals"
const featureFlagRouter = new FlagRouter({})
@@ -601,15 +603,28 @@ describe("DiscountService", () => {
})
const totalsService = {
getSubtotal: () => {
...TotalsServiceMock,
getSubtotal: async () => {
return 1100
},
}
const newTotalsService = {
...newTotalsServiceMock,
getLineItemTotals: async () => {
return [
{
subtotal: 1100,
},
]
},
}
const discountService = new DiscountService({
manager: MockManager,
discountRepository,
totalsService,
newTotalsService,
featureFlagRouter,
})
@@ -631,21 +646,31 @@ describe("DiscountService", () => {
})
it("correctly calculates fixed + total discount", async () => {
let item = {
unit_price: 400,
quantity: 2,
allow_discounts: true,
}
const adjustment1 = await discountService.calculateDiscountForLineItem(
"disc_fixed_total",
item,
{
unit_price: 400,
quantity: 2,
allow_discounts: true,
items: [item],
}
)
item = {
unit_price: 300,
quantity: 1,
allow_discounts: true,
}
const adjustment2 = await discountService.calculateDiscountForLineItem(
"disc_fixed_total",
item,
{
unit_price: 300,
quantity: 1,
allow_discounts: true,
items: [item],
}
)