From 3c208372d26d4ac4c3e50f03a3738a359d76b7ae Mon Sep 17 00:00:00 2001 From: Thomas Blommaert <66438062+thomas-advantitge@users.noreply.github.com> Date: Thu, 11 Feb 2021 21:34:34 +0100 Subject: [PATCH] perf(core): Improve order quantity update performanc --- .../helpers/order-modifier/order-modifier.ts | 20 +++++++++---------- .../src/service/services/order.service.ts | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/core/src/service/helpers/order-modifier/order-modifier.ts b/packages/core/src/service/helpers/order-modifier/order-modifier.ts index 9ff0edb0d7..1f81f89fff 100644 --- a/packages/core/src/service/helpers/order-modifier/order-modifier.ts +++ b/packages/core/src/service/helpers/order-modifier/order-modifier.ts @@ -150,18 +150,18 @@ export class OrderModifier { if (!orderLine.items) { orderLine.items = []; } + const newOrderItems = []; for (let i = currentQuantity; i < quantity; i++) { - const orderItem = await this.connection.getRepository(ctx, OrderItem).save( - new OrderItem({ - listPrice: orderLine.productVariant.price, - listPriceIncludesTax: orderLine.productVariant.priceIncludesTax, - adjustments: [], - taxLines: [], - line: orderLine, - }), - ); - orderLine.items.push(orderItem); + newOrderItems.push(new OrderItem({ + listPrice: orderLine.productVariant.price, + listPriceIncludesTax: orderLine.productVariant.priceIncludesTax, + adjustments: [], + taxLines: [], + line: orderLine, + })); } + await this.connection.getRepository(ctx, OrderItem).createQueryBuilder().insert().values(newOrderItems); + orderLine.items = orderLine.items.concat(newOrderItems); } else if (quantity < currentQuantity) { if (order.active) { // When an Order is still active, it is fine to just delete diff --git a/packages/core/src/service/services/order.service.ts b/packages/core/src/service/services/order.service.ts index 8293397f24..8dd508aaff 100644 --- a/packages/core/src/service/services/order.service.ts +++ b/packages/core/src/service/services/order.service.ts @@ -1329,8 +1329,8 @@ export class OrderService { updatedOrderLine ? [updatedOrderLine] : [], ); await this.connection.getRepository(ctx, Order).save(order, { reload: false }); - await this.connection.getRepository(ctx, OrderItem).save(updatedItems, { reload: false }); - await this.connection.getRepository(ctx, ShippingLine).save(order.shippingLines, { reload: false }); + await this.connection.getRepository(ctx, OrderItem).createQueryBuilder().insert().values(updatedItems); + await this.connection.getRepository(ctx, ShippingLine).createQueryBuilder().insert().values(order.shippingLines); return order; }