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:
5
.changeset/real-pianos-eat.md
Normal file
5
.changeset/real-pianos-eat.md
Normal 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 = (
|
||||
await api.post(
|
||||
`/store/carts/${cart.id}/shipping-methods`,
|
||||
@@ -631,7 +640,7 @@ medusaIntegrationTestRunner({
|
||||
}),
|
||||
]),
|
||||
payment_collection: expect.objectContaining({
|
||||
amount: 6398,
|
||||
amount: 156398,
|
||||
}),
|
||||
})
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
MedusaError,
|
||||
MikroOrmBase,
|
||||
PriceListStatus,
|
||||
promiseAll,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
import {
|
||||
@@ -60,12 +61,38 @@ export class PricingRepository
|
||||
return []
|
||||
}
|
||||
|
||||
const flattenedContext = Object.entries(
|
||||
flattenObjectToKeyValuePairs(context)
|
||||
).filter(
|
||||
([key, value]) =>
|
||||
(isPresent(value) && !Array.isArray(value)) ||
|
||||
(Array.isArray(value) && value.flat(1).length)
|
||||
// We query the rule tables to get all whitelisted rule attributes
|
||||
// This will help cleanup the query and do a db query on only necessary rule attributes.
|
||||
const priceRuleAttributesQuery = knex("price_rule")
|
||||
.distinct("attribute")
|
||||
.pluck("attribute")
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user