Skip to content
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

Add checkout and order promotions #14696

Merged
merged 33 commits into from
Feb 7, 2024

Conversation

IKarbowiak
Copy link
Member

@IKarbowiak IKarbowiak commented Nov 10, 2023

Allow creating promotions that applies on checkout/order level

Covers #13330

  • The solution allows the creation of promotion rules that define checkoutAndOrder discount applied on checkout/order level.
  • Current implementation applies the discount only on Checkout, it's not applicable on Order yet.
  • The limit of possible PromotionRule with checkoutAndOrder predicate was introduced. It's set to 100 for the default.

Measurement:

In case checkout applies for all 100 rules, the checkout calculation time is extended a bit.

Case 1

  • checkout with one line
  • 100 promotion rules with checkoutAndOrder predicate
  • all rules applies on the checkout
Mutation Time in s
CheckoutCreate 0.638
Checkout delivery method update 0.896
Checkout complete 0.775

Case 2

  • checkout with 1 line
  • no promotion rules
Mutation Time in s
CheckoutCreate 0.426
Checkout delivery method update 0.569
Checkout complete 0.713

Case 3

  • checkout with 100 lines
  • 100 promotion rules with checkoutAndOrder predicate
  • all rules applies on the checkout
Mutation Time in s
Checkout delivery method update 0.947
Checkout complete 1.64

Case 4

  • checkout with 100 lines
  • no promotion rules
Mutation Time in s
Checkout delivery method update 0.604
Checkout complete 2.2

Impact

  • New migrations
  • New/Updated API fields or mutations
  • Deprecated API fields or mutations
  • Removed API types, fields, or mutations

Docs

Pull Request Checklist

  • Privileged queries and mutations are either absent or guarded by proper permission checks
  • Database queries are optimized and the number of queries is constant
  • Database migrations are either absent or optimized for zero downtime
  • The changes are covered by test cases

@db-queries
Copy link

db-queries bot commented Nov 10, 2023

Here is the report for b7f5115 (saleor:feature/checkout-and-order-promotions)
Base comparison is 7b6b4d8.

Found 26 differences! (click me)

# saleor.graphql.accountbenchmark account
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  addresses for federation query count                                    	          9	          9	              2
  customers query                                                         	         51	         51	              0
  delete staff members                                                    	         41	         41	              0
  query staff user                                                        	         22	         22	              3
  staff create                                                            	         23	         23	              3
  staff update groups and permissions                                     	         36	         36	              5
  users for federation query count                                        	          8	          8	              3

# saleor.graphql.accountbenchmark permission group
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  groups for federation query count                                       	          5	          5	              0
  permission group create                                                 	         25	         25	              0
  permission group delete                                                 	         30	         30	              3
  permission group query                                                  	         14	         14	              0
  permission group update                                                 	         44	         44	              2
  permission group update remove users with manage staff                  	         36	         36	              3

# saleor.graphql.appbenchmarks app extensions
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  app extensions                                                          	         15	         15	              0
  app extensions with filter[filter0]                                     	         14	         14	              0
  app extensions with filter[filter1]                                     	         10	         10	              0
  app extensions with filter[filter2]                                     	         14	         14	              0
  app extensions with filter[filter3]                                     	         10	         10	              0

# saleor.graphql.appbenchmarks apps
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  apps for federation query count                                         	          9	          9	              3
  apps with tokens and webhooks                                           	          9	          9	              0

# saleor.graphql.attributebenchmark attribute
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  attribute translation                                                   	          6	          6	              0
  attribute value translation                                             	         20	         20	              0
  query attribute                                                         	          6	          6	              0
  query attributes                                                        	          8	          8	              0

# saleor.graphql.channelbenchmark channel
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  channels query                                                          	          5	          5	              0

# saleor.graphql.channelmutations channel update
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  channel update mutation remove warehouse                                	         38	         38	              6

# saleor.graphql.checkoutbenchmark checkout mutations
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
- add billing address to checkout                                         	         71	         75	             10
- add checkout lines                                                      	         74	         80	             11
- add checkout lines with external shipping                               	        178	        183	             74
- add checkout lines with reservations                                    	        157	        169	             59
- add delivery to checkout                                                	         72	         77	             12
- add shipping to checkout                                                	         82	         88	             15
- checkout email update                                                   	         37	         40	              1
- checkout payment charge                                                 	         51	         55	             15
- checkout shipping address update                                        	         76	         81	              9
- checkout voucher code                                                   	         94	        100	             16
- complete checkout                                                       	        234	        239	             81
- complete checkout preorder                                              	        232	        237	             82
- complete checkout with digital line                                     	        275	        280	             95
- complete checkout with out of stock webhook                             	        236	        241	             81
- complete checkout with single line                                      	        236	        241	             81
- create checkout                                                         	         77	         80	              8
- create checkout with reservations                                       	        136	        145	             24
- customer complete checkout                                              	        247	        252	             87
- customer complete checkout for cc                                       	        222	        227	             76
- update checkout lines                                                   	         67	         73	             11
- update checkout lines with reservations                                 	        163	        175	             70

