Skip to content

Commit

Permalink
perf(core): Improve efficiency of order merge
Browse files Browse the repository at this point in the history
We now make use of the new bulk operations for adding, updating &
removing OrderLines during the merge
  • Loading branch information
michaelbromley committed Oct 28, 2024
1 parent 8d65219 commit 0a60ee9
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { grossPriceOf, netPriceOf } from '../../common/tax-utils';
import { ListQueryOptions } from '../../common/types/common-types';
import { assertFound, idsAreEqual } from '../../common/utils';
import { ConfigService } from '../../config/config.service';
import { Logger } from '../../config/logger/vendure-logger';
import { TransactionalConnection } from '../../connection/transactional-connection';
import { Channel } from '../../entity/channel/channel.entity';
import { Customer } from '../../entity/customer/customer.entity';
Expand Down Expand Up @@ -818,7 +819,6 @@ export class OrderService {
}
const orderLinesToDelete: OrderLine[] = [];
for (const orderLineId of orderLineIds) {
// Validation check to ensure that the OrderLine exists on the Order
const orderLine = this.getOrderLineOrThrow(order, orderLineId);
orderLinesToDelete.push(orderLine);
}
Expand Down Expand Up @@ -1760,41 +1760,27 @@ export class OrderService {
}
if (order && linesToInsert) {
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) {
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) {
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)) {
order = result;
}
} catch (e: any) {
Logger.error(e.message, undefined, e.stack);
}
}
const customer = await this.customerService.findOneByUserId(ctx, user.id);
Expand Down

0 comments on commit 0a60ee9

Please sign in to comment.