Explorar el Código

fix(core): Fix order calculation with over 1000 active Promotions

We were previously getting the promotions via the ListQueryBuilder,
which has a default limit of 1000 items. So a user who was doing a data
import which was creating over 1000 Promotions found that, after that limit
was reached, the expected discounts were no longer being applied to the imported
orders.

This commit fixes it by making a direct DB query with no upper limit.
Michael Bromley hace 3 años
padre
commit
7c63f31a12

+ 9 - 4
packages/core/src/service/helpers/order-modifier/order-modifier.ts

@@ -462,10 +462,15 @@ export class OrderModifier {
         }
         }
 
 
         const updatedOrderLines = order.lines.filter(l => updatedOrderLineIds.includes(l.id));
         const updatedOrderLines = order.lines.filter(l => updatedOrderLineIds.includes(l.id));
-        const promotions = await this.connection.getRepository(ctx, Promotion).find({
-            where: { enabled: true, deletedAt: null },
-            order: { priorityScore: 'ASC' },
-        });
+        const promotions = await this.connection
+            .getRepository(ctx, Promotion)
+            .createQueryBuilder('promotion')
+            .leftJoin('promotion.channels', 'channel')
+            .where('channel.id = :channelId', { channelId: ctx.channelId })
+            .andWhere('promotion.deletedAt IS NULL')
+            .andWhere('promotion.enabled = :enabled', { enabled: true })
+            .orderBy('promotion.priorityScore', 'ASC')
+            .getMany();
         await this.orderCalculator.applyPriceAdjustments(ctx, order, promotions, updatedOrderLines, {
         await this.orderCalculator.applyPriceAdjustments(ctx, order, promotions, updatedOrderLines, {
             recalculateShipping: input.options?.recalculateShipping,
             recalculateShipping: input.options?.recalculateShipping,
         });
         });

+ 11 - 4
packages/core/src/service/services/order.service.ts

@@ -1726,10 +1726,17 @@ export class OrderService {
                 }
                 }
             }
             }
         }
         }
-        const { items: promotions } = await this.promotionService.findAll(ctx, {
-            filter: { enabled: { eq: true } },
-            sort: { priorityScore: 'ASC' },
-        });
+
+        const promotions = await this.connection
+            .getRepository(ctx, Promotion)
+            .createQueryBuilder('promotion')
+            .leftJoin('promotion.channels', 'channel')
+            .where('channel.id = :channelId', { channelId: ctx.channelId })
+            .andWhere('promotion.deletedAt IS NULL')
+            .andWhere('promotion.enabled = :enabled', { enabled: true })
+            .orderBy('promotion.priorityScore', 'ASC')
+            .getMany();
+
         const updatedItems = await this.orderCalculator.applyPriceAdjustments(
         const updatedItems = await this.orderCalculator.applyPriceAdjustments(
             ctx,
             ctx,
             order,
             order,