# saleor.graphql.checkoutbenchmark checkouts
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  staff multiple checkouts                                                	          6	          6	              0

# saleor.graphql.checkoutbenchmark homepage
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
- user checkout details                                                   	         57	         59	              5
- user checkout details with tax app                                      	         51	         53	              2

# saleor.graphql.discountbenchmark promotion create
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  promotion create                                                        	         55	         55	              3

# saleor.graphql.discountbenchmark promotion delete
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
- promotion delete                                                        	         30	         31	              1

# saleor.graphql.discountbenchmark promotion rule create
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  promotion rule create                                                   	         40	         40	              0

# saleor.graphql.discountbenchmark promotion rule delete
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
- promotion rule delete                                                   	         20	         21	              0

# saleor.graphql.discountbenchmark promotion rule update
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  promotion rule update                                                   	         39	         39	              1

# saleor.graphql.discountbenchmark promotion update
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  promotion update                                                        	         38	         38	              1

# saleor.graphql.discountbenchmark promotions
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  promotions querytest promotions query                                   	          9	          9	              0

# saleor.graphql.discountbenchmark sales
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  sales query with channel slug                                           	          9	          9	              0
  sales query without channel slug                                        	          7	          7	              0

# saleor.graphql.discountbenchmark voucher code bulk delete
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  voucher code bulk delete queries                                        	         22	         22	              2

# saleor.graphql.discountbenchmark vouchers
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  vouchers query with channel slug                                        	         20	         20	              0
  vouchers query withot channel slug                                      	         19	         19	              0

# saleor.graphql.giftcardbenchmark gift card mutations
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  create never expiry gift card                                           	         26	         26	              4
  gift card bulk activate by staff                                        	         16	         16	              3
  update gift card                                                        	         36	         36	              4

# saleor.graphql.giftcardbenchmark gift card queries
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  filter gift cards by products                                           	         14	         14	              3
  filter gift cards by tags                                               	         12	         12	              3
  filter gift cards by used by user                                       	         13	         13	              3
  query gift card details                                                 	         14	         14	              3
  query gift cards                                                        	         12	         12	              3

# saleor.graphql.orderbenchmark fulfillment
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  fulfillment query                                                       	          6	          6	              0

# saleor.graphql.orderbenchmark fulfillment refund and return products
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  fulfillment refund products order lines                                 	         60	         60	              0
  fulfillment return products order lines                                 	        102	        102	              7

# saleor.graphql.orderbenchmark order
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  staff multiple draft orders                                             	          7	          7	              0
  staff multiple orders                                                   	          7	          7	              0
  staff order details                                                     	         56	         56	              7
  user order details                                                      	         54	         54	              8

# saleor.graphql.orderbenchmark order bulk create
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  order bulk create                                                       	         21	         21	              2

# saleor.graphql.orderbenchmark order fulfill
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  order fulfill                                                           	         19	         19	              0
  order fulfill with gift cards                                           	         19	         19	              0

# saleor.graphql.orderbenchmark order lines create
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  order lines create                                                      	         94	         94	             17
  order lines create variants on promotion                                	        155	        155	             22

# saleor.graphql.pagebenchmark page type
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  page types for federation query count                                   	          3	          3	              0
  query page type                                                         	         18	         18	              6
  query page types                                                        	         25	         25	             12

# saleor.graphql.paymentbenchmark payment transactions
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  payment transactions                                                    	         54	         54	             20

# saleor.graphql.productbenchmark category
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  categories children                                                     	          3	          3	              0
  categories for federation query count                                   	          3	          3	              0
+ category delete                                                         	         70	         65	              9
  category view                                                           	         33	         33	              4

# saleor.graphql.productbenchmark collection
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  collection add products                                                 	         46	         46	              8
  collection bulk delete                                                  	         54	         54	              6
  collection view                                                         	         11	         11	              3
  collections for federation query count                                  	          6	          6	              0
  create collection                                                       	         37	         37	              5
  delete collection                                                       	         51	         51	              7
  remove products from collection                                         	         43	         43	              8
  retrieve collection channel listings                                    	          5	          5	              0

# saleor.graphql.productbenchmark homepage
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  report product sales                                                    	         10	         10	              3
  retrieve product list                                                   	          4	          4	              0

