fix(pricing): calculate pricing repository query (#9265)

**What**
- fix wrong parentheses nesting when generating calculate pricing query
- filter out deleted price lists
- use built in DB values for time comparison

---

![Screenshot 2024-09-23 at 22 18 25](https://github.com/user-attachments/assets/f2fa3891-9dab-4646-947a-50d9a8f937c6)
![Screenshot 2024-09-23 at 22 05 23](https://github.com/user-attachments/assets/7b58b671-6587-4882-800b-275cfcbf369e)

---

TODO:
- [x] check after price list start/end_date is modified, PL prices are not retrieved properly anymore

---

FIXES CC-518
This commit is contained in:
Frane Polić authored and GitHub committed 2024-09-24 10:47:40 +00:00
1 parent 69f6645716
commit d721282600
1 file changed
+21 -11
@@ -54,7 +54,6 @@ export class PricingRepository
return [] return []
} }
const date = new Date().toISOString()
// Gets all the price set money amounts where rules match for each of the contexts // Gets all the price set money amounts where rules match for each of the contexts
// that the price set is configured for // that the price set is configured for
const priceSubQueryKnex = knex({ const priceSubQueryKnex = knex({
@@ -97,23 +96,34 @@ export class PricingRepository
) )
priceSubQueryKnex.orWhere((q) => { priceSubQueryKnex.orWhere((q) => {
for (const [key, value] of Object.entries(context)) { const nullPLq = q.whereNull("price.price_list_id")
q.orWhere({ nullPLq.andWhere((q) => {
"pr.attribute": key, for (const [key, value] of Object.entries(context)) {
"pr.value": value, q.orWhere({
}) "pr.attribute": key,
} "pr.value": value,
q.orWhere("price.rules_count", "=", 0) })
q.whereNull("price.price_list_id") }
q.orWhere("price.rules_count", "=", 0)
})
}) })
priceSubQueryKnex.orWhere((q) => { priceSubQueryKnex.orWhere((q) => {
q.whereNotNull("price.price_list_id") q.whereNotNull("price.price_list_id")
.whereNull("pl.deleted_at")
.andWhere(function () { .andWhere(function () {
this.whereNull("pl.starts_at").orWhere("pl.starts_at", "<=", date) this.whereNull("pl.starts_at").orWhere(
"pl.starts_at",
"<=",
knex.fn.now()
)
}) })
.andWhere(function () { .andWhere(function () {
this.whereNull("pl.ends_at").orWhere("pl.ends_at", ">=", date) this.whereNull("pl.ends_at").orWhere(
"pl.ends_at",
">=",
knex.fn.now()
)
}) })
.andWhere(function () { .andWhere(function () {
this.andWhere(function () { this.andWhere(function () {