-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(medusa): Cart and totals improvements #2475
Conversation
|
b3f3481
to
dc56aa5
Compare
Woooo, I'm excited to try this out tomorrow! Can you try adding a similar thing with the tax_lines for OrderService::decorateTotals also? I'm seeing some long non-database performance times listing orders as well. Thanks! |
31c770b
to
aa9c540
Compare
Man, this is awesome, checkout cart went from 10 seconds to 2 seconds! |
1d75454
to
dded247
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple of small comments. I tested it using medusa-dev and everything seems to work correctly and fast 🚀!
c900531
to
73f8cd8
Compare
d500d72
to
1570bda
Compare
6b1c30c
to
77ec556
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more fixes and we should be ready to go 👍
71db7d0
to
aaba280
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good - a few tests and we can merge :)
1ae63fc
to
b579316
Compare
I suggest to re review the PR just to be sure :) |
2e4e27d
to
50585f8
Compare
@srindom Do you have any more comments on this one? Let us get it merged, deployed to staging, and tested asap 💪 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Cart/Order performance improvement, more specifically during cart completion
The purpose of that Pr is to drastically reduce the response time of the cart completion strategy.
The improvements made here have also some side effects which improve the performances of other part of the code but
it was not the main goal of it.
All improvements will be applicable in later PR's to also improve everything related to cart and order end point
and totals calculations.
Totals calculation
Some improvements have been made to the totals service, in order to not break the existing API right away, it has been
decided by myself (but can evolve) to create a new service
TotalsNewService
where all new methods are implemented and consumed from.Generally speaking there is no mention of any specific entities such as the Cart or Order entity as the different methods
explicitly describe the expected arguments which makes it flexible and do not enforce to pass a specific entity
but only an object that contains the expected properties. The overall idea is to have a totals service
less coupled to specific entities but just do its job which is to compute some calculations based on the
expected arguments.
The overall changes are the following:
This method accept a collection of
items
as well as some optionsincludeTax
which only enforce the tax_lines to exist either provided by the item itself or the ones that will be queried internally through the tax provider service ifuseExistingTaxLines
is not set to truecalculationContext
which is the usual calculation contexttaxRate
which is only there to support backward compatibility. I choose to keep it but the methods have been separateduseExistingTaxLines
which enforce the usage of the tax lines from the items. Can be useful in cases wherethe tax_lines already exists or have been already fetched and assigned by the consumer in order to not re fetch them if
it is not necessary
This method is of a higher rank as it will only take care of using the right tax lines and call another method
to compute the totals of each item.
To compute the totals of each item there is now two methods which are described bellow
As the name describe, this method calcul the totals for the legacy backward compatibility cases where the
taxRate
isexplicitly provided to be used. It is normally in the case of the order.
Which is used to calculate the totals with the new approach using the tax lines.
In that method the options
includeTax
as been kept only to throw if no tax lines are provided by any of thepossible way, either from the item or explicitly provided
This method take a partial item composed of the necessary properties needed to calculate the refund amount.
It is also possible to provide an optional
taxRate
to calculate it the legacy way where the method will callthe getLineItemRefundLegacy method for that.
Now let's look at the gift card part of the totals below
This method take now a maximun amount that can be gifted, it is possible to pass either the
giftCards
or thegiftCardTransactions
as part of the options. If the
giftCardTransactions
are provided then a separate method will be calledgetGiftCardTransactionsTotals
in order to provided the right calculation for that collection otherwise the
giftCards
will be usedto calculate the amount.
This method take care of computing the totals based on the transactions and the region
This method accept a collection of
shippingMethods
as well as some optionsincludeTax
which only enforce the tax_lines to exist either provided by the method itself or the ones that will be queried internally through the tax provider service ifuseExistingTaxLines
is not set to truediscounts
calculationContext
which is the usual calculation contexttaxRate
which is only there to support backward compatibility. I choose to keep it but the methods have been separateduseExistingTaxLines
which enforce the usage of the tax lines from the method. Can be useful in cases wherethe tax_lines already exists or have been already fetched and assigned by the consumer in order to not re fetch them if
it is not necessary
This method is of a higher rank as it will only take care of using the right tax lines and call another method
to compute the totals of each method.
To compute the totals of each method there is now two methods which are described bellow
As the name describe, this method calcul the totals for the legacy backward compatibility cases where the
taxRate
isexplicitly provided to be used. It is normally in the case of the order.
Which is used to calculate the totals with the new approach using the tax lines.
In that method the options
includeTax
as been kept only to throw if no tax lines are provided by any of thepossible way, either from the method or explicitly provided
Cart completion
The main purpose of the previous improvements and refactoring was to improve the overall performances
of the cart completion flow. To achieve that, multiple changes needed to be made and will be described bellow.
Cart service and Order service
Most of the changes in the service are separated in two categories
Some of the method was always expecting a cart id and then was querying the expected cart. In some cases
we are already holding the cart and it make sense that we have the flexibility to provide this cart
instead of fetching it again, expect in those cases where we are not sure what to provide and therefore
we prefer giving the hand to the method to fetch if with the appropriate relations. Therefore for some of the
service methods it is now possible to either give and id or the entity directly. For the later case it
is much more for the people in need of performance improvement and in that case it is the consumer who must
be sure to pass the entity with the appropriate relations.
Finally, the
decorateTotals
had been reworked in order to provide a better readability, understandability and performances.To do so, the hidden quadratic and cubic loops have been replaces by the usage of map or object and the overall
loops have been simplified or reduced. We have therefore remove O(n^(2 or 3)) + XO(n) by only XO(n) where X is inferior
to what it was before. Also, we now take advantage of being able to query the tax lines or use the existing one
before calculating the total which gives more flexibility and reduce the number of time we rely on an
external party. For the example, for a cart with 4 items we have now few calls to the external party instead of 64 for the overall flow.
Before those changes, the number a call was constantly increasing with the number of item in the cart, it is not
the case anymore.
The overall decorate totals of the order service have also been improved the same way the cart has been improved and therefore
the readability, understandability and performances have been improved.
Summary
Some other adjustments have been made along the way in order to improve the usability and simplify the usage of
all the changes and open the door to more refactoring (but simpler) in the entire code base where we are relying
on the totals, cart and order. Therefore, future PR's will be open to tackle those refactoring and align the code base
to move toward the new ways of calculating the totals and the usage of the new api's. It will increase the overall
performances of everything that is related to the cart or the order. It should be taken into consideration
that the future changes will probably also impact the payment provider if they rely on one of the entities
mention above. Also, the actual totals service could potentially be deprecated until a certain period of time
in favour of the new approached.
FIXES CORE-327