# saleor.graphql.productbenchmark product
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  filter products by attributes                                           	         10	         10	              1
  filter products by boolean attributes                                   	         17	         17	              1
  filter products by gift card                                            	         14	         14	              2
  filter products by numeric attributes                                   	         16	         16	              1
  product create                                                          	         69	         69	              3
  product details                                                         	         46	         46	              4
  product translations                                                    	          8	          8	              1
  products for federation query count                                     	         12	         12	              4
  products media for federation query count                               	          5	          5	              0
  products types for federation query count                               	          2	          2	              0
  retrieve channel listings                                               	         20	         20	              0
  retrieve product attributes                                             	         11	         11	              1
  retrieve product images                                                 	          7	          7	              1
  retrieve product media                                                  	          7	          7	              1
  retrive products with product types and attributes                      	         10	         10	              1
  update product                                                          	         62	         62	              5

# saleor.graphql.productbenchmark product variant channel listing update
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  variant channel listing update                                          	         47	         47	              3

# saleor.graphql.productbenchmark variant
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  product variant bulk create                                             	         67	         67	              1
  product variant create                                                  	         71	         71	              3
  products variants for federation query count                            	         10	         10	              3
  retrieve variant list                                                   	         37	         37	              2
  update product variant                                                  	         69	         69	              5

# saleor.graphql.productbenchmark variant stocks
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  product variants stocks create                                          	         29	         29	              4
  product variants stocks create with single webhook called               	         28	         28	              4
  product variants stocks delete by id                                    	         30	         30	              4
  product variants stocks delete by sku                                   	         30	         30	              4
  product variants stocks delete with out of stock webhook many calls     	         26	         26	              3
  product variants stocks update by sku                                   	         28	         28	              4
  product variants stocks update byid                                     	         28	         28	              4
  query product variants stocks                                           	         10	         10	              0

# saleor.graphql.producttest product sorting attributes
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  sort product not having attribute data                                  	         20	         20	              0

# saleor.graphql.shippingbenchmark shipping methods
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  vouchers query with channel slug                                        	          7	          7	              0
  vouchers query without channel slug                                     	          7	          7	              0

# saleor.graphql.shippingbenchmark shipping zones
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  shipping zones query                                                    	          6	          6	              0

# saleor.graphql.shopbenchmark homepage
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  retrieve shop                                                           	          2	          2	              0

# saleor.graphql.warehousebenchmark stock bulk update
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  stocks bulk update queries count                                        	         33	         33	              5

# saleor.graphql.warehousebenchmark stocks
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  stocks query                                                            	          5	          5	              0

# saleor.graphql.webhookbenchmark webhook events
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  webhooks                                                                	          6	          6	              0

Copy link

codecov bot commented Nov 10, 2023

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (2cfe3bf) 93.91% compared to head (b7f5115) 93.92%.
Report is 5 commits behind head on main.

Files Patch % Lines
saleor/graphql/discount/utils.py 89.23% 3 Missing and 4 partials ⚠️
saleor/discount/utils.py 95.53% 2 Missing and 3 partials ⚠️
...graphql/discount/mutations/promotion/validators.py 97.08% 1 Missing and 2 partials ⚠️
saleor/order/utils.py 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14696      +/-   ##
==========================================
+ Coverage   93.91%   93.92%   +0.01%     
==========================================
  Files         929      929              
  Lines       70051    70386     +335     
  Branches     8081     8137      +56     
==========================================
+ Hits        65785    66108     +323     
+ Misses       2747     2746       -1     
- Partials     1519     1532      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@IKarbowiak IKarbowiak force-pushed the feature/checkout-and-order-promotions branch from c923b13 to 7369c8b Compare December 7, 2023 09:48
@IKarbowiak IKarbowiak force-pushed the feature/checkout-and-order-promotions branch from 7369c8b to 4dcafdb Compare December 15, 2023 14:10
@IKarbowiak IKarbowiak force-pushed the feature/checkout-and-order-promotions branch from 4dcafdb to 3e594c0 Compare December 27, 2023 12:15
@IKarbowiak IKarbowiak added the test deployment Deploy test environment for pull request label Jan 5, 2024
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions January 5, 2024 07:59 Inactive
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions January 8, 2024 13:24 Inactive
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions January 9, 2024 10:57 Inactive
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions January 10, 2024 09:18 Inactive
@yellowee yellowee added test deployment Deploy test environment for pull request and removed test deployment Deploy test environment for pull request labels Jan 11, 2024
IKarbowiak and others added 12 commits January 12, 2024 12:12
* Refactor method for fetching variants for predicate

* Limit querysets used for filtering

* Fix failing tests
…5054)

* Add checkout_and_order_predicate field to promotion_rule graphql type.

