Browse Source

perf(core): Improve efficiency of order merge

We now make use of the new bulk operations for adding, updating &
removing OrderLines during the merge
Michael Bromley 1 year ago
parent
commit
0a60ee9f76
1 changed files with 13 additions and 27 deletions
  1. 13 27
      packages/core/src/service/services/order.service.ts

+ 13 - 27
packages/core/src/service/services/order.service.ts

@@ -77,6 +77,7 @@ import { grossPriceOf, netPriceOf } from '../../common/tax-utils';
 import { ListQueryOptions } from '../../common/types/common-types';
 import { ListQueryOptions } from '../../common/types/common-types';
 import { assertFound, idsAreEqual } from '../../common/utils';
 import { assertFound, idsAreEqual } from '../../common/utils';
 import { ConfigService } from '../../config/config.service';
 import { ConfigService } from '../../config/config.service';
+import { Logger } from '../../config/logger/vendure-logger';
 import { TransactionalConnection } from '../../connection/transactional-connection';
 import { TransactionalConnection } from '../../connection/transactional-connection';
 import { Channel } from '../../entity/channel/channel.entity';
 import { Channel } from '../../entity/channel/channel.entity';
 import { Customer } from '../../entity/customer/customer.entity';
 import { Customer } from '../../entity/customer/customer.entity';
@@ -818,7 +819,6 @@ export class OrderService {
         }
         }
         const orderLinesToDelete: OrderLine[] = [];
         const orderLinesToDelete: OrderLine[] = [];
         for (const orderLineId of orderLineIds) {
         for (const orderLineId of orderLineIds) {
-            // Validation check to ensure that the OrderLine exists on the Order
             const orderLine = this.getOrderLineOrThrow(order, orderLineId);
             const orderLine = this.getOrderLineOrThrow(order, orderLineId);
             orderLinesToDelete.push(orderLine);
             orderLinesToDelete.push(orderLine);
         }
         }
@@ -1760,41 +1760,27 @@ export class OrderService {
         }
         }
         if (order && linesToInsert) {
         if (order && linesToInsert) {
             const orderId = order.id;
             const orderId = order.id;
-            for (const line of linesToInsert) {
-                const result = await this.addItemToOrder(
-                    ctx,
-                    orderId,
-                    line.productVariantId,
-                    line.quantity,
-                    line.customFields,
-                );
-                if (!isGraphQlErrorResult(result)) {
-                    order = result;
-                }
-            }
+            const result = await this.addItemsToOrder(ctx, orderId, linesToInsert);
+            order = result.order;
         }
         }
         if (order && linesToModify) {
         if (order && linesToModify) {
             const orderId = order.id;
             const orderId = order.id;
-            for (const line of linesToModify) {
-                const result = await this.adjustOrderLine(
-                    ctx,
-                    orderId,
-                    line.orderLineId,
-                    line.quantity,
-                    line.customFields,
-                );
-                if (!isGraphQlErrorResult(result)) {
-                    order = result;
-                }
-            }
+            const result = await this.adjustOrderLines(ctx, orderId, linesToModify);
+            order = result.order;
         }
         }
         if (order && linesToDelete) {
         if (order && linesToDelete) {
             const orderId = order.id;
             const orderId = order.id;
-            for (const line of linesToDelete) {
-                const result = await this.removeItemFromOrder(ctx, orderId, line.orderLineId);
+            try {
+                const result = await this.removeItemsFromOrder(
+                    ctx,
+                    orderId,
+                    linesToDelete.map(l => l.orderLineId),
+                );
                 if (!isGraphQlErrorResult(result)) {
                 if (!isGraphQlErrorResult(result)) {
                     order = result;
                     order = result;
                 }
                 }
+            } catch (e: any) {
+                Logger.error(e.message, undefined, e.stack);
             }
             }
         }
         }
         const customer = await this.customerService.findOneByUserId(ctx, user.id);
         const customer = await this.customerService.findOneByUserId(ctx, user.id);