chore(pricing): Pricing retrieval improvements (#12128)

**What**
I have removed the check for the context key where it was fetching all attributes available and then stripping out the one that does not exists.. On big dataset these would remove multiple hundreds of ms of query execution
This commit is contained in:
Adrien de Peretti
2025-04-10 11:39:21 +02:00
committed by GitHub
parent 6ae1e7b708
commit 07252691c5
8 changed files with 671 additions and 127 deletions

View File

@@ -286,6 +286,171 @@ moduleIntegrationTestRunner<IPricingModuleService>({
})
})
it("should successfully calculate prices with complex context", async () => {
const context = {
id: "cart_01JRDH08QD8CZ0KJDVE410KM1J",
currency_code: "PLN",
email: "tony@stark-industries.com",
region_id: "reg_01JRDH08ENY3276P6133BVXGWJ",
created_at: "2025-04-09T14:59:24.526Z",
updated_at: "2025-04-09T14:59:24.526Z",
completed_at: null,
total: 1500,
subtotal: 1428.5714285714287,
tax_total: 71.42857142857143,
discount_total: 0,
discount_subtotal: 0,
discount_tax_total: 0,
original_total: 1500,
original_tax_total: 71.42857142857143,
item_total: 1500,
item_subtotal: 1428.5714285714287,
item_tax_total: 71.42857142857143,
original_item_total: 1500,
original_item_subtotal: 1428.5714285714287,
original_item_tax_total: 71.42857142857143,
shipping_total: 0,
shipping_subtotal: 0,
shipping_tax_total: 0,
original_shipping_tax_total: 0,
original_shipping_subtotal: 0,
original_shipping_total: 0,
credit_line_subtotal: 0,
credit_line_tax_total: 0,
credit_line_total: 0,
metadata: null,
sales_channel_id: "sc_01JRDH08KWX1AR5SB0A3THWWQQ",
shipping_address_id: "caaddr_01JRDH08QDXHV9SJXKHT04TXK0",
customer_id: "cus_01JRDH08ATYB5AMFEZDTWCQWNK",
items: [
{
id: "cali_01JRDH08QDQH3CB1DE4S79HREC",
thumbnail: null,
variant_id: "variant_01JRDH08GJCZQB4GZCDDTYMD1V",
product_id: "prod_01JRDH08FPZ6QBZQ096B310RM7",
product_type_id: null,
product_title: "Medusa T-Shirt",
product_description: null,
product_subtitle: null,
product_type: null,
product_collection: null,
product_handle: "t-shirt",
variant_sku: "SHIRT-S-BLACK",
variant_barcode: null,
variant_title: "S / Black",
requires_shipping: true,
metadata: {},
created_at: "2025-04-09T14:59:24.526Z",
updated_at: "2025-04-09T14:59:24.526Z",
title: "S / Black",
quantity: 1,
unit_price: 1500,
compare_at_unit_price: null,
is_tax_inclusive: true,
tax_lines: [
{
id: "calitxl_01JRDH08RJEQ4WXXDTJYWV7B4M",
description: "CA Default Rate",
code: "CADEFAULT",
rate: 5,
provider_id: "system",
},
],
adjustments: [],
product: {
id: "prod_01JRDH08FPZ6QBZQ096B310RM7",
collection_id: null,
type_id: null,
categories: [],
tags: [],
},
},
],
shipping_methods: [],
shipping_address: {
id: "caaddr_01JRDH08QDXHV9SJXKHT04TXK0",
first_name: null,
last_name: null,
company: null,
address_1: "test address 1",
address_2: "test address 2",
city: "SF",
postal_code: "94016",
country_code: "US",
province: "CA",
phone: null,
},
billing_address: null,
credit_lines: [],
customer: {
id: "cus_01JRDH08ATYB5AMFEZDTWCQWNK",
email: "tony@stark-industries.com",
groups: [],
},
region: {
id: "reg_01JRDH08ENY3276P6133BVXGWJ",
name: "US",
currency_code: "usd",
automatic_taxes: true,
countries: [
{
iso_2: "us",
iso_3: "usa",
num_code: "840",
name: "UNITED STATES",
display_name: "United States",
region_id: "reg_01JRDH08ENY3276P6133BVXGWJ",
metadata: null,
created_at: "2025-04-09T14:59:20.275Z",
updated_at: "2025-04-09T14:59:24.250Z",
deleted_at: null,
},
],
},
promotions: [],
}
const calculatedPrice = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{ context: context as any }
)
expect(calculatedPrice).toEqual([
{
id: "price-set-PLN",
is_calculated_price_price_list: false,
is_calculated_price_tax_inclusive: false,
calculated_amount: 1000,
raw_calculated_amount: {
value: "1000",
precision: 20,
},
is_original_price_price_list: false,
is_original_price_tax_inclusive: false,
original_amount: 1000,
raw_original_amount: {
value: "1000",
precision: 20,
},
currency_code: "PLN",
calculated_price: {
id: "price-PLN",
price_list_id: null,
price_list_type: null,
min_quantity: 1,
max_quantity: 10,
},
original_price: {
id: "price-PLN",
price_list_id: null,
price_list_type: null,
min_quantity: 1,
max_quantity: 10,
},
},
])
})
it("should throw an error when currency code is not set", async () => {
let result = service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },