fix(pricing): pricing context calculations only takes into account existing rule attributes (#10771)

* fix(pricing): pricing context calculations only takes into account existing rule attributes

* chore: add changeset
This commit is contained in:
Riqwan Thamir
2025-01-02 10:17:09 +01:00
committed by GitHub
parent 36019b7242
commit 6d989bc8cd
3 changed files with 48 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/pricing": patch
---
fix(pricing): pricing context calculations only takes into account existing rule attributes
@@ -613,6 +613,15 @@ medusaIntegrationTestRunner({
}) })
) )
await api.post(
`/store/carts/${cart.id}/line-items`,
{
variant_id: product.variants[0].id,
quantity: 100,
},
storeHeaders
)
let cartAfterExpensiveShipping = ( let cartAfterExpensiveShipping = (
await api.post( await api.post(
`/store/carts/${cart.id}/shipping-methods`, `/store/carts/${cart.id}/shipping-methods`,
@@ -631,7 +640,7 @@ medusaIntegrationTestRunner({
}), }),
]), ]),
payment_collection: expect.objectContaining({ payment_collection: expect.objectContaining({
amount: 6398, amount: 156398,
}), }),
}) })
) )
@@ -4,6 +4,7 @@ import {
MedusaError, MedusaError,
MikroOrmBase, MikroOrmBase,
PriceListStatus, PriceListStatus,
promiseAll,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
import { import {
@@ -60,12 +61,38 @@ export class PricingRepository
return [] return []
} }
const flattenedContext = Object.entries( // We query the rule tables to get all whitelisted rule attributes
flattenObjectToKeyValuePairs(context) // This will help cleanup the query and do a db query on only necessary rule attributes.
).filter( const priceRuleAttributesQuery = knex("price_rule")
([key, value]) => .distinct("attribute")
(isPresent(value) && !Array.isArray(value)) || .pluck("attribute")
(Array.isArray(value) && value.flat(1).length)
const priceListRuleAttributesQuery = knex("price_list_rule")
.distinct("attribute")
.pluck("attribute")
const [ruleAttributes, priceListRuleAttributes] = await promiseAll([
priceRuleAttributesQuery,
priceListRuleAttributesQuery,
])
const allowedRuleAttributes = [
...ruleAttributes,
...priceListRuleAttributes,
]
const flattenedKeyValuePairs = flattenObjectToKeyValuePairs(context)
const flattenedContext = Object.entries(flattenedKeyValuePairs).filter(
([key, value]) => {
const isValuePresent = !Array.isArray(value) && isPresent(value)
const isArrayPresent = Array.isArray(value) && value.flat(1).length
return (
allowedRuleAttributes.includes(key) &&
(isValuePresent || isArrayPresent)
)
}
) )
// Gets all the prices where rules match for each of the contexts // Gets all the prices where rules match for each of the contexts