From d7212826007c69f57046086e834d71473d0202f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:47:40 +0200 Subject: [PATCH] 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 --- .../pricing/src/repositories/pricing.ts | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/modules/pricing/src/repositories/pricing.ts b/packages/modules/pricing/src/repositories/pricing.ts index 55865d308a..6fa8c34490 100644 --- a/packages/modules/pricing/src/repositories/pricing.ts +++ b/packages/modules/pricing/src/repositories/pricing.ts @@ -54,7 +54,6 @@ export class PricingRepository return [] } - const date = new Date().toISOString() // Gets all the price set money amounts where rules match for each of the contexts // that the price set is configured for const priceSubQueryKnex = knex({ @@ -97,23 +96,34 @@ export class PricingRepository ) priceSubQueryKnex.orWhere((q) => { - for (const [key, value] of Object.entries(context)) { - q.orWhere({ - "pr.attribute": key, - "pr.value": value, - }) - } - q.orWhere("price.rules_count", "=", 0) - q.whereNull("price.price_list_id") + const nullPLq = q.whereNull("price.price_list_id") + nullPLq.andWhere((q) => { + for (const [key, value] of Object.entries(context)) { + q.orWhere({ + "pr.attribute": key, + "pr.value": value, + }) + } + q.orWhere("price.rules_count", "=", 0) + }) }) priceSubQueryKnex.orWhere((q) => { q.whereNotNull("price.price_list_id") + .whereNull("pl.deleted_at") .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 () { - this.whereNull("pl.ends_at").orWhere("pl.ends_at", ">=", date) + this.whereNull("pl.ends_at").orWhere( + "pl.ends_at", + ">=", + knex.fn.now() + ) }) .andWhere(function () { this.andWhere(function () {