* Add reward_type field.

* Add predicate types.

* Temporarily turn checkoutAndOrderPredicate field into JSON.

* Add input types.

* Validate new input fields.

* Cover validations with tests.

* Remove stuff from previous approach.

* Do not allow mixing predicates.

* Fix promotion rule validation

* Adjust clean_promotion_rule validator

* Extend PromotionCreate validation

* Unify promotion rule validations

* Rename PredicateType ORDER field to CHECKOUT_AND_ORDER

* Add more tests to PromotionRuleUpdate

* Update promotion/validators.py file

* Add docstrings to functions in promotion validators.py file

* Add tests for promotion validators

* Add validation for price based predicate and rule with mixed currencies

* Add mising test for PromotionRuleCreate

* Clear graphql/discount/utils.py

* Fix migrations and removed not needed changes

* Apply code review suggestions

* Refactor get_predicate_type function.

---------

Co-authored-by: IKarbowiak <iga.karbowiak@saleor.io>
* Add CheckoutDiscountedObjectWhere

* Add fetch_promotion_rules_for_checkout method

* Introduce PredicateObjectType

* Add schema of including checkout and order discount

* Extend discount models

* Include checkout and order promotion discount in checkoucalculations

* Adjust create or update checkout discount - remove CheckoutDiscount when the voucher is assigned and update CheckoutOrder if any exist

* Adjust checkout discount creation for checkout and order promotions

* Delete CheckoutDiscount when no rule applies anymore

* Add tests for CheckoutLinesAdd

* Fix failing tests

* Add test for generate_checkout_payload_for_tax_calculation

* Include checkout discounts in CheckoutInfo dataloader

* Add tests for checkout base calculations

* Add tests for webhooks

* Add tests for recalculate_checkout_discount

* Add tests for price_override

* Add tests for avatax

* Add test for calculate_checkout_total method for Avatax plugin

* Add test for checkout calculation

* Fix migrations

* Apply code review suggestions
* Adjsut CheckoutAddPromoCode

* Adjust checkoutRemovePromoCode

* Update test for recalculate_checkout_discount
* Rename checkoutAndOrder discount to order discount

* Rename occurrences in comments.

---------

Co-authored-by: zedzior <piotr.zabieglik@gmail.com>
* Add unique constrain on CheckoutDiscount model.

* Use get_or_create when creating CheckoutDiscount.

* Make it working.

* Add test.

* Add additional test.
* Adjust total_price filtering in CheckoutDiscountedObjectWhere

* Add filtering by subtotal_price in CheckoutDiscountedObjectWhere

* Apply code review suggestiins and change DiscountedObjectPredicateInput to DiscountedObjectWhereInput

* Fix failing CI

* Fix failing test
@IKarbowiak IKarbowiak force-pushed the feature/checkout-and-order-promotions branch from 2aaee36 to 34b5150 Compare January 12, 2024 11:27
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions January 29, 2024 10:35 Inactive
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions January 29, 2024 11:15 Inactive
* Convert camel case to snake case.

* Improve tests.

* Correct predicates in tests.

* Apply review comments.
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions January 31, 2024 10:58 Inactive
* Check if clear discounts is needed; call the function once

* Update base_prices if needed only.

* Fix tests.

* Refactor.
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions February 2, 2024 12:56 Inactive
@IKarbowiak IKarbowiak requested review from a team and removed request for a team February 2, 2024 13:38
* Optimize 'fetch_promotion_rules_for_checkout' function.

* Fix tests.

* Remove 'list(iterator)' pattern.
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions February 5, 2024 09:37 Inactive
* Add filter by promotion type

* Apply changes after review

* Fix test to use correct promotion types
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions February 5, 2024 10:20 Inactive
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions February 5, 2024 14:33 Inactive
Copy link
Member

@kadewu kadewu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some possible performance improvments from my site, overall LGTM!

saleor/discount/interface.py Show resolved Hide resolved
saleor/discount/utils.py Outdated Show resolved Hide resolved
@kadewu kadewu requested a review from a team February 6, 2024 14:49
@github-actions github-actions bot temporarily deployed to feature-checkout-and-order-promotions February 6, 2024 16:07 Inactive
@kadewu kadewu requested a review from a team February 6, 2024 17:37
@tomaszszymanski129 tomaszszymanski129 requested a review from a team February 7, 2024 09:21
@IKarbowiak IKarbowiak merged commit b2a6e00 into main Feb 7, 2024
10 checks passed
@IKarbowiak IKarbowiak deleted the feature/checkout-and-order-promotions branch February 7, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.19 migrations perf test Runs performance tests on modified migrations promotions test deployment Deploy test environment for pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants